Thread: 1 or 0 == true or false?

  1. #1
    Registered User
    Join Date
    Aug 2005

    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.


    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:
       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:

    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.

    cout<<"\nPress 'e' then 'enter' to exit program\n";
    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

    Last edited by Discolemonade; 08-13-2005 at 08:51 PM.

  2. #2
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    0 is false, nonzero is true

    i suggest you use true or false directely instead of using 1 or 0.
    such as
    flag = true

  3. #3
    Registered User
    Join Date
    Jan 2005
    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.
    #include <iostream>
    #include <iomanip>
    #include <limits>
    int main()
        bool b = true;
        std::cin >> std::boolalpha >> b;
        if (
            std::cout << "Input Failed." << std::endl;
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            if (b)
                std::cout << "TRUE!!" << std::endl;
                std::cout << "FALSE!!" << std::endl;
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

  4. #4
    Registered User
    Join Date
    Aug 2005
    Daved wrote: " 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!

  5. #5
    Registered User
    Join Date
    Nov 2003
    This Bash quote seems appropiate :O

    (morganj): 0 is false and 1 is true, correct?
    (alec_eso): 1, morganj
    (morganj): bastard.
    Rots Soft
    If the facts don't fit the theory, change the facts.
    Albert Einstein (1879 - 1955)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-lvalue error
    By zlb12 in forum C Programming
    Replies: 1
    Last Post: 04-17-2009, 10:43 AM
  2. Tic Tac Toe program...
    By Kross7 in forum C++ Programming
    Replies: 12
    Last Post: 04-12-2007, 03:25 PM
  3. process killer on a loop
    By Anddos in forum Windows Programming
    Replies: 8
    Last Post: 01-11-2006, 01:50 AM
  4. Need Help with a Bowling Score Program
    By oobootsy1 in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2005, 10:04 AM
  5. Reducing Code size from ridiculous length
    By DanFraser in forum C# Programming
    Replies: 10
    Last Post: 01-18-2005, 05:50 PM