Question is pretty much everything I have to ask. The reason I am clearing screen and outputting it all again is that I don't want the ERROR message output to be piling up after the user messes up on the input. I'll show an example to help understand.
Example output of what I don't want:
1 2 3 4 5 6 7 8
Enter a choice:
1. Chocolate
2. Vanilla
3. Strawberry
4. Banana;
ERROR. Enter 1-4:
ERROR. Enter 1-4:
ERROR. Enter 1-4:
int main()
{
int option;
cout<<"Enter a choice: "<<endl;
cout<<"1. Chocolate"<<endl;
cout<<"2. Vanilla"<<endl;
cout<<"3. Strawberry"<<endl;
cout<<"4. Banana"<<endl;
cin>>option;
while(option<1 || option>4)
{
cin.clear();
cin.ignore(10000,'\n');
system("CLS");
cout<<"ERROR. Enter 1-4: "<<endl;
cout<<"1. Chocolate"<<endl;
cout<<"2. Vanilla"<<endl;
cout<<"3. Strawberry"<<endl;
cout<<"4. Banana"<<endl;
cin>>option;
}
}
A professor suggested gotos as a solution, but they seem to be the ugly duckling around here. What are my options? Do I need to change my method at all to make it more efficient? Is my current method as bad as gotos?
Problem with this code is that if the user inputs a character, the program will break.
That is why I used the cin.clear() function. Hmm did you see my code at all? You wrote what I basically did, except without the cin.clear() and cin.ignore().
No, and the only thing bad about goto is when you get them mixed up, multiple labels, multiple goto's .... ugly
But possibly one can write a code that cls at custom position so you don't reapeat the menu again, but you need to learn win32 or other console advance stuffs first
Print your menu, etc. Use GetConsoleScreenBufferInfo() to learn where the cursor is, print your question, wait for user input, if input is bad, SetConsoleCursorPosition() to where it was, write a whole bunch of spaces (as approriate), go back again and print the new prompt. http://www.google.com/search?btnI=1&q=msdn+GetConsoleScreenBufferInfo
It's a messy solution, but probably easiest for you to start.