Function call

This program is designed to help students practice addition and subtraction. It works the first time through, but for some reason it doesn't pull the sum1 function the second time. Can someone look at it and tell me where my error is?
Any help would be much appreciated.



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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctime>
#include <cstdlib>
using namespace std;

void pick(char& );
bool eof( char  );
bool addition (char );
void sum1(int , int , int , int& , int ,int );
void diff(int , int , int , int&, int , int );
void pause();
int main()
{
    char choice, add='A', sub='S', decision;
    int sum, difference, num1, num2, counter=1, answer, correct=0;
 


    pick (decision);
    while ( !eof(decision))
    {

        if(addition (decision))
        {

            sum1(num1,num2,sum,counter, answer, correct);
        }
        else
        {
            diff(num1, num2,difference, counter, answer, correct);
        }
        pick (decision);

    }

    pause();
}
//*****************************************
void pick(char& one)
{
	char add='A', sub='S', quit='Q';
	cout<<"Would you like to practice addition or subtraction? \n\n";

	cout<<"\nEnter an A to practice addition or an S to practice subtraction.\n\n"
        <<"\nEnter a Q to quit.\n";
	cin>>one;
    one = toupper(one);
    while(one != quit && one != add && one != sub)
    {
        cout<<"Please enter an A to practice addition or an S to practice subtraction.\n";
        cin>>one;
        one=toupper(one);
    }

    if(one==add)
        cout<<"\nYou have chosen to practice addition.\n\n";


    if(one==sub)
        cout<<"You have chosen to practice subtraction.\n";
    pause();
    system("CLS");


}
//*********************************************
bool eof( char  choice)
{
	if( toupper(choice) == 'Q' )
		return true;
	else
		return false;
}
//******
bool addition (char choice)
{
     char add='A';
    if( choice == add)
        return true;
    else
        return false;

}

//****************
void sum1(int x, int y, int z, int& counter, int answer, int right)
{
    srand(time(0));
    while (counter < 11)
    {
        x=(1+(rand()%100));
        y=(1+(rand()%100));
        z=x+y;
        cout<< x <<" + "<< y << " = ?\n";


        cin>>answer;

        if (answer==z)
        {
            cout<<"Congratulations! That is correct!\n\n";
            right++;
            pause();
            system("CLS");
        }
        else
        {
            cout<<"I'm sorry, that is incorrect.  Please try again.\n\n";
            cin>>answer;
                if(answer==z)
                {
                    cout<<"Congratulations! That is correct!\n\n";
                    right++;
                    pause();
                    system("CLS");
                }
                else
                {
                    cout<<"I'm sorry that is incorrect.  The correct answer was "<<z<<endl;
                    pause();
                    system("CLS");

                }

        }

        counter ++;

    }
    cout<<"You got "<<right<<" out of 10 correct!"<<endl;
    pause();
    system("CLS");
}


//***************************
void diff(int x, int y, int z, int& counter, int answer, int right)
{

    srand(time(0));

    while (counter < 11)
    {
        x=((1+rand()%100));
        y=((1+rand()%100));
        z=x-y;

        cout<< x <<" - "<< y << " = ?\n"<<endl;
        cin>>answer;

        if(answer==z)
        {
            cout<<"Congratulations! That is correct!\n\n";
            right++;
            pause();
            system("CLS");
        }
        else
        {
            cout<<"I'm sorry, that is incorrect. Please try again.\n\n";
            cin>>answer;
            if(answer==z)
            {
                cout<<"Congratulations! That is correct!\n\n";
                right++;
                pause();
                system("CLS");
            }
            else
            {
                cout<<"I'm sorry that is incorrect.  The correct answer was "<<z<<endl;
                pause();
                system("CLS");
            }

        }
        counter ++;
    }
    cout<<"You got "<<right<<" out of 10 correct!"<<endl;
    pause();
    system("CLS");
}
//**************************
void pause()
{
     char ch;

     printf("\nPress ENTER to continue..");
     do
     {
          ch = getch();
     }while(ch != 13);

     printf("\n\n");

     return;
}

Last edited on
hi, please put your code inside the [code] tags, for easier visualization..
I'm so sorry, I'm really new at this.

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
 char choice, add='A', sub='S', decision;
    int sum, difference, num1, num2, counter=1, answer, correct=0;
 


    pick (decision);
    while ( !eof(decision))
    {

        if(addition (decision))
        {

            sum1(num1,num2,sum,counter, answer, correct);
        }
        else
        {
            diff(num1, num2,difference, counter, answer, correct);
        }
        pick (decision);

    }

    pause();
}
//*****************************************
void pick(char& one)
{
	char add='A', sub='S', quit='Q';
	cout<<"Would you like to practice addition or subtraction? \n\n";

	cout<<"\nEnter an A to practice addition or an S to practice subtraction.\n\n"
        <<"\nEnter a Q to quit.\n";
	cin>>one;
    one = toupper(one);
    while(one != quit && one != add && one != sub)
    {
        cout<<"Please enter an A to practice addition or an S to practice subtraction.\n";
        cin>>one;
        one=toupper(one);
    }

    if(one==add)
        cout<<"\nYou have chosen to practice addition.\n\n";


    if(one==sub)
        cout<<"You have chosen to practice subtraction.\n";
    pause();
    system("CLS");



Is that better?
Last edited on
If you edit your post, highlight your code and then click on the <> button to the right side of the post in the Format palette, that'll format your code for the forum and make it a lot easier for others to read :)
Oh ok, Thank you :)
The counter continues where it left off after the first ten so when you get here in the sum function (and probably same thing in diff)

while (counter < 11)

the counter value is already at 11 and it skips all the code in the while loop.

THANK YOU SO MUCH!!!!
I changed the call to pass by value and it fixed the problem.
I really appreciate your help
OK, glad it's working for you now.

I guess I would usually keep a counter local to the specific function and not even bother passing it from another one. Unless there's some particular reason the main function needs to know about the counter value.
That is an excellent point. Thank you for the advice.
Topic archived. No new replies allowed.