My program is not calculating sums correctly

I am getting this weird error when calculating sums. Here is what my program does: it generates two random numbers and then randomly puts either an addition or subtraction sign between the two and then I am asked to enter what the correct answer for that question is. But on some of the subtraction questions when I give the answer it says I got it wrong after which the program shows the right answer. What's confusing is that the answer the computer calculates is the same that I entered. Anyways the complete program and a screenshot of the console window which shows the error are below. The function in question here is decAddSub at line 54. The error is shown in the first sum in the output window. It doesn't affect addition problems and only affects some of the subtraction problems.
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
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<random>



using std::cout;
using std::cin;
using std::endl;

double getRandomNumber(int x, int y);
double randomNumberGenerator(int x, int y);
void decAddSub(void);
int main()
{
	char repeat = ' ';
	do
	{
		system("cls");
		repeat = ' ';
		decAddSub();
		cout << "Press y to do some more questions or press any key to exit the program ";
		cin >> repeat;
	} while (repeat == 'y' || repeat == 'Y');

}
double getRandomNumber(int x,int y)
{
	double integer = randomNumberGenerator(x,y);
	double fraction = randomNumberGenerator(x,y);
	double number = integer + fraction / 100;
	return number;

}
double randomNumberGenerator(int x, int y)
{

	std::random_device rd;
	std::default_random_engine generator(rd()); // rd() provides a random seed
	std::uniform_int_distribution<int> distribution(x, y);

	double number = distribution(generator);
	
	return number;
}
struct question
{
	double num1;
	double num2;
	char operation;
	bool correct;
};
void decAddSub(void)
{
	int numberOfQuestions(0), count(0); //number of question the user wants to do and the count of how many are correctly answered
	question* pquestions(nullptr); 
	cout << "How many questions would you like to do? ";
	cin >> numberOfQuestions;
	pquestions = new question[numberOfQuestions];
	for (int i = 0; i< numberOfQuestions; i++)
	{
		if (getRandomNumber(-10,10)<0)
			*(pquestions + i) = { getRandomNumber(0, 100), getRandomNumber(0, 100), '-', false };
		else
			*(pquestions + i) = { getRandomNumber(0, 100), getRandomNumber(0, 100), '+', false };
	}

	for (int i = 0;i< numberOfQuestions; i++)
	{
		double answer1(0); //correct answer
		double answer2(0); // answer entered by the user
		if (pquestions[i].operation == '+')
			answer1 = pquestions[i].num1 + pquestions[i].num2;
		else if (pquestions[i].operation == '-')
			answer1 = pquestions[i].num1 - pquestions[i].num2;
		cout << pquestions[i].num1 << ' ' << pquestions[i].operation << ' ' << pquestions[i].num2 << ' ' << '=' << ' ';
		cin >> answer2;
		
		if (answer1 == answer2)
		{

			pquestions[i].correct = true;
			cout << "correct \n";
		}
		else
			cout << "incorrect, the correct answer is " << answer1<<endl;
	}

	for (int i = 0; i < numberOfQuestions; i++)
	{
		if (pquestions[i].correct)
			count++;
	}
	cout << "You're score is " << count << ' ' << '/' << ' ' << numberOfQuestions << ' ' << 'o' << 'r' << ' ' << count*1.0 / numberOfQuestions* 100*1.0 << '%' << endl;
	delete[] pquestions;
	pquestions = nullptr;
	return;
}

How many questions would you like to do? 5
65.86 - 80.83 = -14.97
incorrect, the correct answer is -14.97
70.37 + 15.08 = 85.45
correct
71.16 + 69.33 = 140.49
correct
19.61 - 53.73 = -34.12
correct
10.12 + 58.02 = 68.14
correct
You're score is 4 / 5 or 80%
Press y to do some more questions or press any key to exit the program Press any
 key to continue . . .
Last edited on
I had a hunch that floating point numbers might run into such a problem at higher decimal places but I never paid any mind to it. The link explains the problem well but I couldn't understand the solutions very well. Would changing the if statement at line 80 as follows do the trick?
1
2
3
4
5
6
7
8
9
if (abs(answer1-answer2)<0.001)
		{

			pquestions[i].correct = true;
			cout << "correct \n";
		}
		else
			cout << "incorrect, the correct answer is " << answer1<<endl;
	}
Last edited on
Topic archived. No new replies allowed.