school assignment - random number question

Hello, I'm working on an assignment for school and was just having some trouble with it. In particular, I'm having trouble with generating a random number between 1-4, then assigning that number to a basic math operator to be used in an equation, comparing two randomly generated integers.

Here is the code:

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
#include<iostream>
#include<conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

class MathProblem
{
private:
	
	int first;
	int second;
	int getOper;  //integers to be used throughout
	int answer;
	int correct;
	char oper;
	
public:

	void showOther();
	void showProblem();
	void setProblem();  //declare the functions
	void getAnswer();
};	

void MathProblem::setProblem()
{

	srand(time(0));
	int num1 = rand() % 60 + 12;   //generate  first operand
	int num2 = rand() % 10 + 1;   //generate second operand
	int getOper = rand() % 5 + 1; //generate operator number
	first = num1;
	second = num2;
	char operate;
	
	if (getOper == 1)
	{
		operate = '+';
	}
	if (getOper == 2)
	{
		operate = '-';
	}
	if (getOper == 3)
	{
		operate = '*';
	}
	if (getOper == 4)
	{
		operate = '/';
	}	
	
	
}

void MathProblem::showProblem()
{
	cout << first << oper << second << " = " << endl;
	
	if (getOper == 1) //for +
	{
	correct = first + second;
	}
	if (getOper == 2) //for -
	{
	correct = first - second;
	}
	if (getOper == 3) //for *
	{
	correct = first * second;
	}
	if (getOper == 4) //for /
	{
	correct = first / second;
	}
}

void MathProblem::getAnswer()
{
	cin >> answer;
}

void MathProblem::showOther()
{

	cout << "Your answer was: " << answer << endl;
	cout << "The correct answer was: " << correct << endl;
	cout << "Were you correct? ";
	if (answer == correct)
	{
		cout << "1";
	}
	else
	{
		cout << "0";
	}
}

int main()
{
	MathProblem problem;
	problem.setProblem();
	problem.showProblem();
	problem.getAnswer();
	problem.showOther();
	getch();
}


My integers are randomly generated each time, however my operator between them is null data, and my "correct answer was: " line yields something like -8980375 every time (I've fixed this error before, but I can't seem to think of what's causing it this time)

Also, any other pointers you happen to have would be greatly appreciated.

Thanks!

edit: added some comments
Your set problem function is incorrect.

Why don't you just assign your results directly to the member variables instead of putting them in temporary then immediately assigning them somewhere else?

As for your actual problem, you are assigning your operation to a NEW, local, temporary variable that is not the same as getOper in your class. This would be easier to see if you didn't create these unnessecary temporary variables.
bah of course...pretty much everything was a result of a lot of trial and error from earlier that I forgot to clean up.

Sorry to waste your time, but thank you, that would have taken me a very long time to figure out on my own.

Thank you, it's working fine now!
Topic archived. No new replies allowed.