Strange bool behavior

I'm trying to use a bool operator to allow the user to choose an action. I wrote this basic program, but if any number other than 1 is entered it runs as if bool was 1 but does not allow for user entry. I know I could use an int instead of bool, but the assignment says use bool, besides why won't it work.

#include<iostream>
using namespace std;

int main ()
{

int p;
bool flag;

cout<<"Please enter 1 to double the value of your number\n";
cin>>flag;
if(flag==true)
{

cout<<"Enter your number\n";
cin>>p;


cout<<2*p<<endl;

cout<<"Thank you"<<endl;
}
else
{
cout<<"Have a pleasent day"<<endl;
}
return 0;
}


Cheers
Any non-zero number is considered true when inputting into a bool. Are you inputting zero actually or something else?
I input 1 and the program works correctly. Any numerical value other than 1 or 0 and the program asks for number entry, but does not allow input, and prints 269029718.
With 0 input it goes into the else cycle.

edited: for clarity,
Congrats on your 3600th post
Last edited on
Topic archived. No new replies allowed.