while loop..
Aug 18, 2014 at 2:29pm UTC
Attached is the code for creating a school arithmetic operation game.
well, i would like to add a code so that after 1 game is finished it it can ask " do you want to play again".. if yes it directs to the menu while if no the it exits the game.. please help me..
i know we have to use the while code.. but im juz not preety sure how to apply those codes here..
while code or do-while code..???
exactly where should i add the code... in main or anywhere... ((CONFUSED))
THANKING YOU IN ADVANCE.. :D
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
#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;
}
}
int main()
{
int Selection;
char displayMenu();
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" );
}
switch (Selection){ // Switchcase on said number
case 1:
for (int i=1;i<=5;i++)
{
number++;
cout << "QUESTION " << number<< 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++)
{
number++;
cout << "QUESTION " << number<< 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++)
{
number++;
cout << "QUESTION " << number<< 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++)
{
number++;
cout << "QUESTION " << number<< 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;
}
system("PAUSE" );
return EXIT_SUCCESS;
}
Aug 18, 2014 at 2:45pm UTC
Line 134: This is a function prototype. It belongs outside main.
Lines 136-148: C++ does not support nested functions. Moves to after line 130. Lose the ;
Lines 18, 47, 76,104: Only ever call srand() ONCE in your program.
Everything from lines 149 to line 210 should be in your while loop.
1 2 3 4 5 6 7
char again;
do
{ displayMenu();
// .... Lines 149 - 210
cout << "Do you want to play again?" ;
cin >> again
} while (again == 'y' );
Aug 19, 2014 at 4:29am UTC
thanks for the help @AbstractionAnon..
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 after editting..
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
#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;
}
Aug 19, 2014 at 7:55am UTC
i've found the solution thank you very much for your help... i really appreciate it... :D
Topic archived. No new replies allowed.