Switch Statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ChrisB

    #1

    Switch Statement

    I have two pieces of code show below. The first contains a switch
    statement. The second contains a text shown on the console application
    screen. The simple menu shows up fine, I enter a number and the number
    shows fine. But I do not get the console message printed, only the
    standard press any key message followed by a program exit. This is
    expected since there is essentially no more programming. Any ideas why
    entering 1 will display 1 on the screen but not New Game message? and
    any ideas how I can change this so that the messages in the switch
    statement do show?



    bool bStillPlaying = true;
    int choice = 0;

    while (bStillPlaying)
    {
    DrawMenu();

    Console::WriteL ine(S"Choose from Menu: ");
    String * input = Console::ReadLi ne();
    int choice = input->ToInt32(0);
    return choice;

    switch(choice)
    {
    case 1: Console::WriteL ine(S"New Game"); break;
    case 2: Console::WriteL ine(S"Load Saved Game"); break;
    case 3: Console::WriteL ine(S"Save Game"); break;
    case 4: Console::WriteL ine(S"Set Options"); break;
    case 5: Console::WriteL ine(S"Adding Plants to Game"); break;
    case 6: Console::WriteL ine(S"Adding Herbivores to Game"); break;
    case 7: Console::WriteL ine(S"Adding Carnivores to Game"); break;
    case 8: Console::WriteL ine(S"Game Over Already?"); break;
    }
    }



    with the call to
    void DrawMenu()
    {
    Console::WriteL ine(S" *****BioSphere Menu*****");
    Console::WriteL ine(S" * *");
    Console::WriteL ine(S" * 1) New Game *");
    Console::WriteL ine(S" * 2) Load Game *");
    Console::WriteL ine(S" * 3) Save Game *");
    Console::WriteL ine(S" * 4) Options *");
    Console::WriteL ine(S" * 5) Add Plants *");
    Console::WriteL ine(S" * 6) Add Herbivores *");
    Console::WriteL ine(S" * 7) Add Carnivores *");
    Console::WriteL ine(S" * 8) Quit *");
    Console::WriteL ine(S" * *");
    Console::WriteL ine(S" *************** *********");
    };
  • Jon Skeet [C# MVP]

    #2
    Re: Switch Statement

    ChrisB <ChrisB@spam.ne t> wrote:[color=blue]
    > I have two pieces of code show below. The first contains a switch
    > statement. The second contains a text shown on the console application
    > screen. The simple menu shows up fine, I enter a number and the number
    > shows fine. But I do not get the console message printed, only the
    > standard press any key message followed by a program exit. This is
    > expected since there is essentially no more programming. Any ideas why
    > entering 1 will display 1 on the screen but not New Game message? and
    > any ideas how I can change this so that the messages in the switch
    > statement do show?[/color]

    Well you've got a return statement before the switch, so I'm not at all
    surprised it doesn't work. (I'm surprised the compiler doesn't warn you
    about unreachable code though.)

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Steve McLellan

      #3
      Re: Switch Statement

      > ChrisB <ChrisB@spam.ne t> wrote:[color=blue][color=green]
      > > I have two pieces of code show below. The first contains a switch
      > > statement. The second contains a text shown on the console application
      > > screen. The simple menu shows up fine, I enter a number and the number
      > > shows fine. But I do not get the console message printed, only the
      > > standard press any key message followed by a program exit. This is
      > > expected since there is essentially no more programming. Any ideas why
      > > entering 1 will display 1 on the screen but not New Game message? and
      > > any ideas how I can change this so that the messages in the switch
      > > statement do show?[/color]
      >
      > Well you've got a return statement before the switch, so I'm not at all
      > surprised it doesn't work. (I'm surprised the compiler doesn't warn you
      > about unreachable code though.)
      >
      > --[/color]

      It probably does, but they often get swamped in other warnings. On the
      subject, is there a way to make certain warnings into errors? For example, I
      had one that told me I'd essentially written a recursive function that
      couldn't ever return, which seemed serious to me. I've only been able to
      find ways of stamping out certain warnings or whole levels of warnings. I
      suppose I could just disable anything less than "really quite serious" and
      ignore the rest, but then I know I'll never be able to idly scan through all
      the others.

      Steve


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Switch Statement

        <"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com>> wrote:[color=blue][color=green]
        > > Well you've got a return statement before the switch, so I'm not at all
        > > surprised it doesn't work. (I'm surprised the compiler doesn't warn you
        > > about unreachable code though.)[/color]
        >
        > It probably does, but they often get swamped in other warnings.[/color]

        Yikes - don't you get your code to compile warning-free? I know it's
        harder with C++ than with C#, but I'm sure it's possible.
        [color=blue]
        > On the subject, is there a way to make certain warnings into errors?
        > For example, I had one that told me I'd essentially written a
        > recursive function that couldn't ever return, which seemed serious to
        > me. I've only been able to find ways of stamping out certain warnings
        > or whole levels of warnings. I suppose I could just disable anything
        > less than "really quite serious" and ignore the rest, but then I know
        > I'll never be able to idly scan through all the others.[/color]

        I would seriously just take the time to correct *every* warning you
        possibly can, and get a clean compile.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Steve McLellan

          #5
          Re: Switch Statement


          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          news:MPG.1b792a 6719a451cf98b06 a@msnews.micros oft.com...[color=blue]
          > <"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com>> wrote:[color=green][color=darkred]
          > > > Well you've got a return statement before the switch, so I'm not at[/color][/color][/color]
          all[color=blue][color=green][color=darkred]
          > > > surprised it doesn't work. (I'm surprised the compiler doesn't warn[/color][/color][/color]
          you[color=blue][color=green][color=darkred]
          > > > about unreachable code though.)[/color]
          > >
          > > It probably does, but they often get swamped in other warnings.[/color]
          >
          > Yikes - don't you get your code to compile warning-free? I know it's
          > harder with C++ than with C#, but I'm sure it's possible.
          >[/color]

          There are a lot of warnings about type conversions which (most of the time)
          can be safely ignored. It IS possible to compile warning free, but even the
          compiler advice says you should ignore at least one level of warnings unless
          you're really being pedantic. Code that compiles without warnings on, for
          example, CodeWarrior, springs a lot of them under the VC++ compiler.
          [color=blue][color=green]
          > > On the subject, is there a way to make certain warnings into errors?
          > > For example, I had one that told me I'd essentially written a
          > > recursive function that couldn't ever return, which seemed serious to
          > > me. I've only been able to find ways of stamping out certain warnings
          > > or whole levels of warnings. I suppose I could just disable anything
          > > less than "really quite serious" and ignore the rest, but then I know
          > > I'll never be able to idly scan through all the others.[/color]
          >
          > I would seriously just take the time to correct *every* warning you
          > possibly can, and get a clean compile.
          >[/color]

          The only way to get rid of some is to do a lot of explicit casting which
          isn't any safer than the warnings themselves. Don't get me wrong, we don't
          have tonnes of heinous errors in our code, it's just that the compiler's
          pretty exuberant about its warnings on the default settings.


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Switch Statement

            <"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com>> wrote:[color=blue][color=green]
            > > Yikes - don't you get your code to compile warning-free? I know it's
            > > harder with C++ than with C#, but I'm sure it's possible.[/color]
            >
            > There are a lot of warnings about type conversions which (most of the time)
            > can be safely ignored. It IS possible to compile warning free, but even the
            > compiler advice says you should ignore at least one level of warnings unless
            > you're really being pedantic. Code that compiles without warnings on, for
            > example, CodeWarrior, springs a lot of them under the VC++ compiler.[/color]

            Sure. (This is one of the reasons I like C# - the compiler rarely warns
            me about something which isn't a problem or at least shouldn't raise
            some suspicion and be expressed differently.)
            [color=blue]
            > The only way to get rid of some is to do a lot of explicit casting which
            > isn't any safer than the warnings themselves.[/color]

            Except that it lets you spot other important warnings :)
            [color=blue]
            > Don't get me wrong, we don't
            > have tonnes of heinous errors in our code, it's just that the compiler's
            > pretty exuberant about its warnings on the default settings.[/color]

            If there are specific warnings that you always ignore, I suggest you
            disable them so that you can see the "real" warnings more easily.

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Chris Mullins

              #7
              Re: Switch Statement

              "Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote[color=blue]
              > There are a lot of warnings about type conversions which (most of the[/color]
              time)[color=blue]
              > can be safely ignored.[/color]

              If you know the warning is not important, disable it. It's been a few years,
              but if I remember right "#pragma warning disable (warning number)" in the
              appropiate spots would do it.

              All commercial C++ software that I worked on had the policy "Turn warning
              level to maximum (4, on MS compilers) and do not check in code that produces
              warnings.".
              [color=blue]
              > Don't get me wrong, we don't
              > have tonnes of heinous errors in our code, it's just that the
              > compiler's pretty exuberant about its warnings on the default
              > settings.[/color]

              Wheras I always though the default settings (warning level 3) were not
              strict enough....

              One of my biggest gripes with VB.NET and C# is that I don't get anywhere
              near the richness of compiler warnings that I get even with 10 year old C++
              compilers.

              --
              Chris Mullins



              Comment

              Working...