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
|
#include <iostream>
using namespace std;
int main()
{
float num1, num2, num3; //defines my two number variables,
//and my answer.
float ans; //defines the user's answer as a var.
int num0; //defines my menu choice
do //This repeats the menu until the user chooses the quit option.
{
//Menu, with addition subtraction multiplication and division problems //choices.
cout << " Menu" << endl;
cout << "1. Addition Problem" << endl;
cout << "2. Subtraction Problem" << endl;
cout << "3. Multiplication Problem" << endl;
cout << "4. Division Problem" << endl;
cout << "5. Quit" << endl;
cout << "Please enter the number of the type of math problem you would like to practice." << endl;
cin >> num0;
while (num0 != 1 && num0 != 2 && num0 != 3 && num0 != 4 && num0 != 5 && num0 != 6) //This should make anything not a 1-6 become a 6.
{
num0 = 6;
}
num1 = 1 + rand() % 500; //Ranomly generates num1 1-500
num2 = 1 + rand() % 500; //Randomly generates num2 1-500
if ( num0 == 1) //Addition problem begins here
{
cout << "Please enter your answer below" << endl;
cout << num1 << endl;
cout << "+" << num2 << endl;
cout << "_____" << endl;
cin >> ans;
num3 = num1 + num2;
if (num3 == ans) //If statement that if answer is correct, user will see congratulatory message,
//otherwise will see the correct answer.
cout << "Congratulations, " << num3 << " is the correct answer!" << endl;
else
cout << "Sorry, the correct answer is " << num3 << "." << endl;
}
else if ( num0 == 2) //Subtraction problem begins here
{
cout << "Please enter your answer below" << endl;
cout << num1 << endl;
cout << "-" << num2 << endl;
cout << "_____" << endl;
cin >> ans;
num3 = num1 - num2;
if (num3 == ans) //If statement that if answer is correct, user will see congratulatory message,
//otherwise will see the correct answer.
cout << "Congratulations, " << num3 << " is the correct answer!" << endl;
else
cout << "Sorry, the correct answer is " << num3 << "." << endl;
}
else if ( num0 == 3) //Multiplication problem begins here
{
num1 = 1 + rand() % 10; //Randomly generates a number 1-10, easier for multiplication
num2 = 1 + rand() % 10; //Randomly generates a number 1-10
cout << "Please enter your answer below" << endl;
cout << num1 << endl;
cout << "*" << num2 << endl;
cout << "_____" << endl;
cin >> ans;
num3 = num1 * num2;
if (num3 == ans) //If statement that is answer is correct, user will see congratulatory message,
//otherwise will see the correct answer.
cout << "Congratulations, " << num3 << " is the correct answer!" << endl;
else
cout << "Sorry, the correct answer is " << num3 << "." << endl;
}
else if ( num0 == 4) //Division problem begins here
{
num1 = 1 + rand() % 10; //Randomly generates a number 1-10, easier for division
num2 = 1 + rand() % 10; //Randomly generates a number 1-10
cout << "Please enter your answer below" << endl;
cout << num1 << endl;
cout << "/" << num2 << endl;
cout << "_____" << endl;
cin >> ans;
num3 = num1 / num2;
if (num3 == ans) //If statement that if answer is correct, user will see congratulatory message,
//otherwise will see the correct answer.
cout << "Congratulation, " << num3 << " is the correct answer!" << endl;
else
cout << "Sorry, the correct answer is " << num3 << "." << endl;
}
else if ( num0 == 5) //Program quit
{
}
else if (num0 != 5 || num0 != 1 || num0 != 2 || num0 != 3 || num0 !=4) //Tells user to enter a valid menu choice
//This should let the user enter in a new choice, but it doesn't!
{
cout << "Please enter a valid menu number to continue." << endl;
cin >> num0;
}
}
while (num0 != 5); //This closes the do loop
return 0;
}
|