Newbie here needing some help

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.

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
#include <iostream> 
#include <cstdlib>
#include <ctime> 

void addition();
void displayMenu();
int isCorrect(int userGuess, int correctAnswer, int currentScore);

using namespace std; 

int main()
{
	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 is a useless variable I need to add because Visual Studio 
       // ends the program once you complete 10 problems. 
	int dummy; 


	displayMenu();
	cin >> selection;

	
	switch (selection)
	{
	case ADD:
	{
		addition();
		break;
	}
	}
	cin >> dummy; // just insert any number here. 
}

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";

		srand(time(0));
		rnum1 = 1 + (rand() % 12); //generates number between 1 and 12
		rnum2 = 1 + (rand() % 12);
		cout << endl;

		cout << rnum1 << "+" << rnum2 << " = ";
		cin >> answer;
		score = isCorrect(rnum1 + rnum2, answer, score);
		cout << "\n";

	}
	cout << "your score is " << score << endl;


	return;

}
int isCorrect(int userGuess, int correctAnswer, int currentScore)
{
	//int currentScore = 0; 

	if (userGuess == correctAnswer)
	{
		cout << "Alright! You got it right! :D ";
		return currentScore += 10;

	}

	else
	{
		cout << "Sorry, you got it wrong :(";
	}

}
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!



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
#include <iostream>
#include <cstdlib>
#include <ctime>

void addition();
void displayMenu();
int isCorrect (int userGuess, int correctAnswer, int currentScore);

using namespace std;

int main()
{
	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 now has a purpose. It recycles your game.
	int dummy;

	while (dummy!=0){                      //While dummy doesn't = 0  the program repeats.
	displayMenu();
	cin >> selection;
	if (selection==0){dummy=0;}     // Game will end when 0 is selected
	else{
	switch (selection)
	{
	case ADD: addition();
	break;cout << "Sorry, you got it wrong :(";

	default: cout <<"People always forget me! Boo hoo" << endl;
	}

}
	}
}
void displayMenu()
{
    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 << "0 - Quit" << endl;          // Give them the option
	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;
	srand (time (0));              // To make numbers more random
	for (int i = 1; i < 11; i++)
	{
		cout << "\n";
		cout << "ADDITION";
		cout << "\n";

		srand(time(0));
		rnum1 = 1 + (rand() % 12); //generates number between 1 and 12
		rnum2 = 1 + (rand() % 12);
		cout << endl;

		cout << "Problem #"<<i <<"     "<< rnum1 << "+" << rnum2 << " = ";
		cin >> answer;
		score = isCorrect(rnum1 + rnum2, answer, score);
		cout << "\n";

	}
	cout << "your score is " << score << endl;
	return;

}
int isCorrect(int userGuess, int correctAnswer, int currentScore)
{
    if (userGuess == correctAnswer)
	{
		cout << "Alright! You got it right! :D ";
		return currentScore += 10;
	}
	else
	{
		cout << "Sorry, you got it wrong :(";
	}
return currentScore;
}
Last edited on
@pearlyman:

line 16: dummy is an uninitialized variable. It contains garbage. possibly 0.

line 18: This loop will not execute if dummy happens to contain 0. Poor practice to have uninitialized variables.

Line 26: The cout will never get executed after a break.

Line 55: Although you moved srand() out of the loop, it still should be in main(), not addition().


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?


#include <iostream>
#include <cstdlib>
#include <ctime>

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;

cout << "Problem #" << i << " " << rnum1 << "+" << rnum2 << " = ";
//cout << rnum1 << "+" << rnum2 << " = ";
cin >> answer;
score = isCorrect(rnum1 + rnum2, answer, score);
cout << "\n";

}
cout << "your score is " << score << endl;


return;

}
int isCorrect(int userGuess, int correctAnswer, int currentScore)
{
//int currentScore = 0;

if (userGuess == correctAnswer)
{
cout << "Alright! You got it right! :D ";
return currentScore += 10;

}

else
{
cout << "Sorry, you got it wrong :(";
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int isCorrect(int userGuess, int correctAnswer, int currentScore)
{
    if (userGuess == correctAnswer)
    {
        cout << "Alright! You got it right! :D ";
        return currentScore += 10;
    }

    else
    {
        cout << "Sorry, you got it wrong :(";
        return currentScore ; // *** added
    }
}
If you want Visual Studio to stop closing after finishing, compile without debugging (Ctrl + F5).
Alternatively you can do
1
2
3
4
cout << "Press enter to exit..." << endl;
//You can use either. getchar() requires you to include cstdio
//cin.get();
//getchar(); 

Last edited on
@ AbstractionAnon

Thanks... I should either put the crack pipe down, or not debug@ 3:30 am.
:) thanks for catching those.
Topic archived. No new replies allowed.