Switch Statement

  • Thread starter Thread starter ChrisB
  • Start date Start date
C

ChrisB

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::WriteLine(S"Choose from Menu: ");
String * input = Console::ReadLine();
int choice = input->ToInt32(0);
return choice;

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



with the call to
void DrawMenu()
{
Console::WriteLine(S" *****BioSphere Menu*****");
Console::WriteLine(S" * *");
Console::WriteLine(S" * 1) New Game *");
Console::WriteLine(S" * 2) Load Game *");
Console::WriteLine(S" * 3) Save Game *");
Console::WriteLine(S" * 4) Options *");
Console::WriteLine(S" * 5) Add Plants *");
Console::WriteLine(S" * 6) Add Herbivores *");
Console::WriteLine(S" * 7) Add Carnivores *");
Console::WriteLine(S" * 8) Quit *");
Console::WriteLine(S" * *");
Console::WriteLine(S" ************************");
};
 
ChrisB said:
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?

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.)
 
ChrisB said:
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.)

--

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
 
It probably does, but they often get swamped in other warnings.

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.
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.

I would seriously just take the time to correct *every* warning you
possibly can, and get a clean compile.
 
Jon Skeet said:
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.

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.
I would seriously just take the time to correct *every* warning you
possibly can, and get a clean compile.

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.
 
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.

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.)
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.

Except that it lets you spot other important warnings :)
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.

If there are specific warnings that you always ignore, I suggest you
disable them so that you can see the "real" warnings more easily.
 
Steve McLellan said:
There are a lot of warnings about type conversions which (most of the time)
can be safely ignored.

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.".
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.

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.
 
Back
Top