Very strange type casting issue

Hi, every part of my code is working until I ran into a very strange issue with the code recognition. I put in 10 very low homework grades (scale of 100)

For this code, it is supposed to pick out the higher grade of two different grading schemes. The first one is 30% midterm, 40%final, and 30% homework. 2nd scheme is midterm dropped, 70% final, and 30% homework. The code below is the ending part checking for the schemes. My entire code works for all the examples I put in until I tried this input combo:

Midterm Grade: 98

Final Grade: 97

Ranging from 3 to 10 Homework (lowest one is dropped): 10, 9, 8, 7, 6, 5, 4, 3, 2, 1

The output says that schemeB is 69.7 while schemeA is 70. 70 correctly came out as the final grade. However, the last part says that the course grade is D....I don't know where it went wrong. The code is supposed to read the 70 and mark it as a C. When tried it with bigger answers like 100% midterm, 100% final, 0% homework, it gave me 70 and correctly marked it as a C. How do I correct this type casting issue? All of these are doubles.

This is the ending part of my code:

if (schemeA < schemeB) {

finalScheme = schemeB;

cout << "Your final score is " << finalScheme << endl;

}

if (schemeB < schemeA) {

finalScheme = schemeA;

cout << "Your final score is " << finalScheme << endl;

}

if (schemeA == schemeB) {

finalScheme = schemeB;

cout << "Your final score is " << finalScheme << endl;

}


if (finalScheme >= 90 && finalScheme <= 100) {

cout << "Your course grade is A" << endl;

}


if (finalScheme >= 80 && finalScheme < 90) {

cout << "Your course grade is B" << endl;

}


if (finalScheme >= 70 && finalScheme < 80) {

cout << "Your course grade is C" << endl;

}


if (finalScheme >= 60 && finalScheme < 70) {

cout << "Your course grade is D" << endl;

}


if (finalScheme >= 0 && finalScheme < 60) {

cout << "Your course grade is F" << endl;

}
Last edited on
Try instead checking if "finalScheme" is >= 6.99 (which can be the final code if a solution, since you'd likely round up anyway).

You can use setprecision to see exactly what "finalScheme" is equal to.
Hi, thanks for the reply. I am using setprecision right now. When the fixed is present, it reveals my schemeA to be 69.9999999999998, but this causes all my other potential outputs to look incredibly ugly with a lot of 0s after it. I removed the fixed to make it cout << setprecision(15); and it just gave me a 70 again....
:(

cout << setprecision(15) << fixed;
schemeA = ((midterm / 100) * 30) + ((final / 100) * 40) + ((hwktotal / 100) * 30);
cout << "Your final score based on Scheme A is " << schemeA << endl;
schemeB = ((midterm / 100) * 0) + ((final / 100) * 70) + ((hwktotal / 100) * 30);
cout << "Your final score based on Scheme B is " << schemeB << endl;

if (schemeA < schemeB) {
finalScheme = schemeB;
cout << "Your final score is " << finalScheme << endl;
}
if (schemeB < schemeA) {
finalScheme = schemeA;
cout << "Your final score is " << finalScheme << endl;
}
if (schemeA == schemeB) {
finalScheme = schemeB;
cout << "Your final score is " << finalScheme << endl;
}
if (finalScheme >= 0 && finalScheme < 60) {
grade = 'F';
}
else if (finalScheme >= 90 && finalScheme <= 100) {
grade = 'A';
}
else if (finalScheme >= 80 && finalScheme < 90) {
grade = 'B';
}
else if (finalScheme >= 70 && finalScheme < 80) {
grade = 'C';
}
else if (finalScheme >= 60 && finalScheme < 70) {
grade = 'D';
}

cout << "Your course grade is " << grade << endl;
You can make a rounding function so it goes to the nearest percentage.
Nearest percentage? The assignment wants the exact grade, and the compiler right now is taking 69.999999999999986 into a 70 for some reasons. Everything else that is not so close on the decimal scale is working fine with the code.

cout << setprecision(15) << fixed; actually fixes this issue and reveals the 69.99999999986, but it also causes all my other answers to come with a lot of 0s following it. I tried removing the fixed, but it causes the final score to turn into 70 again. Is there a way to fix this issue? The correct answer is supposed to be 69.99 instead of 70 (which is what the compiler is putting into my if statements).
Are you sure that's supposed to be the percentage? Do the math by hand.

Don't use std::fixed, it should output to the decimal place the number goes to if available. So if you set the precision to 10, and the number has numbers after the decimal, it'll show all 8. If it has no numbers after the decimal point, it won't show any at all.
Hi, I did the math by hand with my examples and normally, it should all add up to 70 for schemeA. However, the computer compiler is not reading it as such for some reasons, as it listed 70 while giving me a D letter grade. But by my if statements, it completely skipped by check for C and went into D despite being 70
It'll output 70 while displaying "D", right? This is because the variable is holding 69.99999... and so without setprecision, it just outputs a rounded 70, while using the actual value for math like ">=".

To solve this, you need a rounding function. If the value is big enough to round 5 or more decimal points in, it should be rounded up. EX:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void rround(double &i)
{
	if (i - (int(i)) >= .99999)
	{
		i = (int(i) + 1);
	}
}


int main()
{
	double i = 69.99;
	std::cout << std::setprecision(15) << i << '\n';
	rround(i);
	std::cout << std::setprecision(15) << i;
}
Topic archived. No new replies allowed.