do,while problems

This is a discussion on do,while problems within the C++ Programming forums, part of the General Programming Boards category; here is my code: Code: int menu(void) { int answer = 0; do { cout << "----Menu----" << endl << ...

  1. #1
    -
    Join Date
    Feb 2003
    Posts
    47

    do,while problems

    here is my code:

    Code:
    int menu(void)
    {
        int answer = 0;
        
        do {
            cout << "----Menu----" << endl
                 << "1 Add two fractions" << endl
                 << "2 Multiply two fractions" << endl
                 << "3 Exit the program" << endl
                 << "Please select an option (1, 2, or 3): ";
            cin >> answer;
        } while (answer != 1);
        
    return answer;
    }
    for some reason when i change,
    Code:
        } while (answer != 1);
    
    // to this
    
        } while (answer != 1 || 2);
    
    // or this
    
        } while (answer != '1' || '2');
    i can not seem to get the loop to stop once the correct answer is input.

    I have looked at both the do, while and || operator and cant seem to find where the problem is because it causes no errors.
    Thanks to anyone that can help.

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    You can't say (answer == a || b).
    You have to do each one on its own:
    Code:
    while ( (answer != 1) && (answer != 2) )
    When the answer is either 1 or 2, the while loop will end.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    -
    Join Date
    Feb 2003
    Posts
    47
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM

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