Handle Boolean type input

If i need to check user input of the boolean type input, what is the method to handle it, for other data type i know that infinite loop can be use, but boolean just make me confuse ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;
int main()
{
bool i1;
while(true)
  {
   cin>>i1;
   if(i1==1||i1==0)
	break;
  else
	cout<<"Please enter a proper condition(1 to ON/0 to OFF)"<<endl;
  }

return 0;
}

here is my code, when the input or not 1 or 0, it cannot prompt user to to input the i1 again, program just infinite cout the statement(else part), how to handle this input...
Last edited on
with bool, 0 is false, anything else will be interpreted as true.
I put an output statement into your code below to illustrate - if I enter -5, it is interpreted as true or 1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;
int main()
{
    bool i1;
    while(true)
    {
        cin>>i1;
        if(i1==1||i1==0)
        {
            cout << "i1 = " << i1 << endl;
            break;
        }
        else
	        cout<<"Please enter a proper condition(1 to ON/0 to OFF)"<<endl;
    }

    return 0;
-5
i1 = 1
so its that possible to handle it?? because when i compile in my visual studio 2012, if i INPUT the value other than 0 or 1, it just keep on infinite loop and print Please enter a proper condition without asking user to enter input again
I'm not sure why VS is not picking up 1 or 0 as true or false for the bool variable. If I run your code through the C++ shell attached to this site (click the little gear icon at the top right of the code in the post), I don't even get the "Please enter a proper condition".. message if I enter something other than 1 or 0.

If you really wanted to reject integer values other than 1 or 0, I might define i1 as an int instead of a bool.

my lecturer has request me to use the boolean type...so i have no choice...
Does the assignment just require that the variable be a bool type, or does it require that they only enter 1 to mean true?


I can't reproduce the infinite loop, but sometimes if the program is not stopping to prompt for input, there's a newline stuck in the buffer which can be taken care of with the cin.ignore() function. Or the input is in a fail state because the type entered doesn't match the variable type, cin.clear() will reset the fail flag.
Not positive, but I believe an old version of VC++ typedefs a bool to be equivalent to an int or something of that nature, yeah it's weird. Not sure how it is now.
the mistake is in the input: get input as a string (using getline()), then validate it as a name for true or false. That can be as simple as checking that the string is equal to one of "0" and "1".

Hope this helps.
the mistake is in the input: get input as a string (using getline()), then validate it as a name for true or false.
Isn't that effectively the same as doing std::cin >> std::boolalpha >> input;
Yes, with caveats.

Google around boolalpha facets for more.
Topic archived. No new replies allowed.