Web Hosting Talk







View Full Version : c++ switch statement


luxx
09-13-2009, 09:36 PM
I am trying to get my switch statement to end with a default statement so I can loop back to my case 1 statement to bring up my "Main menu". How can I do this? Should I use something other than a default statement?

WO-Jacob
09-14-2009, 03:20 AM
What it sounds like to me, is if case 1 is the same as you want for default, simply drop your case 1 and only use the default, no?

luxx
09-14-2009, 11:52 PM
What I meant was...I want my default statement to run through my cases again if the user doesn't type in a correct option, until the user finally types in a correct option.

UNIXy
09-15-2009, 12:02 AM
You could do something like this

switchit:
a=mainmenu();
switch(a) {
case 'X':
doonething;
break
case 'Y':
dosomething();
break;
default:
goto switchit; /* bring up main menu ago */
}

Regards

WO-Jacob
09-15-2009, 12:15 AM
Then what you really want to do is have a while or some other form of loop above the switch..

psudo code:

while (input = invalid)
{
Give me input!
}
switch (input)
{
cases
}

plumsauce
09-15-2009, 03:21 AM
Then what you really want to do is have a while or some other form of loop above the switch..

psudo code:

while (input = invalid)
{
Give me input!
}
switch (input)
{
cases
}

actually, it should enclose the switch statement in order to get back to the input routine.

eg.

while(1)
{
a=getinput();
switch(a)
{
case 1: break;
default: continue;
break; // just because
}
break; // always bail if doing nothing else
}

Codelphious
09-16-2009, 11:42 PM
actually, it should enclose the switch statement in order to get back to the input routine.

eg.

while(1)
{
a=getinput();
switch(a)
{
case 1: break;
default: continue;
break; // just because
}
break; // always bail if doing nothing else
}

Hold on plumsauce, "getintput" isn't part of the stdio library and functions aren't explained until chapter 3 so they can't be used in this project.

Luxx,

If we knew which text book you were supposed to be reading for your class we might be able to direct you to the exact page you're looking for. :rolleyes:

luxx
09-17-2009, 12:15 AM
Hold on plumsauce, "getintput" isn't part of the stdio library and functions aren't explained until chapter 3 so they can't be used in this project.

Luxx,

If we knew which text book you were supposed to be reading for your class we might be able to direct you to the exact page you're looking for. :rolleyes:

No idea what you're talking about bro. Not doing a school project of any sort, just thought id ask on a forum for some help.

Anyway, thanks for the helpful replies (most of you)

plumsauce
09-17-2009, 02:29 AM
Hold on plumsauce, "getintput" isn't part of the stdio library and functions aren't explained until chapter 3 so they can't be used in this project.

Luxx,

If we knew which text book you were supposed to be reading for your class we might be able to direct you to the exact page you're looking for. :rolleyes:

Chapter 3 of what? And who cares. Certainly not Luxx, as he says "what are you talking about?"

The name getinput() was for illustrative purposes, it could be getcrap() for all that it matters, there was no implication that it was part of any library of any kind whatsoever. The point of the ocde was to illustrate a switch{} within a while{} and the behaviour of continue in that particular construct.

As you contributed no code of any kind to the discussion nor did you bother to dissect any other preceding your post, there is no constructive point in posting at all.

Codelphious
09-17-2009, 11:19 AM
Chapter 3 of what? And who cares. Certainly not Luxx, as he says "what are you talking about?"

The name getinput() was for illustrative purposes, it could be getcrap() for all that it matters, there was no implication that it was part of any library of any kind whatsoever. The point of the ocde was to illustrate a switch{} within a while{} and the behaviour of continue in that particular construct.


I wasn't scrutinizing your example. Obviously you didn't catch on to the jest. The "rolleyes" smilie is used to convey sarcasm.



As you contributed no code of any kind to the discussion nor did you bother to dissect any other preceding your post, there is no constructive point in posting at all.Nor would I. I, like many others who've gone through the formal education process, know that simply giving people code to use does not help them become better programmers and in the end we all suffer because of it.

If you really intend on helping people, perhaps you should explain the design principles behind your examples, or at least offer an explanation of basic control structures to our OP.

vijaykr25
09-21-2009, 10:51 AM
I am trying to get my switch statement to end with a default statement so I can loop back to my case 1 statement to bring up my "Main menu". How can I do this? Should I use something other than a default statement?

the solution is given by below was not working
so i m also hanged...
plz help me ......

Nimsical
09-21-2009, 04:32 PM
Usually I would put everything in a while loop and set a variable (char/int) to check what menu item I'm at.. and when it goes to default I can change it to 1(first) but that's usually not the case:

int i;
while (surfing) {
i = menuitem();
switch (i) {
case 1: dostuff; break;
case 2: otherstuff; break;
default: break; // Note that we won't need this because menuitem() should take care of this case, instead of making everything harder for us here
}
}

vijaykr25
09-23-2009, 10:19 AM
thanx budy its working....:D