Need help looping this switch statement

This is a discussion on Need help looping this switch statement within the C++ Programming forums, part of the General Programming Boards category; Hey guys I'm wondering how to loop the default at the end of the switch. I'm doing this so it ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    120

    Need help looping this switch statement

    Hey guys I'm wondering how to loop the default at the end of the switch. I'm doing this so it will obviously have me go back and answer with 1, 2 or 3 again thanks.

    Code:
    #include <iostream>
                  using namespace std;
    
    
                 int main()
            {
                   int myAnswer, i;
    
    
                  cout << "In what country is Wagga Wagga?" << endl;
                  cout << "1: Japan" << endl;
                  cout << "2: Australia" << endl; 
                  cout << "3: Israel" << endl;
                  cin >> myAnswer;
                
                 switch (myAnswer)
                      {
                         case 1:
                         cout << "Incorrect" << endl;
                        break;
                    
                    case 2:
                   cout << "Correct" <<endl;
                   break;
                   
                   case 3:
                   cout << "Incorrect" << endl;
                  break;
                  default:
                   cout << "Please enter 1 2 or 3 for your answer" << endl;
    
    } 
     
     system ("pause");
     
     return 0;
     
    }
    please excuse how messy it looks but im in a hurry thanks

  2. #2
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    Code:
    do
    {
        /* something over and over */
    }while(  /* user input is not 1,2,or 3 */ );
    would be one approach
    Asking a question you already know the answer to might teach you something you did not know...

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Quote Originally Posted by QuestionKing View Post
    Code:
    do
    {
        /* something over and over */
    }while(  /* user input is not 1,2,or 3 */ );
    would be one approach
    No matter how many different variations of that i try to use, it won't work. Any other/more advice?

    BTW- I'm not doing this for class or anything like that, actually doing it for someone else, so feel free to post the code if you want. thanks guys

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by nick753 View Post
    No matter how many different variations of that i try to use, it won't work. Any other/more advice?

    BTW- I'm not doing this for class or anything like that, actually doing it for someone else, so feel free to post the code if you want. thanks guys
    If you want more specific help you need to give more specific information about the problem. For example, give in code what one of the variations you tried that didn't work and what about it didn't (work)?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Code:
    bool done = false;
    
    while(!done)
    {
          cout << "In what country is Wagga Wagga?" << endl;
          cout << "1: Japan" << endl;
          cout << "2: Australia" << endl; 
          cout << "3: Israel" << endl;
          cin >> myAnswer;
                
          switch (myAnswer)
          {
                case 1:
                    cout << "Incorrect" << endl;
                    done = true;
                    break;
                    
                case 2:
                    cout << "Correct" <<endl;
                    done = true;
                    break;
                   
                case 3:
                    cout << "Incorrect" << endl;
                    done = true;     
                    break;
                
                default:
                    cout << "Please enter 1 2 or 3 for your answer" << endl;
    
    }
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Thanks x4xlds, Can you please explain a little about what done and (!done) and the done = true means? I'd appreciate it. thanks

  7. #7
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Saying !done is the same as saying while(done == false) . You set done to true when you have met the requirements to end the loop, in your case a valid answer of 1,2 or 3.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Quote Originally Posted by xds4lx View Post
    Saying !done is the same as saying while(done == false) . You set done to true when you have met the requirements to end the loop, in your case a valid answer of 1,2 or 3.
    Hmm okay thanks.. got any good sites to look up the done statement on?

  9. #9
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    google c++ not operator
    Asking a question you already know the answer to might teach you something you did not know...

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Quote Originally Posted by QuestionKing View Post
    google c++ not operator
    Are you talking about the (!done)? I know what that means i just don't know what the done = false and done = true means

  11. #11
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    Code:
    done = false;  //  sets the boolean value of the variable done equal to false
    
    done = true;  //  sets the boolean value of the variable done eaual to true
    google boolean
    Asking a question you already know the answer to might teach you something you did not know...

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    19
    True and false can also be thought as 1 (for true) and 0 for false.

    The basic boolean operators are AND (&&), OR (||), NOT (!). Consider the following truth-tables:
    (C is the result of the operation).
    AND:
    A 0 1 0 1
    B 0 0 1 1
    C 0 0 0 1

    OR:
    A 0 1 0 1
    B 0 0 1 1
    C 0 1 1 1

    NOT:
    A 0 1
    C 1 0

    Now, say you have two 8-bit numbers:

    1010 1010 and
    0101 0101

    ANDing them together is very straight forward. 0 && 1 = 0, 1 && 1 = 1.
    That gives:

    0000 0000 as the result.
    ORing them gives 1111 1111.

    It's also very important to know that every conditional expression IS evaluated to either True or False.

    Consider this:

    int i = 7;
    int a = 5;
    while(i=>a)
    {
    }

    The result of the "i=>a" will be evaluated, and either the expression is True, or it is False. It can't be "half true".

  13. #13
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Thanks guys. I knew most of that but just didn't understand the done statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch Statement Errors
    By melodia in forum C Programming
    Replies: 7
    Last Post: 10-25-2009, 04:51 PM
  2. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 04:19 AM
  3. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 03:14 PM
  4. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 04:05 PM
  5. switch statement issues...(undeclared identifiers)
    By mero24 in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2005, 08:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21