Using a seperate function for results of an equation that was chosen and answered in main.

I am currently is my 5th week of the semester and this is my first programming class ever. I am working on a math practice program that includes the equations for addition, multiplication, division, modulus, and subtraction in the main. The menu, results, and summary are all as seperate functions from the main. The problem I am faced with is I am not sure how to make it work. I have been working on this for two days and have yet to get it to compile and execute. Any suggestions are greatly appreciated.

using namespace std;


//declare two global variables for count.
int gNum_Correct;
int gNum_Incorrect;

int gRandom1, gRandom2, gCorrAnswer, gResponse;
int totalAnswered;
char choice,sign;


//prototypes
void splash();
void welcome();
void menu();
void menuError();
void equation(int,int,int,char,string);
void results(int,int,int,int);
void summary();



int main(int argc, char *argv[])
{
unsigned seed = time(0);

srand (seed);

gRandom1 = 1 + rand() % (10); //catch a number from 1 to 10
gRandom2 = 1 + rand() % (10); //catch a number from 1 to 10

/* splash();
welcome();*/

menu();
cin >> choice;
menuError();

cout << showpoint << setprecision(2) << fixed;

switch (choice)
{
case 'A':
case 'a':
equation(gRandom1, gRandom2, gRandom1 + gRandom2, '+', "Addition");
break;
case 'B':
case 'b':
equation(gRandom1, gRandom2, gRandom1 - gRandom2, '-', "Subtraction");
break;
case 'C':
case 'c':
equation (gRandom1, gRandom2, gRandom1 * gRandom2, '*', "Multiplication");
break;
case 'D':
case 'd':
equation (gRandom1, gRandom2, gRandom1 / gRandom2, '/', "Integer Division");
break;
case 'E':
case 'e':
equation (gRandom1, gRandom2, gRandom1 % gRandom2, '%', "Modulus");
break;
case 'F':
case 'f':
case 'X':
case 'x':
system ("CLS");
cout << down10
<< over3 << "\n\n\t\t\tYou have decided to end the "
<< "program.\n" << endl;
cout << down10;
break;
}

summary();

return EXIT_SUCCESS;
}
void equation(int gRandom1,int gRandom2,int gCorrAnswer,char sign,string equationType)
{

system ("CLS");
cout << down11
<< over4 << " " << equationType << "\n" << endl
<< over4 << " " << gRandom1 << "" << endl
<< over4 << " " << sign << " " << gRandom2 << endl
<< over3 << " -------------- \n" << endl
<< over3 << "The correct answer is: " << endl
<< over3 << "Please enter your answer. " << endl;
cin >> gResponse;
cout << down8;

results();

}
void results(int gRandom1,int gRandom2,int gCorrAnswer,int gResponse)
{
if (gRandom1 + gRandom2 == gCorrAnswer && gRandom1 + gRandom2 == gResponse)
{
cout << down11 << over3 << "You are correct!!" << endl;
}
else if (gRandom1 - gRandom2 == gCorrAnswer && gRandom1 - gRandom2 == gResponse)
{
cout << down11 << over3 << "You are correct!!" << endl;
}
else if (gRandom1 * gRandom2 == gCorrAnswer && gRandom1 * gRandom2 == gResponse)
{
cout << down11 << over3 << "You are correct!!" << endl;
}
else if (gRandom1 / gRandom2 == gCorrAnswer && gRandom1 / gRandom2 == gResponse)
{
cout << down11 << over3 << "You are correct!!" << endl;
}
else if (gRandom1 % gRandom2 == gCorrAnswer && gRandom1 % gRandom2 == gResponse)
}
else
{
cout << down11 << over3 << "You are incorrect!!" << endl;
}

}
void summary()
{

system ("CLS");
cout << down11
<< over3 << " Summary() was called!!\n" << endl
<< over2 << "The following global variables will be displayed: \n" << endl
<< over3 << " Total number correct: " << gNum_Correct << endl
<< over3 << " Total number incorrect: " << gNum_Incorrect << endl
<< over3 << " Your average is: " << endl
<< down8;

system ("CLS");
}
void menu()//input from user
{
system ("CLS");
cout << down5
<< over3 << " Welcome to Math Practice" << endl
<< over3 << " ------------------------ " << endl
<< over2 << " Please choose a selection that corresponds with" << endl
<< over2 << " the type ofproblem you would like to solve.\n " << endl
<< over4 << " A. Addition" << endl
<< over4 << " B. Subtraction " << endl
<< over4 << " C. Multiplication " << endl
<< over4 << " D. Integer Division " << endl
<< over4 << " E. Modulus " << endl
<< over4 << " F. Exit the program \n" << endl
<< over3 << " Please ENTER your selection here ";
cin >> choice;
}
void menuError()
{
system ("CLS");
cout << down11
<< over3 << "You have chosen an invalid option." << endl
<< over3 << "Your choices are A,B,C,D,E, and F." << endl
<< over3 << "Please try again." << endl
<< down11;
}
First, welcome to the forums. Secondly, when pasting code, please remember to put them in the [code][/code] tags to help us read what you wrote.

First thing I noticed was there are no #include's at the top of your code. You're missing these four:
1
2
3
4
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime> 


After that, the compiler spit out a bunch of errors. The biggest thing that shows up is easily shown here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void equation(int gRandom1,int gRandom2,int gCorrAnswer,char sign,string equationType) {

    system ("CLS");
    cout << down11
         << over4 << " " << equationType << "\n" << endl
         << over4 << " " << gRandom1 << "" << endl
         << over4 << " " << sign << " " << gRandom2 << endl
         << over3 << " -------------- \n" << endl
         << over3 << "The correct answer is: " << endl
         << over3 << "Please enter your answer. " << endl;
    cin >> gResponse;
    cout << down8;

    results();

}


What does down11 do? and what does over4 do? You never declared them, maybe you meant to move the cursor down 11 lines and then over 4?

Other things I noticed a lot is that you're missing semicolons.

Another spot, here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void results(int gRandom1,int gRandom2,int gCorrAnswer,int gResponse) {
    if (gRandom1 + gRandom2 == gCorrAnswer && gRandom1 + gRandom2 == gResponse) {
        cout << down11 << over3 << "You are correct!!" << endl;
    } else if (gRandom1 - gRandom2 == gCorrAnswer && gRandom1 - gRandom2 == gResponse) {
        cout << down11 << over3 << "You are correct!!" << endl;
    } else if (gRandom1 * gRandom2 == gCorrAnswer && gRandom1 * gRandom2 == gResponse) {
        cout << down11 << over3 << "You are correct!!" << endl;
    } else if (gRandom1 / gRandom2 == gCorrAnswer && gRandom1 / gRandom2 == gResponse) {
        cout << down11 << over3 << "You are correct!!" << endl;
    } else if (gRandom1 % gRandom2 == gCorrAnswer && gRandom1 % gRandom2 == gResponse)
    }
else {
    cout << down11 << over3 << "You are incorrect!!" << endl;
}

}


Notice how messy it looks when a code beautifier is applied to it? It means something is wrong. (Hint: you're missing somethings in your last else if)

And the last major thing that isn't an error but will save you from typing it out everytime is this:
int main(int argc, char *argv[])

Unless you plan on using argc and argv[] in your code, there is no need to type them out. Just do this:
int main()

That should give you plenty of things to work on. Once you're done, bring it back and I'll see what needs to be corrected.

Best of luck
Volatile
Thanks for taking the time to help. I did inadvertantly leave part of it off and I apologize. You mentioined I left some semi-colons off and I did that to kind of clean up the code. If I put them in, I have to put a cout in front of the lines I add them too. When I compile and try to execute, I get an error for line 32 which states too few arguments to function 'void results(int,int,int,int)' and points to line 104 at this point in file. I have tried everything to rectify it but cannot get this to work. Again, thanks for the advice.
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
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
#include <ctime>
#define over2 "\t\t"
#define over3 "\t\t\t"
#define over4 "\t\t\t\t"
#define down5 "\n\n\n\n\n"
#define down7 "\n\n\n\n\n\n\n"
#define down8 "\n\n\n\n\n\n\n\n"
#define down10 "\n\n\n\n\n\n\n\n\n\n"
#define down11 "\n\n\n\n\n\n\n\n\n\n\n"

using namespace std;

//declare two global variables for count.
int gNum_Correct;
int gNum_Incorrect;

int gRandom1, gRandom2, gCorrAnswer, gResponse;
int totalAnswered;
char choice,sign;

//prototypes
void menu();
void menuError();
void equation(int,int,int,char,string);
void results(int,int,int,int);
void summary();

int main()
{   
    unsigned seed = time(0);
    
    srand (seed);

    gRandom1 = 1 + rand() % (10); //catch a number from 1 to 10
    gRandom2 = 1 + rand() % (10); //catch a number from 1 to 10
  
   /* splash();
    welcome();*/
    
    menu();
    cin >> choice;
    menuError();
   
         cout << showpoint << setprecision(2) << fixed;  
    
         switch (choice)
         {
                case 'A': 
                case 'a':                          
                         equation(gRandom1, gRandom2, gRandom1 + gRandom2, '+', "Addition");
                         break;
                case 'B':
                case 'b':
                         equation(gRandom1, gRandom2, gRandom1 - gRandom2, '-', "Subtraction");
                         break;
                case 'C':
                case 'c':
                        equation (gRandom1, gRandom2, gRandom1 * gRandom2, '*', "Multiplication");
                        break;
                case 'D':
                case 'd':
                        equation (gRandom1, gRandom2, gRandom1 / gRandom2, '/', "Integer Division");
                        break;
                case 'E':
                case 'e':
                        equation (gRandom1, gRandom2, gRandom1 % gRandom2, '%', "Modulus");
                        break;
                case 'F':
                case 'f':
                case 'X':
                case 'x':
                        system ("CLS");
                        cout << down10
                             << over3 << "\n\n\t\t\tYou have decided to end the "
                                      << "program.\n" << endl;
                        cout << down10;
                        break;
         }
      
    summary();
    
    return EXIT_SUCCESS;
}
void equation(int gRandom1,int gRandom2,int gCorrAnswer,char sign,string equationType)
{
    
    system ("CLS");
    cout << down11
         << over4 << "   " << equationType << "\n" << endl
         << over4 << "      " << gRandom1 << "" << endl
         << over4 << "   " << sign << "  " << gRandom2 << endl
         << over3 << "        --------------       \n" << endl
         << over3 << "Please enter your answer. " << endl;  
         cin >> gResponse;
         cout << down8;
    
   results();
             
}
void results(int gRandom1,int gRandom2,int gCorrAnswer,int gResponse)
{   
    if (gRandom1 + gRandom2 == gCorrAnswer && gRandom1 + gRandom2 == gResponse)
    {   
        cout << down11 << over3 << "You are correct!!" << endl;
    }
    else if (gRandom1 - gRandom2 == gCorrAnswer && gRandom1 - gRandom2 == gResponse)
    {
        cout << down11 << over3 << "You are correct!!" << endl;
    }
    else if (gRandom1 * gRandom2 == gCorrAnswer && gRandom1 * gRandom2 == gResponse)
    {
        cout << down11 << over3 << "You are correct!!" << endl;
    }
    else if (gRandom1 / gRandom2 == gCorrAnswer && gRandom1 / gRandom2 == gResponse)
    {
        cout << down11 << over3 << "You are correct!!" << endl;
    }
    else if (gRandom1 % gRandom2 == gCorrAnswer && gRandom1 % gRandom2 == gResponse)
    {
        cout << down11 << over3 << "You are correct !!" << endl;
    }        
    else
    {
        cout << down11 << over3 << "You are incorrect!!" << endl;
    }
              
}              
void summary()
{   
     
    system ("CLS");
    cout << down11
         << over3 << "     Summary() was called!!\n" << endl 
         << over2 << "The following global variables will be displayed: \n" << endl
         << over3 << "    Total number correct:   " << gNum_Correct << endl
         << over3 << "    Total number incorrect: " << gNum_Incorrect << endl
         << over3 << "     Your average is: " << endl
         << down8;
    
    system ("CLS");    
}
void menu()//input from user
{
    system ("CLS");
        cout << down5
             << over3 << "     Welcome to Math Practice" << endl
             << over3 << "     ------------------------ " << endl
             << over2 << "  Please choose a selection that corresponds with" << endl
             << over2 << "    the type ofproblem you would like to solve.\n " << endl
             << over4 << " A. Addition" << endl
             << over4 << " B. Subtraction " << endl
             << over4 << " C. Multiplication " << endl
             << over4 << " D. Integer Division " << endl
             << over4 << " E. Modulus " << endl
             << over4 << " F. Exit the program \n" << endl
             << over3 << "  Please ENTER your selection here ";                               
             cin >> choice;
}
void menuError()
{
    system ("CLS");
    cout << down11
         << over3 << "You have chosen an invalid option." << endl
         << over3 << "Your choices are A,B,C,D,E, and F." << endl
         << over3 << "Please try again." << endl
         << down11;     
}
btw, you can use loop instead using lots of define...
Or even call a function that will print it out.
1
2
3
4
void down(int n) {
    for (int i = 0; i < n; i ++)
        cout << "\n";
}


And when you call Results, you need to pass four parameters. You're currently calling results with no parameters.
I am a little confused about the parameter thing. Can someone please give me a good example on using a parameter with what I have. In my last lab we did the stub and driver lesson and I totally understood it. I am sooo lost at this point.
equation(gRandom1, gRandom2, gRandom1 + gRandom2, '+', "Addition");

gRandom1, gRandom2, (gRandom1 + gRandom2), '+', "Addition" are all parameters in your equation function.

For your results function, your definition wants these four parameters
results(int gRandom1,int gRandom2,int gCorrAnswer,int gResponse)

So when you call results at the end of your main function, you need to pass 4 parameters to it.
Thanks for bearing with me. If I do this:void results(int gRandom1,int gRandom2,int gRandom1 + gRandom2 ,int gResponse) will that not mean that I have to have a different results function for each equation? One for addition, one for subtraction and so on? I think if I can full understand this part of the program the rest will be a lot easier for me.
Your error seems to be coming from line 101 of the file that you posted. It's because you pass nothing to the function results. The function results is expect 4 integers. That is why you get an error. The compiler is looking for a definition of results that has no parameters, but it doesn't exist.

If you go to line 101, put in 4 parameters, in this case, you want gRandom1, gRandom2, cCorrAnswer, and gResponse. This will make the compiler happy since you're passing four variables, and you'll be happy because you're passing them in the correct order.
Thanks a bunch. I think I now understand fully. I can move on to finishing with the summary and the final average part of the program.
This is the first time I have posted with this forum and I can guarantee you I will be doing more when I get in trouble.
Things that seem elementary to some are seen by others such as myself as obstacles that take some understanding in order to overcome. If I run into another issue, I will post again.
Thanks again!!
No a problem, and good luck.

I also completely understand about difficulties you have. I've been there before, and so have so many others. It helps me improve my skills, and I read just about every post on here to see about different ways to do things. A lot of the users here that help are extremely knowledgeable and helpful. I've also been working with others and trying to teach them, and it's a hassle since it's so simple for me, but patience really is a virtue in teaching.

Drinking helps too -.-
If I knew where you lived I'd send you a six pack. Thanks again for helping me out of a jam.
Topic archived. No new replies allowed.