Escaping a called function and returning to main()

I was wondering what the best way to get back to the main function from the called functions: int addition() and int subtraction(). They are called from the switch(choice) and i just want the switch to replay itself after the user does an addition or subtraction problem.



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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include <string>

using namespace std;

int menu();
int addition();
int subtraction();

int main()
{




int choice;
int score = 0;
choice = menu();

do
{





    switch  (choice){
            case 0: cout << score;                     break;
            case 1: addition();                        break;
            case 2: subtraction();                     break;
            default : cout << "Error in input\n\n";    break;
    }
    }
while (choice != 0);
}


int menu() //main menu that gets called by the switch
{
    int MenuChoice;
        cout << "Enter 1 for addition\n";
        cout << "Enter 2 for Subtraction\n";
        cout << "Enter 0 to quit\n";
        cout << "-----------------\n";
        cout << "Choice: ";
    cin >> MenuChoice; // user input 
    return MenuChoice; //return the choice to choice to select a case
}



int addition() //function that is called by the users selection
{
    int score = 0;
    int answer;
    int random1, random2;



srand(time(NULL));

random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99


    cout << "\n" << random1 << " + " << random2 << " = "; //code to format the random math problem
    cin >> answer; // user input of answer
{




if (answer == (random1+random2)){
        cout << "Congratulations\n\n";  
        ++score;
        cout << score;}   //if user is correct than the score is incremented
    else{
        cout << "Incorrect\n\n";} //if not than the score is not incremented

return 0;

}
}



int subtraction() // follows same model as previous function, but instead subtraction.
{


    int answer;
    int random1, random2;
    int score = 0;



srand(time(NULL));
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99



    cout << "\n" << random1 << " - " << random2 << " = ";
    cin >> answer;


    if (answer == (random1-random2)){
        cout << "Congratulations\n\n";
        ++score;
        cout << score;}
          else{
            cout << "Incorrect\n\n";}

return 0;

}



Last edited on
Within main, add a while-loop around the switch statement and another option to quit. The use a bool, change this bool to exit the loop.
Last edited on
Change this:
1
2
3
4
5
6
choice = menu();

do
{

   //.... 


To this:
1
2
3
4
5
6
7
//choice = menu(); used to be here...

do
{
   choice = menu();

   //.... 

but thing is i cant even get out of the addition or subtraction as is. The program runs and then u select the case and then it will just keep throwing math problems at you forever and ever. wouldnt i need some sort of statement to get me out of the function, within the function, before i use your advice?
Last edited on
Further investigate why I posted what I posted above. Meaning, what does your menu function do. Neither your subtraction or addition functions loop. So they are not the problem. Look at your existing loop. Look at the condition at which it is true and false. Know why I suggest what I suggested with everything in consideration. You will learn something and it will stick with you from this point on.

If then you still cant figure it out come back and ask.

Huge hint. I'm guessing the Quit option works as intended?
Last edited on
That worked perfectly thank you! One final question: How can I get the score counter to work properly. At the moment it just prints 1 every time you answer a question correctly. Any advice?
When you go out of scope( the function ends ) you lose the data stored within the variable. Either pass the variable as a reference to the function, or declare the variable as static. (:

EDIT:
You'd be better off passing to the functions by reference, because you'd be keeping two seperate scores, one for each function.
Last edited on
Topic archived. No new replies allowed.