-
1 or 0 == true or false?
I have just started learning c++. And to try out what I learned I wrote a short 10 question true or false quiz.
Problem:
1. The only way I can get the answers inputed is by using a 1 for true and a 0 for false.
How can I actually get the user to input either the string (true) or the character (t) etc.
Example of what Im using now:
Code:
cout<<"\nQuestion #1\n";
cout<<"\nThe default IRQ for com 1 is 4\n";
cin>> flag;
if (flag==1) {
cout<<"\nvery good " <<str1 <<" you are correct\n";
}
else if (flag==0) {
cout<<"\nSorry " <<str1 <<" That is incorrect";
cout<<"\nCom 1 and Com 3 both share the IRQ of 4\n";
}
---------------------------------
Here are the variables I am using:
Code:
string str1;
bool flag = 1;
char letter;
2. If you try to type anything other than a 1 or 0 the program just shoots straight to the end. I thought maybe nesting another else if would solve the problem but it doesnt.
3. I added the "Char letter" variable in order to keep the thing from disappearing as soon as you answer the last question.
Code:
cout<<"\nPress 'e' then 'enter' to exit program\n";
cin>>letter;
It works ok, and keeps the program from dissapearing too soon, but I have also read something about "cin.get()"
I tried using that in the beginning but kept getting 'Parse errors"
Cout someone tell me what a parse error is and the correct way to use the cin.get() "command?"
Thank you in advance for your help
Steve
-
0 is false, nonzero is true
i suggest you use true or false directely instead of using 1 or 0.
such as
flag = true
-
To allow the user to type in true or false, you can use the boolalpha flag from iomanip. This means that if the user types true (all lowercase) then the flag is set to true, if they type false, it is set to false, and if they type anything else (even, "TRUE" or "1"), then cin will go into an error state and your flag will not be changed.
The program will shoot to the end if cin goes into a fail state until you clear the fail flags and empty the stream of the bad input (this is what happened when in your question #2).
See the example below. It shows you how to clear the fail flags if cin fails, and how to use cin.get(). Notice I added the ignore there before the cin.get(), which is required when you use operator>> because the user types their answer and hits enter and you must ignore the <enter> they typed before asking them to hit <enter> again to finish the program. Code:
#include <iostream>
#include <iomanip>
#include <limits>
int main()
{
bool b = true;
std::cin >> std::boolalpha >> b;
if (std::cin.fail())
{
std::cout << "Input Failed." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
else
{
if (b)
std::cout << "TRUE!!" << std::endl;
else
std::cout << "FALSE!!" << std::endl;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
}
-
Daved wrote: "...you can use the boolalpha flag from iomanip..."
Ahhh... I tried the boolalpha, but this is why I had problems. I didnt include <iomanip>.
so I just relegated myself to the 1 and 0 format. Will go back and try again.
Thank you for all your help and information!
-
This Bash quote seems appropiate :O
(morganj): 0 is false and 1 is true, correct?
(alec_eso): 1, morganj
(morganj): bastard.