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
|
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
// constants for menu choices
const char ADDITION = '1',
SUBTRACTION = '2',
EXIT = '3';
// declaring variables.
int addright = 0,
addwrong = 0,
addition = 0,
subright = 0,
subwrong = 0,
subtraction = 0;
int num1,
num2,
correctanswer,
studentanswer;
char category;
// used for random number generation.
unsigned seed = time(0);
srand(seed);
do
{
// display the main menu and input of category.
cout << setw(27) << " MENU " << endl;
cout << "---------------------------" << endl;
cout << "1: Enter 1 for Addition" << endl;
cout << "2: Enter 2 for Subtraction" << endl;
cout << "3: Enter 3 to Exit" << endl << endl;
cout << "Enter your selection: ";
cin >> category;
// validate menu selection.
while (category < ADDITION || category > EXIT)
{
cout << "Please enter a valid menu option: " << endl;
cin >> category;
}
// run program as long as exit isn't selected.
if (category != EXIT)
{
switch (category)
{
case ADDITION:
do
{
// random number generation for problems.
num1 = 1 + rand() % 9;
num2 = 1 + rand() % 9;
correctanswer = num1 + num2;
cout << "What is the sum of " << num1 << " and " << num2 << "?" << endl;
cout << "Enter your answer, or enter \"-1\" to return to the menu" << endl << endl;
cin >> studentanswer;
if (studentanswer != -1)
// if the student opts to try a problem, increment the addition problem counter.
++addition;
// if the student enters -1, they return to the main menu.
while (studentanswer != -1)
{
// if the student is correct, increment the addright counter, congratulate them and move to the next problem.
if (studentanswer == correctanswer)
{
addright++;
cout << "Very good!" << endl;
break;
}
else
{
// if the student is wrong, increment the addwrong counter tell them and let them try until they get it right.
if (studentanswer != correctanswer)
addwrong++;
cout << "No, please try again." <<endl;
cout << "Enter your answer, or enter \"-1\" to return to the menu" << endl << endl;
cin >> studentanswer;
}
}
} while (studentanswer != -1);
// provide results for their addition problems when they are completed.
cout << "Summary: " << endl;
cout << "Addition Problems Played: " << addition << endl;
cout << "Number of times answered correctly: " << addright << endl;
cout << "Number of times answered incorrectly: " << addwrong << endl << endl;
break;
case SUBTRACTION:
do
{
// random number generation for problems.
num1 = 10 + rand() % 9;
num2 = 1 + rand() % 9;
correctanswer = num1 - num2;
cout << "What is the difference between " << num1 << " and " << num2 << "?" << endl;
cout << "Enter your answer, or enter \"-1\" to return to the menu" << endl << endl;
cin >> studentanswer;
if (studentanswer != -1)
// if the student opts to try a problem, increment the subtraction problem counter.
subtraction++;
while (studentanswer != -1)
{
if (studentanswer == correctanswer)
{
// if the student is correct, increment the subright counter, congratulate them and move to the next problem.
subright++;
cout << "Very good!" << endl;
break;
}
else if (studentanswer != correctanswer)
{
// if the student is wrong, increment the subwrong counter tell them and let them try until they get it right.
subwrong++;
cout << "No, please try again." <<endl;
cout << "Enter your answer, or enter \"-1\" to return to the menu" << endl << endl;
cin >> studentanswer;
}
}
} while (studentanswer != -1);
// provide results for their subtraction problems when they are completed.
cout << "Summary: " << endl;
cout << "Subtraction Problems Played: " << subtraction << endl;
cout << "Number of times answered correctly: " << subright << endl;
cout << "Number of times answered incorrectly: " << subwrong << endl << endl;
}
}
} while (category != EXIT);
return 0;
}
|