Variable always equal to 0.

Hello! I am trying to make a fraction calculator for an assignment. In summary, one of my variables (totalPercent) is set to equal a value (correct) divided by a total. However, despite any alterations I have done, the variable is always equal to 0. For instance, if I get 2 out 3 problems correct I will be given 0.00% However, if I make the program display the value for each individual variable only totalPercent gives the incorrect output that I am expecting.

My directions for this particular area of the program is:

The program must be able to display the number of correct answers and percentage with 2 digits after the decimal point.

Please see below for my 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
	//2 Decimal Point Representation
	cout << fixed << showpoint << setprecision(2); 

	int correct = 0;
	int total = 3;
	double totalPercent;
	totalPercent = static_cast<double> (correct/total) * 100;
	cout << "You answered " << correct << " of " << "3" << " problems correctly " << " (" << totalPercent << ")" << endl;

// Please know that the variable correct is referenced in another function
// Function : fractionComparison
// Description: Compare the answer given by the user with the answer calculated by the program.
int fractionComparison (int num1, int num2, int den1, int den2, int num3, int den3,int answerUser1, int answerUser2, char opSign, double &floatPoint, int &correct)
{
	
	

	if (num3 == answerUser1, den3 == answerUser2)
	{
		cout << "Great Job! Your answer was correct!" << endl;
		cout << endl;
		cout << num1 << "/" << den1 << opSign << num2 << "/" << den2 << "=" << num3 << "/" << den3 << endl;
		cout << endl;
		floatPoint = static_cast<double> (num3) / den3;
		cout << "The fraction's Floating-point Value is: " << floatPoint << endl;
		cout << endl;
		correct = correct++;
	}
	else 
	{


I apologize in advance if I may have broken some rule that I haven't read yet.
Thank you for your time!

**Edit
I should add that these code segments are taken out of context to be easier on the eyes.
Last edited on
Are lines 1 - 8 the final lines of your program? Because you set correct equal to 0 on line 4, so when you calculate total percent on line 7 the result would be 0/3, which gives you 0.
Sorry! There are a few chunks of code between line 8 and line 10. I reduced the amount of code to be easier on the eyes.

I have a while loop that runs just before totalPercent:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	while (keepGoing != '%')
	{
		while (count <=3)
		{
			cout << "Please enter your operation -->" << endl;
			cout << endl;
			cin >> num1 >> slash >> den1 >> opSign >> num2 >> slash >> den2;
			cout << "Please enter your result -->" << endl;
			cout << endl;
			cin >> answerUser1 >> slash >> answerUser2;
			// Function Calls
			fractionCalc (num1, num2, num3, den1, den2, den3, opSign);
			fractionComparison (num1, num2, den1, den2, num3, den3, answerUser1, answerUser2, opSign, floatPoint, correct);
			count = count++;
		}
		totalPercent = (correct/total) * 100;


where correct = correct++ occurs in the function fractionComparison.

Sorry if I have been confusing. I wasn't sure if I was allowed to post the entire code.
Last edited on
Ah! I see it now. Line 7 of what you originally posted:
totalPercent = static_cast<double> (correct/total) * 100;
It isn't being cast correctly. correct/total will evaluate first before it is cast a double. That means it will follow the rules of integer division, so 2/3 will evaluate to 0 as it has been doing. Try something like casting one of them as a double:
totalPercent = static_cast<double>correct / total * 100;

(Also, when you use the increment operator, you only need to use it by itself. You don't need count = count++. count++ will do the same exact thing in fewer keystrokes).
Last edited on
Thank you so much. I personally don't like to leave problems in the wind and this problem, in particular, has stressed me out.

I've tested the code just now and it works wonderfully.

Thank you also for your tip for "count++"!

Please have a wonderful day or night! You certainly have my appreciation!
Topic archived. No new replies allowed.