int main()
{
int userInput; /*i want the user input has to be between 1 - 100 and making sure that if the input number other than 1 - 100 it will give error or the repick the number. so how do i make that variable ? is it ( 1 < userInput < 100 ) */
cout << "Pick a number between 1 and 100: ";
cin >> userInput;
Repeat if:
- Not an int was inserted, OR
- an int less than 1 was inserted, OR
- an int greater than 100 was isnerted
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
usingnamespace std;
int main()
{
int userInput; /*i want the user input has to be between 1 - 100 and making sure that if the input number other than 1 - 100 it will give error or the repick the number. so how do i make that variable ? is it ( 1 < userInput < 100 ) */
do
{
cin.clear();
cout << "Pick a number between 1 and 100: ";
cin >> userInput;
} while(cin.bad() || userInput < 1 || userInput > 100);
return0;
}
Thank you, that was fast. Btw, what does cin.bad() do?
and how do you write in a format for the program in the box with
1
2
3
4
5
on the outline ?
sorry if i am asking too much. new too c++ and this forum xD
cin.bad checks if the cin operation has failed. If you do something like try to input a character while an integer is expected, this flag will be triggered.
I did do a mistake, above, I forgot to clear the cin.bad() flag with cin.clear()
TO get the number lines, you need to put code tags around your code. Put [co de] and [/co de] around your code.
pardon if am asking too much but could u explain more about the cin.bad() and cin.clear() ?
i dont know how it works. if i input a char into a variable that suppose to be an integer it will just give an infinite loop even though there is cin.bad() written.
btw, thanks for pointing out the logical OR operator to set a range to variable.
#include <iostream>
usingnamespace std;
int main()
{
int userInput; /*i want the user input has to be between 1 - 100 and
making sure that if the input number other than 1 - 100 it will give error or the
repick the number. */
do
{
if(cin.bad())
{
cin.clear();
cin.ignore();
}
cout << "Pick a number between 1 and 100: " ;
cin >> userInput;
} while( cin.bad() || (userInput < 1) || (userInput > 100));
return 0;
}
But this also result in an infinite loop when given a character.
#include <iostream>
usingnamespace std;
int main()
{
int userInput; /*i want the user input has to be between 1 - 100 and making
sure that if the input number other than 1 - 100 it will give error or the repick
the number. */
cout << "Pick a number between 1 and 100: " ;
while (true)
if ( (!(cin >> userInput)) || (userInput < 1) || (userInput > 100))
{
cout << "Pick a number between 1 and 100: " ;
cin.clear();
cin.ignore();
}
elsebreak;
return 0;
}
Can someone please help me understand why the first code result in an infinite loop on a character input?
#include <iostream>
usingnamespace std;
int main()
{
int userInput; /*i want the user input has to be between 1 - 100 and
making sure that if the input number other than 1 - 100 it will give error or the
repick the number. ) */
do
{
if(cin.fail())
{
cin.clear();
cin.ignore();
}
cout << "Pick a number between 1 and 100: " ;
cin >> userInput;
if(cin.bad())
cout <<"Bad input"<<endl;
//The cin.bad() clause does not trigger
} while( cin.fail() || (userInput < 1) || (userInput > 100));
return 0;
}
woots...many reply o0.
sometimes even though cin.bad or fail is used.
it will just give an infinite loop.
and if it does not give an infinite loop it will count the number of character of words u input or the digit. and it will do the number of loops.