Jun 2, 2020 at 9:30am UTC
I wrote a program that asks the user to enter true or false to a trivia question. I learned that standard cin just inputs 1 or 0 for true and false.
I want to be able to input the word true or false but I having problems with my boolalpha stuff:
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
bool answer;
cout << "True or False: Richard Nixon was the first U.S. President to be impeached? " << endl;
cin << boolalpha << answer; // this line is wrong
if(answer)
cout << "Sorry. That is incorrect." << endl;
else
cout << "You are correct!" << endl;
getch();
return 0;
}
Jun 2, 2020 at 10:04am UTC
I got it going now. I discovered that if you use cin.setf(ios::boolalpha) you can get the input stream to accept the words true and false.
Jun 2, 2020 at 10:06am UTC
You need to change the operators:
cin >> boolalpha >> answer; //Note: >>
Jun 2, 2020 at 1:00pm UTC
I didn't know that one.
If you want to accept something else, like "yes" / "no", and there is no built in way, just use logicals:
if(answer == "yes") //example. often you iterate uppercase/lowercase the input and check vs one upper/lower answer to match caseless.