c++ while loop keeps executing simple [duplicate] - c++

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;
}

Related

Why do I have an endless loop if first input is a letter? [duplicate]

This question already has answers here:
cin infinite loop when reading in a non-numeric value
(2 answers)
Why cin stopped working after cin>>(int) failed?
(2 answers)
Closed 9 months ago.
Hi I wish that the following code can ask for an input of 2 integers within a certain range. If the condition is not satisfied, the loop kicks in until right numbers are typed in. The problem is that if I give a letter first (e.g. "r"), I will get an endless loop immediately (the system doesn't ask for the second but instead keeps showing "something wrong, please input again"). If the second input is a letter, the program can terminate. Could you tell me where goes wrong? Thanks very much.
#include <iostream>
int main()
{
int x, y;
std::cout << "Please input 2 integers in the range: \n";
std::cin >> x >> y;
while (! ((x >= 0) && (x < 10) &&
(y >= 0) && (y < 10)) )
{
std::cout << "\nsomething wrong, please input again\n";
std::cin >> x >> y;
}
}

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++ Infinite Loop repeating [duplicate]

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

Why am I getting an Infinite loop when validating my user input? [duplicate]

This question already has answers here:
Why do I get an infinite loop if I enter a letter rather than a number? [duplicate]
(4 answers)
Closed 7 years ago.
I'm trying to validate the user inputs. If the user inputs anything but an integer it should stay in the While loop. But when I give the program a "w" for instance the program just endlessly prints "Please input integer" and I have to stop the program.
int MAns1 = 0
while (!(cin >> MAns1))
{
cout << "\nPlease Enter An Integer: ";
cin.clear();
}
Because you should be testing (cin >> MAns1), not !(cin >> MAns1). I would do something like this though:
#include<iostream>
using std::cin;
using std::cout;
int main() {
int MAns1 = 0;
for (;;)
{
cout << "\nPlease Enter An Integer: ";
cin >> MAnsi;
// did the last read succeed?
if(!cin) {
// it did *not* succeed.
break;
}
}
}

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.