The last time I was making a program, I wanted to input from user a boolean answer (to save space ;)) An input of 0/1 works good (0-false) and (1-true), but if the user inputs the boolean values as "true" and "false", it comes out to be an error.
So, are there any ways of doing so.?
I believe, that the strings "true" and "false" are defines using #define in the header files because of which the input is creating an error. Am i correct.?
techie1991
26
Junior Poster in Training
Recommended Answers
Jump to PostThose #define true 1 and #define false 0 are just preprocessor macros which will find and replace all true and false and replace them with 1 and 0 respectively.
Jump to Postboolalpha and noboolalpha work for cin too:
#include <iostream> using namespace std; int main() { bool awesome = false; cout << "Are you awesome? "; if (!(cin >> boolalpha >> awesome)) { cin.clear(); cin >> noboolalpha >> awesome; } if (awesome) cout << "You *are* awesome!" << …
All 5 Replies
gerard4143
371
Nearly a Posting Maven
rpdm
4
Newbie Poster
techie1991
26
Junior Poster in Training
rpdm
4
Newbie Poster
techie1991
26
Junior Poster in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.