Thread: Do..While in a Switch

  1. #1
    Registered User
    Join Date
    Oct 2009

    Do..While in a Switch

    Well. I'm having a little bit of an issue, and I'm taking it's just from my absolute lack of experience with C++ atm. But hey, lets give this a go. So, I'm constructing a little menu system. So, I'm using the Do..While statement, while in one itself. (Mind you, I'm still on the first few lessons, but I'm still fiddling around with them. So I'll gladly take some advice on better methods, though I'm not sure if I'll understand it at this time.) So here's my code so far.

    #include <iostream>
    using namespace std;
    int main()
        int input;
        int menu1;
        int menu2;
        int menu3;
        int menu4;
        int menu5;
        menu1 = 0;
        menu2 = 0;
        menu3 = 0;
        menu4 = 0;
        menu5 = 0;
        cout<<"Choose an Option.\n";
        cout<<"1. Option 1.\n";
        cout<<"2. Option 2.\n";
        cout<<"3. Option 3.\n";
        cout<<"4. Option 4.\n";
        cout<<"5. Option 5.\n";
        cout<<"0. Exit.\n";
        cout<<"Selection: ";
        }while (input != 0);
        switch ( input ) {
        case 1:
            cout<<"1. Sub1 Option 1.\n";
            cout<<"2. Sub1 Option 2.\n";
            cout<<"3. Sub1 Option 3.";
            cout<<"4. Sub1 Option 4.";
            cout<<"5. Sub1 Option 5.";
            cout<<"9. Exit.";
            cin>> menu1;
        }while ( menu1 != 9 );
    So, this code is quite incomplete. I'm still at the first stage of making it. Basically, (it's supposed to) work like this:

    You choose option 1, it brings to a submenu. Then, you can choose one of the sub-submenu's. In the end, it'll be similar to a pyramid.

    So a quick explanation of the variables. Input is the main input - for the first menu. Then, Menu1 controls the first submenu. So from the code I have, in essence, at the end, I could put }while ( menu != 9 && input != 0 ) and I (believe) it would still work out. It's just running both subs.

    But, when I run this code, it doesn't move on from one menu to another. (Though I've only done the first sub-menu.) So when I push 1, it should move to the first case, but it doesn't. So what have I got wrong, here?

  2. #2
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Somewhere nearby,Who Cares?
    because you are still in the first loop .
    why ?
    look at your while continuation condition! it says ! until 0! loop! you chose 1! so it is still not zero! loop continues!
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson

  3. #3
    Registered User
    Join Date
    Oct 2009
    Wow, that's just awful. Thanks. xD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM