Function writeDataI'm having a problem creating a menu using a switch statement
16 Replies - 1256 Views - Last Post: 18 June 2009 - 09:00 AM
#1
Function writeData
Posted 16 June 2009 - 07:22 AM
//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
Replies To: Function writeData
#2
Re: Function writeData
Posted 16 June 2009 - 07:54 AM
#3
Re: Function writeData
Posted 16 June 2009 - 01:36 PM
gork4life, on 16 Jun, 2009 - 06:22 AM, said:
//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
#4
Re: Function writeData
Posted 16 June 2009 - 01:50 PM
system()commands!!!!!
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
#5
Re: Function writeData
Posted 16 June 2009 - 07:39 PM
#6
Re: Function writeData
Posted 17 June 2009 - 05:51 AM
#7
Re: Function writeData
Posted 17 June 2009 - 07:52 AM
gork4life, on 17 Jun, 2009 - 04:51 AM, said:
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')
#8
Re: Function writeData
Posted 17 June 2009 - 10:54 AM
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.
#9
Re: Function writeData
Posted 17 June 2009 - 11:10 AM
metaphorical, on 17 Jun, 2009 - 09:54 AM, said:
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
#10
Re: Function writeData
Posted 17 June 2009 - 01:33 PM
ImaSexy, on 17 Jun, 2009 - 06:52 AM, said:
gork4life, on 17 Jun, 2009 - 04:51 AM, said:
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.
#11
Re: Function writeData
Posted 17 June 2009 - 01:43 PM
//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
#12
Re: Function writeData
Posted 17 June 2009 - 02:01 PM
#13
Re: Function writeData
Posted 17 June 2009 - 02:31 PM
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
#14
Re: Function writeData
Posted 17 June 2009 - 07:03 PM
ImaSexy, on 17 Jun, 2009 - 04:31 PM, said:
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.
#15
Re: Function writeData
Posted 18 June 2009 - 04:35 AM
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:
gork4life, on 17 Jun, 2009 - 12:43 PM, said:
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.
|
|

New Topic/Question
Reply




MultiQuote






|