Switch structure not working

This is a discussion on Switch structure not working within the C++ Programming forums, part of the General Programming Boards category; Hello again everyone... I'm kind of stumped on this one. I'm writing a small text based game, and none of ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Norway, Michigan
    Posts
    10

    Switch structure not working

    Hello again everyone... I'm kind of stumped on this one. I'm writing a small text based game, and none of my switch structures seem to be working. I am almost positive that the syntax is correct, but no matter what the input is, it always goes to the default choice, which returns to the menu. Here's my code:
    Code:
    switch(main_menu_choice){
                                 case 1:
                                 system("cls");
                                 goto Play1;
                                 case 2:
                                 system("cls");
                                 goto Help;
                                 case 3:
                                 exit(0);
                                 default:
                                 cout<<"Invalid entry. restarting game...";
                                 system("Pause");
                                 system("cls");
                                 goto MainMenu;
                                 }
    I have no idea what goes wrong. This should work, shouldnt it?

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    You forgot your break; statements.
    Code:
    switch(main_menu_choice){
        case 1:
        system("cls");
        goto Play1;
        break;
        case 2:
        system("cls");
        goto Help;
        break;
        case 3:
        exit(0);
        break;
        default:
        cout<<"Invalid entry. restarting game...";
        system("Pause");
        system("cls");
        goto MainMenu;
        break;
    }
    Also you will want to try and avoid using goto. there are some places it is needed. Although they are few and far between.
    Avoid system() as well, look at the FAQ for ways to clear the screen.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Norway, Michigan
    Posts
    10
    break statements dont help, I've been trying for hours. it compiles, but it does not work.
    About goto:
    I know very well that goto is evil, but it's the only statement that I know that jumps to a specific section of the program, which is what I need for this.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    You might try surrounding statements with ({}) braces. Also, main_menu_choice is an int correct?
    Code:
    switch(main_menu_choice)
    {
        case 1:
    {
        system("cls");
        goto Play1;
        break;
    }
        case 2:
    {
        system("cls");
        goto Help;
    }
        break;
        case 3:
    {
        exit(0);
    }
        break;
        default:
    {
        cout<<"Invalid entry. restarting game...";
        system("Pause");
        system("cls");
        goto MainMenu;
    }
        break;
    }
    sorry for poor indention, but I dont have my editor on this comp

    >>I know very well that goto is evil, but it's the only statement that I know that jumps to a specific section of the program, which is >>what I need for this.
    Umm, functions?

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, it is all correct, and yes, the use of goto (and the wish to jump to a place in code) in this way is awful.

    May-be main_menu_choice is a char, and that's why if you type, say '1', it goes to the default case (because '1' != 1)?
    Last edited by anon; 05-23-2007 at 08:27 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    print main_menu_choice, it might contain a value you dont expect.
    and the break after the goto is not needed

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Quote Originally Posted by him61 View Post
    break statements dont help, I've been trying for hours. it compiles, but it does not work.
    About goto:
    I know very well that goto is evil, but it's the only statement that I know that jumps to a specific section of the program, which is what I need for this.
    No no no!
    If you continue to think like that you'll never be anything but a glorified assembly language programmer.
    Get rid of those gotos and use structured programming. If you don't understand structured program then take the time to learn. One look at that code and any prospective employer will be screaming 'Next!'.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    why using goto
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  9. #9
    Registered User
    Join Date
    Sep 2006
    Location
    Norway, Michigan
    Posts
    10
    Sorry for my n00bishness guys... I just haven't learned anything major yet in my c++ class, just a few basic structures, and the teacher told me to make an RPG game for a final project.
    I experimented with char to begin with, and I discovered that I left it in on accident... Thanks again!
    Again, I know that Goto is evil, and I will try to learn functions and use them before my class ends so that I can use it in here.

    And again, many thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using a character array in a switch question.
    By bajanElf in forum C Programming
    Replies: 10
    Last Post: 11-08-2008, 07:06 AM
  2. switch loop not working
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 10-29-2008, 12:54 PM
  3. addrinfo Structure Not Working
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 10-22-2008, 10:07 AM
  4. case switch not working
    By AmbliKai in forum C Programming
    Replies: 2
    Last Post: 10-09-2008, 06:42 AM
  5. default switch statement not working
    By gL_nEwB in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2006, 10:13 AM

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