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
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// Ask the user for the answer to x+y, x-y, x*y or x/y, depending on
// whether operation is 1, 2, 3, or 4. Return when they get it
// right, or quit (by entering -1 for their answer
void askQuestion(int x, int y, char operation)
{
int answer; // answer to problem
int guess; // user's guess
const char *prompts[4] = {"plus",
"minus",
"times",
"divided by"};
// Determine the answer
switch (operation) {
case 1: answer = x+y; break;
case 2: answer = x-y; break;
case 3: answer = x*y; break;
case 4: answer = x/y; break;
}
// Now ask them until they get it right or enter -1
while (true) {
cout << "How much is " << x << ' ' << prompts[operation-1]
<< ' ' << y << "?\n";
cout << "Enter your answer (-1 to return to menu): ";
cin >> guess;
if (guess == -1) {
return; // user quit
} else if (guess == answer) {
cout << "Very Good!\n";
return;
} else {
cout << "No. Please try again.\n";
}
}
}
int main(int argc, char *argv[])
{
int operation;
int aaa;
int bbb;
srand((unsigned)time(0));
while (true) {
cout << "*******Welcome to the Arithmetic quiz********\n";
cout << "MENU:\n\nEnter 1 for Addition\n";
cout << "Enter 2 for Subtraction\n";
cout << "Enter 3 for Multiplication\n";
cout << "Enter 4 for Division\n";
cout << "Enter 5 to Exit\n\n";
cin >> operation;
if (operation == 5) {
break; // exit
}
aaa = rand() % 99 + 10;
bbb = rand() % 99 + 10;
askQuestion(aaa, bbb, operation);
}
return 0;
}
|