While Loop Repeats 4 Times - c++

I'm trying to only accept integers as input and loop through it at the same time as a sort of validation. The problem is, when a user enters something along the lines of "Two" the while loop goes through the input 3 different times before asking the user again. Is there a way to prevent this from happening and just skip the 3 iterations?
cout << "Enter Student ID: ";
while(!(cin >> id))
{
cout << "\nERROR: Please enter a Positive Whole Number" << endl;
cin.clear();
cin.ignore ();
cout << "Enter Student ID: ";
}

You can use this.
cin.ignore (std::numeric_limits<streamsize>::max (), '\n') ;
It would skip all the lines and you wont get extra loop iterations, no matter how many invalid characters you enter.

The reason for your while loop iterating is that the stream is not being extracted completely so by just using this line
cin.ignore (std::numeric_limits<streamsize>::max (), '\n') ;
the loop will iterate only for once because no matter how much big input you give it will be extracted. Cin.ignore() only removes the one character from the stream.
In your case if you enter "two" then after firs iteration only 't' will be extracted and the input would be "wo" for the second iteration. But by adding this line stream would be empty in the second iteration and will take input from user.

Related

Avoid bad user input (string when what's asked is an integer)

I have an infinite while loop, where the user is asked for a number.
My problem is very simple: If the input is a string, I want to re-ask user for input, outputting the message "Enter a valid choice: ".
I searched, and looks like I should check for cin.fail(), then call cin.clear() and cin.ignore().
Here's my code:
int main() {
int choice;
bool failed = false;
while (true) {
if (failed) cout << "Enter a valid choice: ";
else cout << "Enter a number: ";
cin >> choice;
if (cin.fail()) {
cin.clear();
cin.ignore();
failed = true;
}
}
return 0;
}
However, this doesn't really fix my problem. Of course, it isn't printing infinitely, but for each letter extra letter , it prints another "Enter a valid choice:"
Seems like I need to call cin.ignore() for each extra letter.
Any other way of doing this?
You have an infinite loop because you are not breaking the loop even when valid input is enter. Is that what you really want? If so, at the least, you are not resetting the failed flag in valid input.
More importantly, when invalid input is entered, you are not ignoring everything that was enteted, you are only ignoring 1 char at a time. That is why you see extra prompts.
Try this instead :
int main() {
int choice;
while (true) {
cout << "Enter a number: ";
while (!(cin >> choice)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Enter a valid choice: ";
}
}
return 0;
}
The reason it is printing so many times is because you are only clearing the state of cin, but aren't clearing the input buffer. You can do so in multiple ways:-
Use fflush(stdin) to clear the input buffer.This is the C method and can be done by including cstdio header.
Use the cin.ignore to ignore all characters in the current input stream. You can do this by replacing the line cin.ignore() which ignores a single character by this code cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') which ignores the entire line. For this you need the limits header.
Finally you can do the same with a simple loop like while (cin.get() != '\n'){continue;} which ignores all characters till new line.
Also another approach to the same problem is to take the input in form of a string and use the strtol() or the isdigit() functions to check if the input is valid.
By the way the infinite loop is because you have not used the break statement to terminate the loop. So you can avoid this by adding
if(!failed)
break;
Also you need to change the state of Failed at the entry of each loop by adding
failed=false;
at the start of the loop body.

Loop repeats indefinitely after break (c++)

My problem here is when the loop is broken after the user indicates they're done, the program then begins to print the last input infinitely. What's going on here? I've tried using break, return 0, setting loop = a, all results in the same thing. On a program earlier I used a for loop (used break) and it terminated just fine.
Note: This post is NOT up for syntax corrections, this is just a fragment, and the code runs fine until I terminate the loop. I'm wanting to know what's causing this to happen with a while-loop specifically.
Edit: Nevermind, figured out what was going on. When attempting to end the loop there was a datatype error (as you can see, we only receive input for two variables, both of which are ints), which caused the loop to the start repeating the last valid datatype entered. So my question is why does this happen with a data type error?
`
while (loop != "a")
{
cout << "Enter the first number: ";
cin >> input1;
cout << "\nEnter the second number: ";
cin >> input2;
cout << input1 << '\t' << input2 << '\n';
if (loop == "|")
{
break;
}
}`
Change the value of loop with in the block of while loop according to your condition if loop=="a" or loop =="|" then while loop will be terminated.
In the given code of you the value of loop is not changing therefore while loop will run infinite.
I hope you can understand.
It might be because you entered a wrong data type into 'cin'. This makes cin fail and it repeats the last acceptable value in the command prompt. Nothing to do with the loop. If you try it outside of the loop you'll see the same thing happen.
It's good practice to do the following:
while (!(cin >> input))
{
cin.clear();
cin.ignore(256, '\n');
cout << "Bad input. Try again: ";
}
so if cin fails - it will clear the fail status, ignore the last input and ask the user to enter his input again.
Hope this helps

Catch cin exception

I want to ask the user for input, which I get with cin like this
void AskForGroundstate() {
cout << "Please enter an groundstate potential value in Volt:" << endl;
if (!(cin >> _VGroundstate)) {
cin.clear();
cin.ignore();
cout << "Groundstate potential not valid." << endl;
AskForGroundstate();
}
}
_VGroundstate is a double, so if the user enters an String with not numbers, it should ask him again for a better input. But the problem is, that when the input is for example "AA", than the program executes AskForGroundstate two times, with "AAA" three times etc. Did I use the clear wrong?
The problem is that cin.ignore() drops one character; you want to drop all characters to end of line:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
This ensures that all invalid input is dropped before end-users are prompted for input again.

Why it repeats 5 times?

void firstSentence(void){
string incorrectSentence;
string correctSentence = "I have bought a new car";
cout << "Your sentence is: I have buy a new car" << endl;
cout << "Try to correct it: ";
cin >> incorrectSentence;
if(incorrectSentence == correctSentence){
cout << "Goosh. Your great. You've done it perfectly.";
}
else{
firstSentence();
}
}
That's my function I am trying to call in my program. But I am stuck little and angry because I can't find solution on my own. What it does is, that if the condition in "if statement" is true, my output is not what I expected. Output is 5 times repeated "Try to correct it. Your sentence is: I have buy a new car ..
Why it repeats exactly 5 times and whatever, what's going on there and why it's not working?
This:
cin >> incorrectSentence;
does not read a line but a whitespace-delimited token. If your input is the correct sentence, that means that the first time it will read "I", while the rest of the sentence remains in the input stream. The program correctly determines that "I" is not the same as "I have bought a new car", loops, and reads "have" the second time around. This is also not the same as the correct sentence, so it loops again and reads "bought". This continues until everything is read from the stream, at which point cin >> incorrectSentence; blocks again .
The solution is to use
getline(cin, incorrectSentence);
...which reads a line.

Catch ios::failure keeps looping

The code below should check every input once and display "Not a number" whenever the input is not a number.
int input;
while (1 == 1){
cout << "Enter a number: ";
try{
cin.exceptions(istream::failbit);
cin >> input;
}catch(ios::failure){
cout << "Not a number\n";
input = 0;
}
}
The problem is that when the catch is called (when it is not a number) it displays "Invalid number" endlessly like if the while() loop was executed several times but without asking for any new input.
while(true) or while(1) [or for(;;)] are customary ways to make a "forever loop".
You need to "clean up" the input that isn't acceptable within the cin stream.
The typical approach is to call cin.ignore(1000, '\n'); which will ignore all input until the next newline [up to 1000 characters - you can choose a bigger number, but usually a 1000 is "enough to get to a newline].
You will almost certainly also (thanks Potatoswatter) need to call cin.clear(); on the input, to remove the failed state, so that next input can succeed. [And cin.ignore() is further input, so it needs to go before that - just to be clear].
Though you failed to extract characters from the stream into an int, those characters remain in the stream so that you can attempt to extract them as something else, instead.
To skip them entirely, run std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); inside your catch block.
Then, whatever the user enters next will be the first thing in the stream. And perhaps that'll be a number, so your next attempt to extract into an int succeeds.
Well yeah. Your try-catch statement is inside the loop. So you try something, it fails and throws an exception, then you catch the exception, and you never exit or return from the loop so you do the same thing all over again.
But since your input wasn't processed the first time (throwing an exception instead), it's not going to be processed the second time, or the third time, or any time.
To advance, handle the exception by ignoring the input until the next space:
int input;
while (1 == 1){
cout << "Enter a number: ";
try{
cin.exceptions(istream::failbit);
cin >> input;
}catch(ios::failure){
cout << "Not a number\n";
input = 0;
//the line below ignores all characters in cin until the space (' ')
//(up to 256 characters are ignored, make this number as large as necessary
cin.ignore(256, ' ');
}
}
By the way, as a general rule: exceptions should be for something that is truly exceptional, particularly since there is overhead for handling the exception. There is debate about whether invalid user input is exceptional.
As an alternative, you can make a much more compact, equally correct loop without exceptions like the following:
int input;
while (true){ //outer while loop that repeats forever. Same as "while(1 == 1)"
cout << "Enter a number: ";
//The following loop just keeps repeating until a valid value is entered.
//The condition (cin >> input) is false if e.g. the value is a character,
//or it is too long to fit inside an int.
while(!(cin >> input)) {
cout << "Not a number" << endl;
input = 0;
}
}