C++ Infinite Loop repeating [duplicate] - c++

This question already has an answer here:
How to reset std::cin when using it?
(1 answer)
Closed 5 years ago.
int departmentNo;
bool depNumIncorrect = false;
do
{
depNumIncorrect = false;
cout << "Please enter your department number... ( 1 / 2 / 3 )" << endl;
cin >> departmentNo;
if (departmentNo < 1 || departmentNo > 3)
{
cout << "Invalid Entry." << endl;
depNumIncorrect = true;
}
} while (depNumIncorrect == true);
Whenever I input a correct input (1/2/3), the code works fine and continues without looping. Whenever I input an incorrect integer, the code loops as it is supposed to. But when I input a character or a string, it puts the code into an infinite loop. The "Please enter your department number... ( 1 / 2 / 3 )" is repeatedly output to the console.
How could I go about changing this so that it doesn't start an infinite loop when a character or string is input, but rather loops as it is supposed to and allows me to input again?

deparmentNo is (I assume) an Int and you have a problem of conversion. As mentioned by others you should clear error flag of std::cin. Moreover you probably should not cin directly into an int but rather to a string , and then attempting conversion to int. The result of conversion attempt (successful or not) would be an additional criteria to check for valid entry

Related

c++ while loop keeps executing simple [duplicate]

This question already has answers here:
Infinite loop with cin when typing string while a number is expected
(4 answers)
cin infinite loop when reading in a non-numeric value
(2 answers)
std::cin infinite loop for wrong input
(1 answer)
Closed last month.
Hello I want to simply get user input as an integer from 1- infinity, I can enter something like 0, -1, -20 and my function handles this error properly and simply asks the user to renter once. When I enter a string or character the terminal is spammed with this reprint message. How do I fix this?
int numOfUsers(){
int x = 0;
int numPlayers;
cout << "how many users want to play basketball? please enter a number above 1";
cin >> numPlayers;
while(x==0){
if (((numPlayers % 1) == 0) && (numPlayers >= 2)){
x++;
} else {
cout << "\nplease enter a valid number of players\n";
cin >> numPlayers;
}
}
return numPlayers;
}

cin in a while loop not working as expected [duplicate]

This question already has answers here:
How to reset std::cin when using it?
(1 answer)
Why would we call cin.clear() and cin.ignore() after reading input?
(4 answers)
Resetting cin stream state C++
(4 answers)
Closed last month.
I am a new cpp learner and currently trying the std iostream library. I am working on a program that takes number as input and output the number, and exit when enter 0. Also I want the program can ignore the invalid input, output an error message when encounter an invalid input and continue reading the number until I enter 0 to exit. Below is the code I wrote:
while (true) {
cout << "Please input value" << endl;
cin >> value;
if (cin.fail()) {
cerr << "Invalid input" << endl;
} else if (value == 0) {
cout << "Exit with 0";
break;
} else {
cout << "You have entered " << value << endl;
}
}
The code does not work as I expected. When I input normal number and it worked well, output each number until I entered 0:
Please input value
1
You have entered 1
Please input value
2
You have entered 2
Please input value
0
Exit with 0
However when I entered something not a number, then the cin won't work in the next while loop and repeatedly output the error message:
Please input value
a
Invalid input
Please input value
Invalid input
Please input value
Invalid input
Please input value
Invalid input
Please input value
more lines
I assume that when a cin fail, it will not read another number in the next loop. I was wondering how can I "reactivate" the cin to make it read number even it failed previously.
Any help is appreciated.

Checking if input is an integer, and inputting more than 1 character results in multiple statements being printed in console instead of just once [duplicate]

This question already has answers here:
cin input (input is an int) when I input a letter, instead of printing back incorrect once, it prints correct once then inc for the rest of the loop
(2 answers)
Closed 2 years ago.
{
valid = true; //Assume the cin will be an integer.
cin >> menuValue;
if (cin.fail()) //cin.fail() checks to see if the value in the cin
//stream is the correct type, if not it returns true,
//false otherwise.
{
cin.clear(); //This corrects the stream.
cin.ignore(); //This skips the left over stream data.
cout << "Please enter an Integer from 1-6 only." << endl;
valid = false; //The cin was not an integer so try again.
}
}
Im trying to make an error checkpoint, where is a user inputs something that isn't an integer, it'll ask them to rein put the number, the only issue is If I were to input something such as jiasdhais, it would print the same message as many times as the length of the input. Any way around this?
Try this:
{
int input;
if( !( std::cin >> input) ){
//in case of fail do stuff
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
} else {
//then check for 1-6
//your code ...
}
}
If input is not integer it will be discarded.More specifically if your architecture holds int as 4bytes then the input should be in range -2,147,483,648 to 2,147,483,647, otherwise will be discarded.

C++: Tracking iterations in code (after n iteration do x) / User input

I had a question in regards to a beginner assignment I was working on. The initial assignment requires me to make a program that asks the user to enter any number other than 5 until the user enters 5. If they enter 5 the program will alert them saying they input 5.
The next part of the assignment requires me to make a condition where after 10 iterations or 10 inputs of a non 5 value, the program messages the user and exits the program.
I finished the first part but had trouble with the second part. I searched stackoverflow and found something about the "get" function, but I'm not sure how to implement it correctly. How would I track the number of inputs or iterations and make a condition to where after n number of successful iterations the program exits?
Also , how would I make a condition to where if the user inputs a character instead of an integer the program warns the user and exits?
Thanks for the help. Here is the code I have written so far.
// This program works, however, if user inputs a character or a very large number
//then the program malfunctions.
// Learn more about the get function.
#include <iostream>
using namespace std;
int main()
{
int inpt;
cout << "Please input any number other than 5.\n";
cin >> inpt;
while (inpt != 5)
{
cout << "Please input another number other than 5.\n";
cin >> inpt;
}
if (inpt = 5)
{
cout << "Hey! You weren't supposed to enter 5!";
}
return 0;
}
you need to add a counter
int count = 0;
increment it each time round the loop
cout << "Please input another number other than 5.\n";
cin >> inpt;
count++;
and stop if the count gets too big
if(count>10) break;
you could also change your while condition
Note
if(inpt = 5) doesnt do what you think, you mean inpt == 5

Infinite loop after receiving input and testing to see if it's an int in C++ [duplicate]

This question already has answers here:
Infinite loop with cin when typing string while a number is expected
(4 answers)
Closed 6 years ago.
I'm trying to take an integer from the user. I'm using cin.ignore to make sure that the input is an int. However, when it is not an int, it causes the program to enter an infinite loop.
int steps = 0;
while (steps<2 || steps>100)
{
char tmp[1000];
cout << "Zadejte pocet cyklu: ";
cin >> steps;
cin.ignore(numeric_limits<int>::max(), '\n');
if (!cin || cin.gcount() != 1)
{
cin.getline(tmp,1000);
steps = 0;
}
}
If the input stream doesn't contain an integer when you do cin >>
steps, the stream enters an error state, which must be explicitly
cleared (cin.clear()). Until then, the error state remains, and all
further attempts to input will be treated as no-ops.