473,387 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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::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" ************************");
};
Jul 21 '05 #1
6 1735
ChrisB <Ch****@spam.net> wrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
> ChrisB <Ch****@spam.net> wrote:
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.)

--


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
Jul 21 '05 #3
<"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com>> wrote:
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com>> wrote:
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.


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


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.
Jul 21 '05 #5
<"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com>> wrote:
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.


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6
"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote
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.

--
Chris Mullins

Jul 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
17
by: prafulla | last post by:
Hi all, I don't have a copy of C standard at hand and so anyone of you can help me. I have always wondered how switch statements are so efficient in jumping to the right case (if any)? Can...
5
by: Alvin Bruney | last post by:
Is a switch more efficient than an if statement? I observe thru the debugger that a switch statement jumps directly to its case handler where as an if statement examines all conditions...
3
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
27
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
25
by: v4vijayakumar | last post by:
'continue' within switch actually associated with the outer 'while' loop. Is this behavior protable? int ch = '\n'; while (true) { switch(ch) { case '\n': cout << "test"; continue; } }
12
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
2
by: Phillip B Oldham | last post by:
What would be the optimal/pythonic way to subject an object to a number of tests (based on the object's attributes) and redirect program flow? Say I had the following: pets = {'name':...
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.