Help me

well i have 2 new problems...

1. when i play again the percentage is increasing to 160% 180% and so on... why is tat so.. i know its something to do wit my "per=" code but im not preety sure how to edit it.. please teach me..

2. if let say i remove the srand() and paste it out side its showing error..
and if i remove for the rest and remain it only on void add()... the subract and the rest is not generating random number...

the code..
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;
int correctcounter=0;
int incorrectcounter=0;
float per;        
static int number;

void add()
{
         unsigned seed = time(0);    
         srand(seed);
         int A = rand()%20+1;
         int B = rand()%20+1;
        
         int result=0;

         cout<<A<<" + "<<B<<" = ?"<<endl;
         cout<< "Answer : ";
         cin>>result;
         cout<<endl;
        
         if(result == A+B)
         {
         correctcounter++;
         cout<<"CORRECT!! Well done!! "<<endl<<endl;
         cout<<"----------------------------------------------------"<<endl;
        
         }
         if(result !=A+B)
         {
         incorrectcounter++;
         cout<<"WRONG!! The answer is "<<A+B<<endl<<endl;
         cout<<"----------------------------------------------------"<<endl;
         }
}

void subtract()
{
        unsigned seed = time(0);    
        srand(seed);
        int A = rand()%20+1;
        int B = rand()%20+1;
        
        int result=0;

        cout<<A<<" - "<<B<<" = ?"<<endl;
        cout<< "Answer : ";
        cin>>result;
        cout<<endl;
        
        if(result == A-B){
        correctcounter++;
        cout<<"CORRECT!! Well done!! "<<endl<<endl;
        cout<<"----------------------------------------------------"<<endl;
        
        }
        if(result !=A-B){
        incorrectcounter++;
        cout<<"WRONG!! The answer is "<<A-B<<endl<<endl;
        cout<<"----------------------------------------------------"<<endl;
        }
}

void multiply()
{
        int result=0;

        unsigned seed = time(0);    
        srand(seed);
        int A = rand()%20+1;
        int B = rand()%20+1;
        
        cout<<A<<" * "<<B<<" = ?"<<endl;
        cout<< "Answer : ";
        cin>>result;
        cout<<endl<<endl;
        
        if(result == A*B){
        correctcounter++;
        cout<<"CORRECT!! Well done!! "<<endl<<endl;
        cout<<"----------------------------------------------------"<<endl;
        
        }
        if(result !=A*B){
        incorrectcounter++;
        cout<<"WRONG!! The answer is "<<A*B<<endl<<endl;
        cout<<"----------------------------------------------------"<<endl;
        }

}

void divide()
{
        int result=0;

        unsigned seed = time(0);    
        srand(seed);
        int A = rand()%20+1;
        int B = rand()%20+1;
        if (B>A)
            {
              int temp = A;
              A = B;
              B = temp;
             }
        
        cout<<A<<" / "<<B<<" = ?"<<endl;
        cout<< "Answer : ";
        cin>>result;
        
        if(result == A/B){
        correctcounter++;
        cout<<"CORRECT!! Well done!! "<<endl;
        cout<<"----------------------------------------------------"<<endl;
        
        }
        if(result !=A/B){
        incorrectcounter++;
        cout<<"WRONG!! The answer is "<<A/B<<endl;
        cout<<"----------------------------------------------------"<<endl;
        }
}
char displayMenu();
int Selection;
char displayMenu()
 {   
     cout << "Select a topic"<<endl;
     cout <<endl;  
          cout << "1.Addition"<<endl;
          cout << "2.Subtraction"<<endl;   
          cout << "3.Multiplication"<<endl;
          cout << "4.Division"<<endl<<endl;
          cout << "Topic No. : ";
     cin >> Selection;
     
     system("cls");
}  
int main()
{
  char again;
  do
  {  displayMenu();
     switch(Selection){		//	Switchcase on said number
		case 1:
            for(int i=1;i<=5;i++) 
            {  
                cout << "QUESTION " << i<< endl<< endl;
    	    	add();	//	If 1; addition
            }
    	    	cout<< "CORRECT ANSWER : "<< correctcounter<<endl;
    	    	cout<< "WRONG ANSWER : "<< incorrectcounter<<endl<<endl;
    	    	per=(correctcounter*100)/5;
    	    	cout<<"Percentage is "<<per<<"%"<<endl<<endl;
            break;
        
		case 2:
            for(int i=1;i<=5;i++) 
            {  
                cout << "QUESTION " << i<< endl<< endl;
    			subtract(); //	If 2; subtraction
            }
    	    	cout<< "CORRECT ANSWER : "<< correctcounter<<endl;
    	    	cout<< "WRONG ANSWER : "<< incorrectcounter<<endl<<endl;
    	    	per=(correctcounter*100)/5;
    	    	cout<<"Percentage is "<<per<<"%"<<endl<<endl;

            break;
            
		case 3:
            for(int i=1;i<=5;i++) 
            {  
                cout << "QUESTION " << i<< endl<< endl;
    			multiply(); //	If 3; multiplication
    		}
    	    	cout<< "CORRECT ANSWER : "<< correctcounter<<endl;
    	    	cout<< "WRONG ANSWER : "<< incorrectcounter<<endl<<endl;
    	    	per=(correctcounter*100)/5;
    	    	cout<<"Percentage is "<<per<<"%"<<endl<<endl;

            break;
        
		case 4:

            for(int i=1;i<=5;i++) 
            {  
                cout << "QUESTION " << i<< endl<< endl;
    			divide(); //	If 4; division
    		}
    	    	cout<< "CORRECT ANSWER : "<< correctcounter<<endl;
        	    cout<< "WRONG ANSWER : "<< incorrectcounter<<endl<<endl;
        	    per=(correctcounter*100)/5;
    	    	cout<<"Percentage is "<<per<<"%"<<endl<<endl;

            break;
            
        default:
			cout << "ERROR: Please enter the topic number from 1-4." << endl<<endl;
            
            
      	}
    cout << "Do you want to play again? (y/n)";
    cin >> again;
           system("cls");
  } while (again == 'y');


   

    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on
1. You never reset correctcounter and incorrectcounter.

2. Call srand once, at the beginning of main().
should i add like this before the main...

unsigned seed = time(0);
srand(seed);

and,
how do i write code to reset...

im searching online but cant find anything...
srand should be called from inside main but before the loop starts.

By reset I mean set correctcounter and incorrectcounter to zero.
1
2
correctcounter = 0;
incorrectcounter = 0;
thank sooooo muchh... i really appreciate your help alot... thank you...

have a nice day... :D
Topic archived. No new replies allowed.