Hi everyone. I'm new to the forum and thought it would be a good idea to come to the experts with some of my questions. I am currently trying to write a relatively simple program but being quite new to c++ i'm struggling with some things. I am trying to use a switch statement twice in my program. The user makes a selection and according to the switch statement it returns a given answer. But i then need to get the user to make another selection using the same switch statement. I can write the switch statment twice and it works fine but i have been informed i can use the same one twice. Any help with this would be greatly appreciated and if the technical speak could be kept to a minimum that would help too as i'm afraid my tiny brain cant cope :) Thanks everyone.

Recommended Answers

All 3 Replies

Not quite sure what you mean, but maybe this will help you :

switch(option){ 
 case 1 : doCase1(); break;
 case 2 : doCase2(); break;
 case 3 :
 case 4 : doCase3And4(); break;
 default : break;
}

The idea is that if the user picks 3 or 4 it will go into case 4. If this is not what you needed then perhaps you can post an example of what you mean.

Hi. Yeah i'm not the best at explaning it. I nedd to use the switch twice. i'v used the scanf then &leaving to go to the switch statement and mak the choice. I then need to use it again but store the choice in a different address. When i use the next scanf and &arriving it obviously doesn't go the switch statment again. I've put an extract of my code to try and help. Thanks for the help.
Heres the first bit:

      scanf ("%d", &leaving);

      switch (leaving)
      {
             case 1:
                  system ("CLS");
                  printf("\nYou have selected : ");
                  printf(ash);
                  break;

then the next section has:

scanf("%d", &arriving);

and i need this to go to the same switch statement.
I've cut lots out to keep it short :)

Hi. Yeah i'm not the best at explaning it. I nedd to use the switch twice. i'v used the scanf then &leaving to go to the switch statement and mak the choice. I then need to use it again but store the choice in a different address. When i use the next scanf and &arriving it obviously doesn't go the switch statment again. I've put an extract of my code to try and help. Thanks for the help.
Heres the first bit:

scanf ("%d", &leaving);

switch (leaving)
{
case 1:
system ("CLS");
printf("\nYou have selected : ");
printf(ash);
break;

then the next section has:

scanf("%d", &arriving);

and i need this to go to the same switch statement.
I've cut lots out to keep it short :)

The purpose of a switch statement is to take an arbitrary input, compare it to the parameters of the case and see if it matches, then do whatever is in the case. So what you would do for anything with a switch statement is

switch(input)
{
case a:

*do some stuff*

break;

case b:

*do some different stuff*

break;

case c:

*do yet some else*

break;

This specific code would only go through the switch once, and only do what your selection was, either a, b or c. If you want to keep doing the switch statement over and over so that you can use it multiple times, stick it in a loop. The best is a while (actually a do while would be better but if you are a beginner I don't think they have taught you a do while yet) so you could have while (input != x) around your switch statement, and then it will keep looping through your switch until you give it the x input, then it will break out of the loop containing the switch.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.