Hi everyone. I am new to C++, and I have an assignment I could use some feedback on. This program is basically a simple math tutor program that lets you practice basic division, subtraction, multiplication and division. Each function generates 10 problems, with numbers from 1 to 12. Everything in this code seems to be running okay, except one small part. One of the requirements is to have the score displayed at the end, and I can't get it to return properly. Either I get nothing, or when I try to print it to the screen, it looks to be giving me a garbage value. I only added my addition function to make this a little easier to read. I don't know why it's doing this.
Line 92: What score is returned if the user got the answer wrong?
Hint: The compiler will supply a value of 0.
Line 62: This is not the correct place to call srand(). srand() should be called once at the beginning of main(). Calling srand() inside a loop can cause the the RNG to be reset to return the same sequence of pseudo-random numbers if called within the same second.
Seed the random number generator (Srand(time(0));
I added a while loop to keep your program cycling as long as dummy==0
Switch / case always always always gets a default.
Other than that good job!
Okay, so I moved the srand() out of the loop and into the main () function. That part makes sense as it seems a bit tedious to have to add another srand () to every function. I have to use a do-while (requirement for project), and I like them because I can see my program run at least once. I still can't get the score output to work properly. It doesn't make sense to me. When it says "you're score is " << score, the output is something like "28222896". Why is that?
void addition();
void displayMenu();
int isCorrect(int userGuess, int correctAnswer, int currentScore);
using namespace std;
int main()
{
srand(time(0));
enum choice { EXIT = 0, ADD = 1, SUBT, MULT, DIV }; // stores each case from switch statement for easy memorization.
int selection; // stores users selection.
int dummy;
do{
displayMenu();
cin >> selection;
switch (selection)
{
case ADD:
{
int score;
addition();
//score = isCorrect;
break;
}
}
} while (selection != EXIT);
cin >> dummy;
}
void displayMenu()
{
//int selection;
cout << "Welcome to to Nick's Math Lab!\n";
cout << "\n";
cout << "1 - Addition" << endl;
cout << "2 - Subtraction" << endl;
cout << "3 - Multiplication" << endl;
cout << "4 - Division" << endl;
cout << endl;
cout << "Please make a selection (TYPE 0 TO QUIT): ";
}
void addition()
{
//cout << "this is addition";
int rnum1; // stores random number
int rnum2; // stores another random number
int answer;
int score = 0;
for (int i = 1; i < 11; i++)
{
cout << "\n";
cout << "ADDITION";
cout << "\n";
rnum1 = 1 + (rand() % 12); //generates number between 1 and 12
rnum2 = 1 + (rand() % 12);
cout << endl;