Using Functions in Math Program

Below is my code. My math program basically consists of a do while loop that allows the user to input a number that corresponds with a mathematical operation such as (1 = add, 2 = multiply, etc.) and if they get it wrong it outputs the right answer. I put a counter on it to keep track of how many they got right and wrong. Then the end of the program ends when they hit the number 5 and it then it outputs how many they got right and wrong.

I originally wrote this program all in the int main() but I had to then rewrite it using functions. My code performs perfectly when written in just int main(). The problem I am having is that my counter does not work when they are in the functions. If you get a problem right twice the correct counter should read: 2, but it will just keep outputting "1" no matter how many times you get it incorrect or correct. Could someone help me out or share what I need to do in order to fix this. Thanks!

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using namespace std;

//void welcome();
//void splash();
void add(int, int, int, int, int);
void multiply(int, int, int, int, int);
void int_div(int, int, int, int, int);
void Modulus(int, int, int, int, int);




int main()
{
    int option, num1, num2, userAnswer, correct, incorrect;
    //using constants to make code easier to read
    const int ADD = 1, MULTIPLY = 2, INT_DIV = 3, MODULUS = 4, EXIT = 5;
    unsigned int seed = time(NULL);

    correct = incorrect = 0;

    srand(seed);

    //function call
    //splash();
    //welcome();

    //do while loop allows user to continuously input any number they would like
    //until they enter 5.
    do
    {
        num1 = 1 + rand() % 10;
        num2 = 1 + rand() % 10;

        system("CLS");

        cout << down5;
        cout << over4 << "Basic Math" << endl;
        cout << over4 << "----------" << endl;
        cout << over4 << "1. Addition" << endl;
        cout << over4 << "2. Multiplication" << endl;
        cout << over4 << "3. Integer Division" << endl;
        cout << over4 << "4. Modulus" << endl;
        cout << over4 << "5. Exit" << endl;
        cout << over4 << "Option: ";
        cin >> option;
        cout << "\n\n";


        if (option == ADD)
        {
            add(num1, num2, correct, incorrect, userAnswer);
        }
        else if (option == MULTIPLY)
        {
           multiply(num1, num2, correct, incorrect, userAnswer);
        }
        else if (option == INT_DIV)
        {
            int_div(num1, num2, correct, incorrect, userAnswer);
        }
        else if (option == MODULUS)
        {
            Modulus(num1, num2, correct, incorrect, userAnswer);
        }
        else if (option == EXIT)
        {
            cout << over4 << "Correct\tIncorrect" << endl
                 << over4 << "-------\t---------" << endl
                 << over4 << setw(4) << correct << "\t"
                          << setw(5) << incorrect << endl;
        }
        else
        {
           cout << over4 << option << " is not a valid menu item." << endl;
        }
        cout << down5;
        system("PAUSE");

    } while (option != EXIT);   //once 5 is entered the loop breaks and the system closes

    cin.get();
}

void add(int num1, int num2, int userAnswer, int correct, int incorrect)
{
    correct = incorrect = 0;

    cout << over4 << "Addition" << endl << endl;
    cout << over4 << num1 << " + " << num2 << " = ";
    cin >> userAnswer;

    if(userAnswer == num1 + num2)
    {
        cout << over4 << "Correct!" << endl;
        correct++;
        cout << over4 << "Correct " << correct << endl;
    }
    else
    {
        cout << over3 << "Incorrect. The correct answer is "
             << num1 + num2 << endl;
        incorrect++;
        cout << over4 << "Incorrect " << incorrect << endl;
    }
}

void multiply(int num1, int num2, int userAnswer, int correct, int incorrect)
{
    correct = incorrect = 0;

    cout << over4 << "Multiplication" << endl;
    cout << over4 << num1 << " * " << num2 << " = ";
    cin >> userAnswer;

    if(userAnswer == num1 * num2)
    {
        cout << over4 << "Correct!" << endl;
        correct++;
        cout << over4 << "Correct " << correct << endl;
    }
    else
    {
        cout << over3 << "Incorrect. The correct answer is "
             << num1 * num2 << endl;
        incorrect++;
        cout << over4 << "Incorrect " << incorrect << endl;
    }
}

void int_div (int num1, int num2, int userAnswer, int correct, int incorrect)
{
    cout << over4 << "Integer Division" << endl;
    cout << over4 << num1 << " / " << num2 << " = ";
    cin >> userAnswer;

    if(userAnswer == num1 / num2)
    {
        cout << over4 << "Correct!" << endl;
        correct++;
        cout << over4 << "Correct " << correct << endl;
    }
    else
    {
        cout << over3 << "Incorrect. The correct answer is "
             << num1 / num2 << endl;
        incorrect++;
        cout << over4 << "Incorrect " << incorrect << endl;
    }
}

void Modulus(int num1, int num2, int userAnswer, int correct, int incorrect)
{
    cout << over4 << "Modulus" << endl;
    cout << over4 << num1 << " % " << num2 << " = ";
    cin >> userAnswer;

    if(userAnswer == num1 % num2)
    {
        cout << over4 << "Correct!" << endl;
        correct++;
        cout << over4 << "Correct " << correct << endl;
    }
    else
    {
        cout << over3 << "Incorrect. The correct answer is "
             << num1 % num2 << endl;
        incorrect++;
        cout << over4 << "Incorrect " << incorrect << endl;
    }
}
In the add and modify functions the code sets correct and incorrect back to 0 at the beginning of the function.
Even when I take that code out, the function still does the same thing and reverts back to 0 every time no matter what.
You don't need to set correct and incorrect to 0, like wildblue said.

However, your functions need to pass the variables by reference rather than by value.

This is an oversimplification, but passing by reference means whatever changes you make to your variables inside your function will be saved to that variable when you get out of the function. Passing by value means all the changes you make to those variables inside your function will NOT change the variable outside the function.

So since you pass by value, your variables will remain unchanged in main... so they will be 0 still.

Let me give you a quick example of passing by reference and passing by value (note this is pseudocode).

By value:
1
2
3
4
5
6
7
8
int foo(int a)
{
    a++;
}

int number = 0;
foo(number);
// Number here will still be 0. 


By reference:
1
2
3
4
5
6
7
8
int foo(int &a)
{
    a++;
}

int number = 0;
foo(number);
// Number here will now be 1. 


Apply this to your code and see if this helps.
I really appreciate the feedback! I took your advice and tried doing that in my functions and this is the error I received:

undefined reference to `add(int, int, int, int, int)'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Last edited on
Make sure your function declarations on lines 5-8 match the function definitions underneath main.

If you put a & before some of the variable arguments in one of those, you need to put the & in the variable arguments in the other.
so in my variable declaration above the main would I just put them as say:

void add(int&, int&, int&, int&, int&);
If your function definition has it as void add(int &num1, int &num2, int &userAnswer, int &correct, int &incorrect), then yes.
Ok I just tried it and it worked! That's awesome man I really appreciate it. I just haven't gotten that far with my programming to know that or even necessarily know what that means. I have had a brief introduction to pointers and references, but I'm still very far away from fully grasping that concept. (And I'm about to come up on that section in my Jumping into C++ book I just bought by Alex Allain) But I'm sure I will learn about them also in my Object-oriented programming class coming up in the spring. Thanks again!
I also hate to keep asking questions, but when I press the 5 key which exits the program and it displays how many I have gotten right and wrong, I am getting random numbers for some reason. The first time I ran it I got "6 incorrect" and "1 correct", then second time "18 correct" and "1 incorrect"
Last edited on
I'll have to look at it later tonight. If you post your modified code, it will make it a lot easier for me (or someone else that may look at this before I can get to it).
Below is my modified code. I originally had the "option == EXIT" code in the main but I transferred it to a function, but in the main and in the function I get the same results where it randomly outputs correct and incorrect numbers when the option 5 is pushed.
It does keep track of how many I have correct and incorrect while the program is running, but the final display of incorrect and correct is wrong.
If it helps, the correct seems to just output random numbers, and the incorrect seems to usually output either a "1" or a "0"

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using namespace std;

//void welcome();
//void splash();
void add(int&, int&, int&, int&, int&);
void multiply(int&, int&, int&, int&, int&);
void int_div(int&, int&, int&, int&, int&);
void Modulus(int&, int&, int&, int&, int&);
void exit(int&, int&);




int main()
{
    int option, num1, num2, userAnswer, correct, incorrect;
    //using constants to make code easier to read
    const int ADD = 1, MULTIPLY = 2, INT_DIV = 3, MODULUS = 4, EXIT = 5;
    unsigned int seed = time(NULL);

    correct = incorrect = 0;

    srand(seed);

    //function call
    //splash();
    //welcome();

    //do while loop allows user to continuously input any number they would like
    //until they enter 5.
    do
    {
        num1 = 1 + rand() % 10;
        num2 = 1 + rand() % 10;

        system("CLS");

        cout << down5;
        cout << over4 << "Basic Math" << endl;
        cout << over4 << "----------" << endl;
        cout << over4 << "1. Addition" << endl;
        cout << over4 << "2. Multiplication" << endl;
        cout << over4 << "3. Integer Division" << endl;
        cout << over4 << "4. Modulus" << endl;
        cout << over4 << "5. Exit" << endl;
        cout << over4 << "Option: ";
        cin >> option;
        cout << "\n\n";


        if (option == ADD)
        {
            add(num1, num2, correct, incorrect, userAnswer);
        }
        else if (option == MULTIPLY)
        {
           multiply(num1, num2, correct, incorrect, userAnswer);
        }
        else if (option == INT_DIV)
        {
            int_div(num1, num2, correct, incorrect, userAnswer);
        }
        else if (option == MODULUS)
        {
            Modulus(num1, num2, correct, incorrect, userAnswer);
        }
        else if (option == EXIT)
        {
            exit(correct, incorrect);
            /*cout << over4 << "Correct\tIncorrect" << endl
                 << over4 << "-------\t---------" << endl
                 << over4 << setw(4) << correct << "\t"
                          << setw(5) << incorrect << endl; */
        }
        else
        {
           cout << over4 << option << " is not a valid menu item." << endl;
        }
        cout << down5;
        system("PAUSE");

    } while (option != EXIT);   //once 5 is entered the loop breaks and the system closes

    cin.get();
}

void add(int &num1, int &num2, int &userAnswer, int &correct, int &incorrect)
{
    cout << over4 << "Addition" << endl << endl;
    cout << over4 << num1 << " + " << num2 << " = ";
    cin >> userAnswer;

    if(userAnswer == num1 + num2)
    {
        cout << over4 << "Correct!" << endl;
        correct++;
        cout << over4 << "Correct " << correct << endl;
    }
    else
    {
        cout << over3 << "Incorrect. The correct answer is "
             << num1 + num2 << endl;
        incorrect++;
        cout << over4 << "Incorrect " << incorrect << endl;
    }
}

void multiply(int &num1, int &num2, int &userAnswer, int &correct, int &incorrect)
{
    cout << over4 << "Multiplication" << endl;
    cout << over4 << num1 << " * " << num2 << " = ";
    cin >> userAnswer;

    if(userAnswer == num1 * num2)
    {
        cout << over4 << "Correct!" << endl;
        correct++;
        cout << over4 << "Correct " << correct << endl;
    }
    else
    {
        cout << over3 << "Incorrect. The correct answer is "
             << num1 * num2 << endl;
        incorrect++;
        cout << over4 << "Incorrect " << incorrect << endl;
    }
}

void int_div (int &num1, int &num2, int &userAnswer, int &correct, int &incorrect)
{
    cout << over4 << "Integer Division" << endl;
    cout << over4 << num1 << " / " << num2 << " = ";
    cin >> userAnswer;

    if(userAnswer == num1 / num2)
    {
        cout << over4 << "Correct!" << endl;
        correct++;
        cout << over4 << "Correct " << correct << endl;
    }
    else
    {
        cout << over3 << "Incorrect. The correct answer is "
             << num1 / num2 << endl;
        incorrect++;
        cout << over4 << "Incorrect " << incorrect << endl;
    }
}

void Modulus(int &num1, int &num2, int &userAnswer, int &correct, int &incorrect)
{
    cout << over4 << "Modulus" << endl;
    cout << over4 << num1 << " % " << num2 << " = ";
    cin >> userAnswer;

    if(userAnswer == num1 % num2)
    {
        cout << over4 << "Correct!" << endl;
        correct++;
        cout << over4 << "Correct " << correct << endl;
    }
    else
    {
        cout << over3 << "Incorrect. The correct answer is "
             << num1 % num2 << endl;
        incorrect++;
        cout << over4 << "Incorrect " << incorrect << endl;
    }
}

void exit(int &correct, int &incorrect)
{
    cout << over4 << "Correct\tIncorrect" << endl
         << over4 << "-------\t---------" << endl
         << over4 << setw(4) << correct << "\t"
                  << setw(5) << incorrect << endl;
}
Be careful both with your function arguments and how you call them.

Firstly, you shouldn't be using references in your functions unless you need them to be changed in the function. For example, num1 and num2 aren't changed in the functions - they are just used by those functions. So they can just be passed in as int num1, int num2 without using a reference. Technically passing them in as a reference is not making anything behave incorrectly, but it is a safer thing to do. You do not want num1 and num2 to change in the function, so don't pass them by reference.

If you look carefully at the order of your arguments, you are mismatching them. For example, when you call the add function on line 53, you pass in correct and incorrect as the 3rd and 4th arguments. However, on line 87, they are the 4th and 5th arguments.

That's why your correct and incorrect numbers are off.
Honestly man I didn't even know that the order mattered when calling the functions, although I should have figured it did since everything in C++ is so tedious and even a missing a semi colon can throw off your entire program. I can't thank you enough for your help! My program runs just fine now and the main() looks a lot cleaner due to the functions.
You will probably be seeing more posts from me in about a month or so because I start my object oriented programming class in the beginning of January and I know that class will be difficult and arduous. Thanks again, you were a lot of help
Topic archived. No new replies allowed.