Function writeData

I'm having a problem creating a menu using a switch statement

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2048 Views - Last Post: 18 June 2009 - 09:00 AM Rate Topic: -----

#1 gork4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-June 09

Function writeData

Post icon  Posted 16 June 2009 - 07:22 AM

void menu(void) {
//allow user to choose to append records, display records or exit the program
char answer;

cout << "(A)ppend Records, (S)how Records, (E)xit" << endl;
cout << "Please enter your option: ";
cin >> answer;
while (answer != 'e' || answer != 'E')
{
switch (answer)
{
case 'A':
case 'a':
writeData();
break;
case 'S':
case 's':
readData();
break;
case 'E':
case 'e':
cout << "You chose to exit the program!" << endl;
break;
default:
cout << "Invalid option - please choose a valid option";
break;
}

}

}//end menu


// its creating an infinite loop and i'm not understanding why please help

Is This A Good Question/Topic? 0
  • +

Replies To: Function writeData

#2 AGRAC393  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 248
  • Joined: 13-May 09

Re: Function writeData

Posted 16 June 2009 - 07:54 AM

Don't forget to use the [ code ] [ /code ] tags. But, that aside, are you getting any errors? What exactly is your question?
Was This Post Helpful? 0
  • +
  • -

#3 metaphorical  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-May 09

Re: Function writeData

Posted 16 June 2009 - 01:36 PM

View Postgork4life, on 16 Jun, 2009 - 06:22 AM, said:

void menu(void) {
//allow user to choose to append records, display records or exit the program
char answer;

cout << "(A)ppend Records, (S)how Records, (E)xit" << endl;
cout << "Please enter your option: ";
cin >> answer;
while (answer != 'e' || answer != 'E')
{
switch (answer)
{
case 'A':
case 'a':
writeData();
break;
case 'S':
case 's':
readData();
break;
case 'E':
case 'e':
cout << "You chose to exit the program!" << endl;
break;
default:
cout << "Invalid option - please choose a valid option";
break;
}

}

}//end menu


// its creating an infinite loop and i'm not understanding why please help


I am guessing it is your input being outside of your while loop making you stay in the infinite loop try this.
void menu(void) {
//allow user to choose to append records, display records or exit the program
char answer;
	
	while (answer != 'e' || answer != 'E')
	{
			system("CLS");
			cout << "(A)ppend Records, (S)how Records, (E)xit" << endl;
			cout << "Please enter your option: ";
			cin >> answer;
			switch (answer)
			{
			case 'A':
			case 'a':
				writeData();
				break;
			case 'S':
			case 's':
				readData();
				break;
			case 'E':
			case 'e':
				cout << "You chose to exit the program!" << endl;
				break;
			default:
				cout << "Invalid option - please choose a valid option";
				break;
			}

	}	

}//end menu




basically having the user input on the outside of the while loop only allows the user to input information once so put it in the loop so they can choose an option everytime the loop rolls through. and to keep it clean i added the
system("CLS");
so that it will stay pretty without a bunch of built up menu selections.

This post has been edited by metaphorical: 16 June 2009 - 01:37 PM

Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Function writeData

Posted 16 June 2009 - 01:50 PM

NEVER use
system() 
commands!!!!! :crazy:

I dont understand why you have a while loop !=E
if your switch statment has conditions for E?

but heres a cleaner version,

void menu(){
char answer;
cout<<"(A)ppend Records, (S)how Records, (E)xit"<<endl
	  <<"Please enter your option: ";
cin>>answer; 
switch(toupper(answer)){
case 'A':
writeData();break;
case 'S':
readData();break;
case 'E':
cout << "You chose to exit the program!" << endl; return;
break;
default:
cout << "Invalid option - please choose a valid option"; break;
}
}


This post has been edited by ImaSexy: 16 June 2009 - 01:55 PM

Was This Post Helpful? 0
  • +
  • -

#5 metaphorical  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-May 09

Re: Function writeData

Posted 16 June 2009 - 07:39 PM

well sorry about that man, was just to make it all pretty looking on the screen. yes there are other ways to do it but it was quick and easy for the moment so guy could see what was going on clearly without a lot of clutter in his console.
Was This Post Helpful? 0
  • +
  • -

#6 gork4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-June 09

Re: Function writeData

Posted 17 June 2009 - 05:51 AM

I figured it out I had to and && instead of || in the while condition. This is what my teacher told me and it worked. Thanks for all the input.
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Function writeData

Posted 17 June 2009 - 07:52 AM

View Postgork4life, on 17 Jun, 2009 - 04:51 AM, said:

I figured it out I had to and && instead of || in the while condition. This is what my teacher told me and it worked. Thanks for all the input.


a && in your while condition? so your saying your input has to be equal to 'e' and 'E', that doesnt make sense?

here:
answer = toupper(answer);
while (answer != 'E')



Was This Post Helpful? 0
  • +
  • -

#8 metaphorical  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-May 09

Re: Function writeData

Posted 17 June 2009 - 10:54 AM

dude the && statement seems kinda wierd. Why don't you just do this:

with the while loop put this


while(true)
{
	  //insert code here please put input statement inside while so the user can have a looping menu

}





then in your switch statement when they hit E or e end the program.

case 'E':
case 'e':
	cout << "You have ended the program" << endl;
	return 0;




and if you leave the input statement inside the loop like I said you will be set. Literally you don't
have to have the while statement watch to see if E or e is hit you just have to end the program within the
E or e statement in the switch code.
Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Function writeData

Posted 17 June 2009 - 11:10 AM

View Postmetaphorical, on 17 Jun, 2009 - 09:54 AM, said:

dude the && statement seems kinda wierd. Why don't you just do this:

with the while loop put this


while(true)
{
	  //insert code here please put input statement inside while so the user can have a looping menu

}





then in your switch statement when they hit E or e end the program.

case 'E':
case 'e':
	cout << "You have ended the program" << endl;
	return 0;




and if you leave the input statement inside the loop like I said you will be set. Literally you don't
have to have the while statement watch to see if E or e is hit you just have to end the program within the
E or e statement in the switch code.


thats along the lines of my eariler post.
but its a void function so it cant return a value,
but you can just say return;
i think a do{}while would be bettter
Was This Post Helpful? 0
  • +
  • -

#10 gork4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-June 09

Re: Function writeData

Posted 17 June 2009 - 01:33 PM

View PostImaSexy, on 17 Jun, 2009 - 06:52 AM, said:

View Postgork4life, on 17 Jun, 2009 - 04:51 AM, said:

I figured it out I had to and && instead of || in the while condition. This is what my teacher told me and it worked. Thanks for all the input.


a && in your while condition? so your saying your input has to be equal to 'e' and 'E', that doesnt make sense?

here:
answer = toupper(answer);
while (answer != 'E')




well as you know && returns false when either condition is false. While || tests the first condition and if that's true it will go to the next condition.

while(answer != 'e' || answer != ''E)

means that if the user presses 'e' the first condition will turn false, while the second condition will still remain true. But both have to be false to exit out the loop.
Was This Post Helpful? 0
  • +
  • -

#11 gork4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-June 09

Re: Function writeData

Post icon  Posted 17 June 2009 - 01:43 PM

I'm having a hard time figuring out why writeData is outputing the prompt for a name and the prompt for the street in my code.


//Specification: Append and display records in a address database

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void menu(void);
void writeData(void);
void readData(void);
string * split(string, char);

const char FileName[] = "c:/TestAddress.txt";

int main () {
		menu();
		return 0;
} //end main

void menu(void) {
//allow user to choose to append records, display records or exit the program

	char answer = ' ';
	cout << "(A)ppend Records, (S)how Records, (E)xit" << endl;
	cout << "Please enter your option: ";
	cin >> answer;
	

	while (answer != 'e' && answer != 'E')
	{
			switch (answer)
			{
			case 'A':
			case 'a':
				writeData();
				break;
			case 'S':
			case 's':
				readData();
				break;
			case 'E':
			case 'e':
				cout << "You chose to exit the program!" << endl;
				break;
			default:
				cout << "Invalid option - please choose a valid option";
				
			}

	}	

}//end menu

void writeData(void){
//Write the Address Info to a file
	string outFileBuffer;
	ofstream outFile(FileName, ios::app);
	char answer = 'n';
	string name = "";
	string street = "";
	string city = "";
	string state = "";
	string zipcode = "";

if (outFile.is_open())
{
	do{
		cin.ignore(0);
	
		cout << "Enter you Name: ";
		getline(cin, name);
		cout << "Enter your Street: ";
		getline(cin, street);
		cout << "Enter your City: ";
		getline(cin, city);
		cout << "Enter your State: ";
		getline(cin, state);
		cout << "Enter your Zipcode: ";
		getline(cin, zipcode);

		outFile << name << "," << street
			<< "," << city << "," << state 
			<< "," << zipcode << endl;
	
	
	
		cout << "Do you want to enter Another Record? (Y/N):";
		cin >> answer;
	}while (answer == 'y' && answer == 'Y');







}


}//end write data

void readData(void){
//read data from a file
//use the split function to break a
//deliminated line of text into fields

}//end read data

string * split(string theLine, char theDeliminator){
		//Break theline into fields and save the fields to an array.
		//Each field will occupy one element in a character array.
		//theLine is a string with fields separated with theDeliminator character.
		//Assumes the last field in the string is terminated with a newline.
		//Useage: string *theFields = split(lineBuffer, ',');

		//determine how many splits there will be so we can size our array
		int splitCount = 0;
		for(int i = 0; i < (int)theLine.size(); i++){
				if (theLine[i] == theDeliminator)
						 splitCount++;
		}
		splitCount++; //add one more to the count because there is not an ending comma

		//create an array to hold the fields
		string* theFieldArray;
		theFieldArray = new string[splitCount];

		//split the string into seperate fields
		string theField = "";
		int commaCount = 0;

		for(int i = 0; i < (int)theLine.size(); i++){ //read each character and look for the deliminator
			   if (theLine[i] != theDeliminator) {
					   theField += theLine[i]; //build the field
			   }
			  else { //the deliminator was hit so save to the field to the array
					   theFieldArray[commaCount] = theField; //save the field to the array
					   theField = "";
					   commaCount++;
			  }
		}
		theFieldArray[commaCount] = theField; //the last field is not marked with a comma...

		return theFieldArray;
} //end split




Was This Post Helpful? 0
  • +
  • -

#12 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Function writeData

Posted 17 June 2009 - 02:01 PM

Topics merged -- please do not create duplicate topics.
Was This Post Helpful? 0
  • +
  • -

#13 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Function writeData

Posted 17 June 2009 - 02:31 PM

your && is wrong, it needs to be ||,
your sayint that your single input needs to equal,
'e' and 'E', which is impossible.
you dont need a while loop either, use an if else,
you also have conditions for 'E' in a while loop
that only executes if the input is not equal to E,
so thats pointless.
here:
if (answer != 'e' || answer != 'E')
	{
			switch (answer)
			{
			case 'A':
			case 'a':
				writeData();
				break;
			case 'S':
			case 's':
				readData();
				break;
			default:
				cout << "Invalid option - please choose a valid option";
				
			}

	}	
else
{
cout<<"Exiting Program"<<endl;
return; // or exit(1);
}


This post has been edited by ImaSexy: 17 June 2009 - 02:32 PM

Was This Post Helpful? 0
  • +
  • -

#14 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Function writeData

Posted 17 June 2009 - 07:03 PM

View PostImaSexy, on 17 Jun, 2009 - 04:31 PM, said:

your && is wrong, it needs to be ||,
your sayint that your single input needs to equal,
'e' and 'E', which is impossible.
you dont need a while loop either, use an if else,
you also have conditions for 'E' in a while loop
that only executes if the input is not equal to E,
so thats pointless.
here:
if (answer != 'e' || answer != 'E')  // this will ALWAYS be true



No, || is wrong and && is correct. He's saying that the input must NOT equal 'e' AND NOT equal 'E'. If it equals either of those chars, he wants to exit.

( answer != 'e' || answer != 'E' ) is always true no matter what the input is, because ANY input is either not an 'e' or it's not an 'E'. There is no possible input that is BOTH an 'e' and and 'E' at the same time.
Was This Post Helpful? 0
  • +
  • -

#15 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Function writeData

Posted 18 June 2009 - 04:35 AM

My apologies on behalf of the DIC people who gave you all that bad advice about AND and OR because they weren't reading your code and seeing the NOT.

I hope you are clear on that part now (you and r.stiltskin are right they are wrong (although well-intentioned, I'm sure).

Now to your question here:

View Postgork4life, on 17 Jun, 2009 - 12:43 PM, said:

I'm having a hard time figuring out why writeData is outputing the prompt for a name and the prompt for the street in my code.


I don't understand what you are asking.
Could you make it a little clearer for us.
Perhaps some example of the undesired output would help us understand.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2