How do I clear recent screen output instead of having to CLS everything and outputting the whole screen again?

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:


Example of what I am doing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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?
Last edited on
DO NOT use goto.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
	int option = 0;

	    cout<<"Enter a choice: "<<endl;
	    cout<<"1. Chocolate"<<endl;
	    cout<<"2. Vanilla"<<endl;
	    cout<<"3. Strawberry"<<endl;
	    cout<<"4. Banana"<<endl;
	    cin>>option; //get input

	while(option<1 || option>4)
	{
                system("clear"); //removes previous
		cout<<"ERROR. Enter 1-4: "<<endl;
		cout<<"1. Chocolate"<<endl;
		cout<<"2. Vanilla"<<endl;
		cout<<"3. Strawberry"<<endl;
		cout<<"4. Banana"<<endl;
		cin>>option;
            }
	}
}


Problem with this code is that if the user inputs a character, the program will break.
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().
Last edited on
What's wrong with outputting everything again?
Is my current method as bad as gotos ?


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
Are you sure your professor meant the goto language construct and not the goto character position function?
http://www.google.com/search?btnI=1&q=msdn+SetConsoleCursorPosition

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.

Good luck.
Thank you, yes now that you mention it he did say something about gotoxy, something similar to that.
The gotoxy() function is found in <conio.h>. If you are using an old Borland C++, you can #include it and use it.
Topic archived. No new replies allowed.