To clear your screen you can use system("CLS") - keep in mind this makes your program less portable though.
The reason you continuously loop when you enter bad input is because you're trying to accept a number, but are receiving a char, and your input buffer is overflowing.
Try flushing the input stream after you get the data you want.
Also, I would recommend using a switch rather than multiple IFs.
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
The reason you continuously loop when you enter bad input is because you're trying to accept a number, but are receiving a char, and your input buffer is overflowing.
It's not because of an input buffer overflow. When you try to read a non-digit into an integer, the read fails. The input buffer remains the same. Next time you try to read, since that character is still there, the input fails again. And again. And ... you get the idea.
1) Test your read to see if it was successful. Don't askHow? Look it up. It's part of cin error checking.
2) If it fails, clear the input buffer asDuki's link suggests. Be sure to process the error in your code. Don't just go on your merry way and execute the code as if nothing happened.
Also, I would recommend using a switch rather than multiple IFs.
So would I, but only if you've learned them.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
And isnt there any other way to clear screen other than involving system in it. Cause i would like my program to be as independent as possible.
Output '\n' 25 to 50 times. Depending on how many lines in your console.Should i make function for exiting rather than typing it in all the time (Hardcoding is the expression i think ?)
No. Just remove the lines
cout<<"Press 1 to exit\n";
cin>>exit;
from your switch and put them once at the bottom of the loop.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
>>So the question is do operators like +,-,/,* have the same effect ?
No.
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32