cout<<"Menu:"<<endl;
cout<<" 1 - Clear Order Data"<<endl;
cout<<" 2 - Order Ice Cream Sundaes"<<endl;
cout<<" 3 - Order Ice Cream Cones"<<endl;
cout<<" 4 - Order Milk Shakes"<<endl;
cout<<" 5 - Generate Receipt for Order"<<endl;
cout<<" 6 - Exit Program (generate inventory data)"<<endl<<endl;
cout<<"Select ===> ";
cin>>menuSel;
cin.ignore(256,'\n');
while(!cin||menuSel<0||menuSel>=7)
{
cout<<"invalid menu selection, please try again";
cin.clear();
cin>>menuSel;
cin.ignore(256,'\n');
}
switch(menuSel)
{
case 1: ClearOrder(totalSundaes, totalICCones, totalShakes);
break;
case 2: SundaeOrder(totalSundaes);
break;
case 3: ICConesOrder(totalICCones);
break;
case 4: ShakesOrder(totalShakes);
break;
case 5: GenerateOrder(totalSundaes, totalICCones, totalShakes, fout);
break;
case 6: ExitProgram(fin,fout);
break;
}
return 0;
}
void ClearOrder(int & totalSundaes, int & totalICCones, int & totalShakes)
//clears totals of customer order
//PreCondidtions, none
//PostConditions, depending on user input data is cleared
{
char userAns;
cout<<"Are you sure you would like to start the order over? (Y/N) : ";
cin>>userAns;
cin.ignore(256,'\n');
toupper(userAns);
if(!cin || userAns !='Y' || userAns !='N')
{
cout<<"invalid input, please try again: ";
cin.clear();
cin>>userAns;
}
else if (userAns=='Y')
{
totalSundaes=0;
totalICCones=0;
totalShakes=0;
}
return;
15 Replies - 1018 Views - Last Post: 01 May 2012 - 11:33 PM
#1
program terminates after first menu selection/infinite loops
Posted 30 April 2012 - 09:24 PM
after i select one of the menu options, and provide input the program terminates without giving me the option to choose from the menu again. in addition my ClearOrder function has an infinite loop that i can't seem to fix. this is not all the code
Replies To: program terminates after first menu selection/infinite loops
#2
Re: program terminates after first menu selection/infinite loops
Posted 30 April 2012 - 09:34 PM
If you want the option to make another menu selection, the entire menu-selection section of your program should be inside a loop.
Your ClearOrder function has no loop. However, this expression
will always be true. userAns can't be Y and N at the same time, so it will always be either "not Y" or "not N". Since it's an "OR" expression, if any one of its parts is true, the entire compound expression is true.
You want
and I don't know why you have !cin in any of your while or if statements. Can you explain why that is there?
Your ClearOrder function has no loop. However, this expression
(!cin || userAns !='Y' || userAns !='N')
will always be true. userAns can't be Y and N at the same time, so it will always be either "not Y" or "not N". Since it's an "OR" expression, if any one of its parts is true, the entire compound expression is true.
You want
(userAns !='Y' && userAns !='N')
and I don't know why you have !cin in any of your while or if statements. Can you explain why that is there?
This post has been edited by r.stiltskin: 30 April 2012 - 09:35 PM
#3
Re: program terminates after first menu selection/infinite loops
Posted 30 April 2012 - 09:43 PM
im pretty new to C++ (it's my first semester) but I put !cin so the loop (which is there, i must have copied it wrong) would run if cin was in failure
#4
Re: program terminates after first menu selection/infinite loops
Posted 30 April 2012 - 09:44 PM
Why would cin be in failure?
#5
Re: program terminates after first menu selection/infinite loops
Posted 30 April 2012 - 09:52 PM
I'm not saying that it can never happen -- for example it can happen if the user enters Ctrl-Z in Windows or Ctrl-D in Linux -- but I was wondering why you would be concerned about that in this program.
#6
Re: program terminates after first menu selection/infinite loops
Posted 01 May 2012 - 12:09 PM
thank you for the help, i fixed the infinite loop, but now it wont display the error message when there is invalid input... its only suppose to take t and n but it takes everything
void ClearOrder(int & totalSundaes, int & totalICCones, int & totalShakes)
//clears totals of customer order
//PreCondidtions, none
//PostConditions, depending on user input data is cleared
{
char userAns;
cout<<"Are you sure you would like to start the order over? (Y/N) : ";
cin>>userAns;
cin.ignore(256,'\n');
toupper(userAns);
if(userAns !='Y' && userAns !='N')
{
cout<<"invalid input, please try again: ";
cin.clear();
cin>>userAns;
}
else if (userAns=='Y')
{
totalSundaes=0;
totalICCones=0;
totalShakes=0;
}
return;
}
#7
Re: program terminates after first menu selection/infinite loops
Posted 01 May 2012 - 01:36 PM
toupper doesn't change its parameter. It returns uppercase casted to int.
(it actually wants the parameter as int)
(it actually wants the parameter as int)
#8
Re: program terminates after first menu selection/infinite loops
Posted 01 May 2012 - 02:46 PM
I finally got ClearOrder to work, and have started to test my other functions...this next one works as far as i know...the error message works with negative numbers but when I enter a letter such as 'g', the program goes nuts and endlessly prints the menu. thank you to all that have helped so far.
int SundaeOrder(int & totalSundaes)
//Gets a int value from the user
//PreCondition, none
//PostCondition, an int value totalSundaes is returned to main
{
int numSundaes;
cout<<"Number of ice cream sundaes you would like: ";
cin>>numSundaes;
cin.ignore(256,'\n');
if(numSundaes<0)
{
cout<<"***Error: Invalid ice cream sundaes. Please re-enter."<<endl<<endl;
cin.clear();
cout<<"Number of ice cream sundaes you would like: ";
cin>>numSundaes;
}
else if(numSundaes>0)
{
totalSundaes=totalSundaes+numSundaes;
}
return totalSundaes;
}
#9
Re: program terminates after first menu selection/infinite loops
Posted 01 May 2012 - 04:27 PM
Although this is not what you asked, your function has a big error. If the user entered a negative value a second time, your program will happily accept it, and do nothing !! It will return the same value that you passed to it as a argument, making the function useless. You should change the code to :
As far as your problem is concerned, I use VC++ 2010 and the programs I make terminate automatically if I try to enter a character instead of a number...But if you want to receive only inputs which are less than 10, here is a (rather bad) way :
Please note that the method given above only works for single digit numbers, and will recognize 1,11,132 all as 1. So be careful. I have used fflush(stdin) to clear the input stream so that if a user enters 123, 23 is not left in the input stream.
while(numSundaes>0)
{
cout<<"***Error: Invalid ice cream sundaes. Please re-enter."<<endl<<endl;
cin.clear();
cout<<"Number of ice cream sundaes you would like: ";
cin>>numSundaes;
}
totalSundaes=totalSundaes+numSundaes;
As far as your problem is concerned, I use VC++ 2010 and the programs I make terminate automatically if I try to enter a character instead of a number...But if you want to receive only inputs which are less than 10, here is a (rather bad) way :
int SundaeOrder(int & totalSundaes)
{
char numSundaes;
cout<<"Number of ice cream sundaes you would like: ";
numSundaes=getchar();
fflush(stdin);
if(numSundaes<'0' || numSundays>'9')
{
cout<<"***Error: Invalid ice cream sundaes. Please re-enter."<<endl<<endl;
cout<<"Number of ice cream sundaes you would like: ";
numSundaes=getchar();
fflush(stdin);
}
totalSundaes=totalSundaes+(numSundaes-48);
return totalSundaes;
}
Please note that the method given above only works for single digit numbers, and will recognize 1,11,132 all as 1. So be careful. I have used fflush(stdin) to clear the input stream so that if a user enters 123, 23 is not left in the input stream.
#10
Re: program terminates after first menu selection/infinite loops
Posted 01 May 2012 - 04:59 PM
do you know how to make it display the error message if a character is entered? the program isnt suppose to terminate...as for making numSundaes a character i dont think that would work since i have to keep a running total....but i could be wrong
#11
Re: program terminates after first menu selection/infinite loops
Posted 01 May 2012 - 05:43 PM
I would appreciate it if you could at least read what I wrote... And by the way, the code I gave you will display an error message if a character is entered, not exit. Besides, I have declared numSundaes to be a char, but I have later checked it if it is numeric or not. If it is not, it gives an error, and if it is, it converts the char to and int. Although this is useful only for inputs less than 10, it works fine 
So, here is a working code if you want. Try it and then give feedback !!
So, here is a working code if you want. Try it and then give feedback !!
int SundaeOrder(int & totalSundaes)
{
char numSundaes;
cout<<"Number of ice cream sundaes you would like: ";
numSundaes=getchar();
fflush(stdin);
while(numSundaes<'0' || numSundays>'9')
{
cout<<"***Error: Invalid ice cream sundaes. Please re-enter."<<endl<<endl;
cout<<"Number of ice cream sundaes you would like: ";
numSundaes=getchar();
fflush(stdin);
}
totalSundaes=totalSundaes+(numSundaes-48);
return totalSundaes;
}
This post has been edited by aresh: 01 May 2012 - 05:45 PM
#12
Re: program terminates after first menu selection/infinite loops
Posted 01 May 2012 - 06:34 PM
aresh, on 01 May 2012 - 07:27 PM, said:
I have used fflush(stdin) to clear the input stream so that if a user enters 123, 23 is not left in the input stream.
You should not use fflush(stdin). Never.
fflush is intended to be used on output streams only. Its use with an input stream gives undefined behavior.
If there's an unwanted entry it would be better to simply wait until the next time input is requested and then rely on your input validation code to deal with it.
#13
Re: program terminates after first menu selection/infinite loops
Posted 01 May 2012 - 09:38 PM
Fine. Can you give an example on how to make the same function (SundaeOrder) without fflush ? Because I did not find any other way to produce the same effect !!
#14
Re: program terminates after first menu selection/infinite loops
Posted 01 May 2012 - 09:59 PM
First of all I wouldn't mix C and C++ i/o methods in my program. If you're writing C++, stick to C++, so don't use getchar().
Instead of extracting a single number from the input, I would declare a string to use as an input buffer -- a C++ string so I don't have to worry about the size. Then I would use getline to read an entire line of input, so I'm clearing out everything that the user enters whether it's valid input or not. e.g.:
Next you can do whatever is necessary to validate the input. For example you could check the buffer to see if the first char is a digit, and if it is, use atoi to extract an int
still using the same while(numSundaes < 0 || numSundays > 9) code to validate the amount
and if there's anything else in the buffer after that int just ignore it.
Or you might decide to check the entire buffer and reject it if there's anything invalid in it at all.
Basically, what you do depends on how fussy you want to be. But by reading the entire line into the buffer, you can be sure there's no garbage left in the stream, and you can also make sure that your program won't crash due to invalid input because the string buffer can accept any kind of input and the program can test it thoroughly before trying to use it.
Edit: Just noticed that you had char values '0' and '9' in that while statement. Changed them to int.
Instead of extracting a single number from the input, I would declare a string to use as an input buffer -- a C++ string so I don't have to worry about the size. Then I would use getline to read an entire line of input, so I'm clearing out everything that the user enters whether it's valid input or not. e.g.:
string buffer; getline(buffer, cin);
Next you can do whatever is necessary to validate the input. For example you could check the buffer to see if the first char is a digit, and if it is, use atoi to extract an int
if(isdigit(buffer[0]) {
numSundaes = atoi(buffer.c_str());
still using the same while(numSundaes < 0 || numSundays > 9) code to validate the amount
and if there's anything else in the buffer after that int just ignore it.
Or you might decide to check the entire buffer and reject it if there's anything invalid in it at all.
Basically, what you do depends on how fussy you want to be. But by reading the entire line into the buffer, you can be sure there's no garbage left in the stream, and you can also make sure that your program won't crash due to invalid input because the string buffer can accept any kind of input and the program can test it thoroughly before trying to use it.
Edit: Just noticed that you had char values '0' and '9' in that while statement. Changed them to int.
This post has been edited by r.stiltskin: 01 May 2012 - 10:02 PM
#15
Re: program terminates after first menu selection/infinite loops
Posted 01 May 2012 - 10:54 PM

New Topic/Question
This topic is locked


MultiQuote


|