I'm supposed to write a program that uses different functions to take the scores of two tests and a homework and convert that into a letter grade. There is 'calcFinalScore' which does what it says calculates the final score then displays it as a grade point, and then 'printFinalScore' which displays their score as a letter grade. I've got it all down good but for some reason, the decimal gets truncated off the final score.
The tests are worth 40% of the final grade and the homework is worth 20%
I'm fairly certain I have the math right but its been awhile. So take that into consideration.
@Furry Guy, I tried both of what you suggested and it still comes out without a decimal. The sample input it gives us is Test 1 = 87.5, Test 2 = 82, Homework = 95. And the output is supposed to be 86.8. I checked it and that is what it should be, but I'm just getting plain old 86.
Enter the score for test #1: 87.5
Enter the score for test #2: 82
Enter the score for the homework: 95
The student's final score is: 86.8
Their final letter grade is: B
#include <iostream>
#include <iomanip> // <--- Added. For "setprecision" and "setw()".usingnamespace std; // <--- Best not to use.
double calcFinalScore(double, double, double); // <--- Adding a variable name can be very helpful.
void printFinalScore(double);
int main()
{
// local variables
double test1{}, test2{}; // the two tests scores. <--- ALWAYS initialize all your variables.double hw{}; // the homework score.
double finalScore{}; // the student's final score.
std::cout << std::fixed << std::setprecision(2); // <--- Added. Only needs done once.
cout << "Enter the score for test #1: ";
cin >> test1;
cout << "Enter the score for test #2: ";
cin >> test2;
cout << "Enter the score for the homework: ";
cin >> hw;
finalScore = calcFinalScore(test1, test2, hw);
cout << "The student's final score is: " << finalScore << endl;
printFinalScore(finalScore);
system("PAUSE");
return 0;
}
double calcFinalScore(double num1, double num2, double num3)
{
int final; // <--- Should be a "double".
final = (num1 * 0.4) + (num2 * 0.4) + (num3 * 0.2); // <--- This stuffs a "double" into an "int" loosing the decimal value.
return final; // <--- Changed back to a "double" with out the decimal value. It is now "?.0000".
//return (num1 * 0.4) + (num2 * 0.4) + (num3 * 0.2); // <--- This is all you need here.
}