Switch within a switch called from a function

As the title is referencing is I want to setup functions to call at certain point within the code. For example if I set up four functions fn1, fn2 fn3, fn4 can I call these either from main or within the function? I am doing this off my iPad since I am not near my computer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void fn1();
void fn2();
void fn3();
void fn4();
main(){


fn1;

fn1(choice) { 
switch
   case 1 : do some stuff
        fn2();
        break;
   case 2 : do some more stuff
        fn3();
        break;
   case 4 : do even more stuff
        .
        . 
        .

 }
void fn1(){ 
//setup call to fn2


}



I know it isn't complete but I am just making sure I can do this or not
Last edited on
You can call those functions from anywhere you like.
Cool. I am working on a SIMPLE exercise out of primer plus and going beyond what the author is asking for. Just wanted to make sure it would work. I will post my code once I get done with it. Most likely tomorrow or later
also you can only call a function anywhere as long as it is a prototype..

Last edited on
So far I have got this to work. Somewhat... What I want to to do is go to the second switch statement which I have set as a function. The fAnimal function works like it is supposed to but upon '0' exit back to the main menu it jumps to the quit statement and exits the program. Am I missing something here? Also another little problem is in the sub switch if I try and enter anything other than the correct response from the list the program freezes. I have another program which is a calc program and everything works fine in it. so I am beating myself up over this. ie... under the default : I have this. and if anything is pressed other than the 5 different responses it keeps looping until the correct response if given.
1
2
3
default :
            cout << "You must enter a 1 thru 4 or 0 to EXIT";
            cin >> choice;


So I just can't figure out what I have done here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*3. Write a precursor to a menu-driven program. The program should display a menu
offering four choices, each labeled with a letter. If the user responds with a letter
other than one of the four valid choices, the program should prompt the user to
enter a valid response until the user complies.Then the program should use a
switch to select a simple action based on the user’s selection.A program run could
look something like this:
Please enter one of the following choices:
c) carnivore p) pianist
t) tree g) game
f
Please enter a c, p, t, or g: q
Please enter a c, p, t, or g: t
A maple is a tree.
*/
#include "version.h"
#include <iostream>
using namespace std;
void showMenu();
void carnivoe();
void pianist();
void tree();
void game();
int fAnimal();
int piano, gamer, trees;
char choice;
int main()
{

    showMenu();
    cin >> choice;
    while (choice != 'q' && choice != 'Q')
    {
        switch (choice)
            {
            case 'C' :
            case 'c' :
                        {
                            carnivoe();
                            fAnimal();
                        }




        /*   case 'T' :
           case 't' :

                            cout << "You chose a Tree.\n"
                                    "Please select from the list of what you want:\n"
                                    "1) Dog                              2) Cat   \n"
                                    "3) Wolf                             4) Bear  \n"
                                    "5) Lion\n";
                            cout << "Select: ";
                            cin >> select;

*/

           case 'p' :
           case 'P' :

              /*              cout << "You chose a Carnivore.\n"
                                    "Please select from the list of what you want:\n"
                                    "1) Dog                              2) Cat   \n"
                                    "3) Wolf                             4) Bear  \n"
                                    "5) Lion\n";
                            cout << "Select: ";
                            cin >> piano;
*/

           case 'g' :
           case 'G' :
/*
                            cout << "You chose a Carnivore.\n"
                                    "Please select from the list of what you want:\n"
                                    "1) Dog                              2) Cat   \n"
                                    "3) Wolf                             4) Bear  \n"
                                    "5) Lion\n";
                            cout << "Select: ";
                            cin >> gamer;
*/
            case 'q':
            case 'Q':
                cout << "Good bye";
                return 0;

            default :
            "You did not make a correct choice. please try again.";
            showMenu();
            cin >> choice;
    }
    showMenu();
    cin >> choice;
    }
    return 0;
}
void showMenu()
{
    cout << "Please enter one of the following choices:\n"
            "c) carnivore                    p) pianist\n"
            "t) tree                         g) game   \n"
            "q) Quit";
    cout << "\nChoice: ";
}

void carnivoe(){

    cout << "You chose a Carnivore.\n"
            "Please select from the list of what you want:\n"
            "1) Dog                              2) Cat   \n"
            "3) Wolf                             4) Bear  \n"
            "5) Lion                             0) Exit  \n";
    cout << "Select: ";

}

int fAnimal(){
        int animal;
        cin >> animal;
        while (animal != 0)
        {
        switch (animal)
                {

                    case 1 :
                        cout << "You chose a dog which is a type of carnivore.\n";
                        break;
                    case 2 :
                        cout << "Yes your 'Fluffy' is also a type of carnivore.\n";
                        break;
                    case 3 :
                        cout << "A wolf if just a big, mean and nasty dog, but also a type of carnivore.\n";
                        break;
                    case 4 :
                        cout << "A bear is just a huge critter with a very bad disposition.\n";
                        break;
                    case 5 :
                        cout << "A Lion is a big kitty and also called the 'King of the Jungle'\n";
                        break;
                    case 0 :
                        cout << "Returning to the main menu\n";
                        showMenu();
                        cin >> choice;
                    default :
                        "You did not make a correct choice. please try again.\n";
                      cin >> animal;
                    }
                    carnivoe();
                    cin >> animal;
        }
    showMenu();
    cin >> choice;
}
Last edited on
A function should have a single purpose. Your fAnimal does both something animal-related and asks for a choice. That is too complicated. Limit it to the animal-stuff.

You are using global variables. Don't. They might seem to make things simpler, but in practice they do the opposite.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main() {
  char choice;
  showMenu();
  while ( std::cin >> choice ) {
    switch ( choice ) {
      case 'q':
        return 0;
      case 'x':
        // call xfunc()
        break;
      default:
        std::cout << "invalid option\n";
    }
    showMenu();
  }
  return 0;
}
But how can I get it to return to the main menu without quiting the program?

Like this is my simple calculator program... it works

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <cstdlib>
#include <iostream>

using namespace std;
float add(float num1, float num2);
float sub(float num1, float num2);
float mut(float num1, float num2);
float div(float num1, float num2);
void showMenu();
int main()
{
      float i, j;
      int choice;
      while (choice != 0)
{

    showMenu();
    cin >> choice;
    //setup switch statements

        switch (choice)

    {
        case 1 :
            cout << "Enter two numbers\n";
            cout << " = " << add (i, j) << endl;
            break;
        case 2 :
            cout << "Enter two numbers\n";
            cout << " = " << sub(i, j) << endl;
            break;
        case 3 :
            cout << "Enter two numbers\n";
            cout << " = " << mut(i, j) << endl;
            break;
        case 4 :
            cout << "Enter two numbers\n";
            cout << " = " << div(i, j) << endl;
            break;
        case 0 :
            cout << "Good Bye!!!";
            return 0;
        default :
            cout << "You must enter a 1 thru 4 or 0 to EXIT";
            cin >> choice;
    }
    }
    return 0;
}

void showMenu(){
    cout << endl <<"Simple Calculator\n" << "1. Addition\n" << "2. Subtraction\n" << "3. Multiplication\n"
         << "4. Division\n" << "0 to EXIT\n" << "Choice: ";

}

float add(float num1, float num2)
    {
    cin >> num1 >> num2;
    cout << "Adding " << num1 << " and " << num2;
    return num1 + num2;
    }
float sub(float num1, float num2)
    {
    cin >> num1 >> num2;
    cout << "Subtracting " << num1 << " and " << num2;
    return num1 - num2;
    }
float mut(float num1, float num2)
    {
    cin >> num1 >> num2;
    cout << "Multiplying " << num1 << " and " << num2;
    return num1 * num2;
    }
float div(float num1, float num2)
    {
    cin >> num1 >> num2;
    cout << "Dividing " << num1 << " and " << num2;
    return num1 / num2;
    }
That sort of works. Why do you have line 45? The choice was not 0, so line 18 will give you a new value.

Why does the main say "Enter two numbers", but the functions actually read them?
Why does the main have i and j at all, because it does not use them?
Following that thought, the cin >> choice; could be in showMenu().


fAnimal() is no different from add(); they are both called and they both return. It is the differences in the main() that makes one "work" and other "not".
I was following an example I seen on youtube.
Just follow what you've learn not every new one function you will see
Well I can get the program to work and it will call the functions that I have built but once I try and go back to the main menu is when everything gets squirrelly... It will show the main menu but jump to the very first case statement again... So is this not iteration? About to give up and move on to another exercise.
Last edited on
Which version of main() does that?
Line 14: choice is uninitialized. First time through the loop, you going to be comparing garbage. That doesn't explain the behavior you're seeing, but it's still wrong.
I was talking about the original switch case with a nested switch called from a function. Once I tell the function to return to the main switch it gets really trashed.
How does this behave?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

void showMenu() { std::cout << "'x' or 'q'\n"; }
void xfunc();

int main() {
  char choice;
  showMenu();
  while ( std::cin >> choice ) {
    switch ( choice ) {
      case 'q':
      case 'Q':
        return 0;
      case 'x':
        xfunc();
        break;
      default:
        std::cout << "invalid option\n";
    }
    showMenu();
  }
  return 0;
}


void xfunc() {
  int animal;
  std::cout << "'1', '2', or '0'\n";
  while ( std::cin >> animal ) {
    switch ( animal ) {
      case 1:
        std::cout << "cat\n";
        break;
      case 2:
        std::cout << "dog\n";
        break;
      case 0:
        std::cout << "Back to main\n";
        return;
      default:
        std::cout << "invalid option\n";
    }
    std::cout << "'1', '2', or '0'\n";
  }
}
Last edited on
Topic archived. No new replies allowed.