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.
#include <iostream>
double calcFinalScore(double, double, double);
void printFinalScore(double);
int main()
{
std::cout << "Enter the score for test #1: ";
double test1;
std::cin >> test1;
std::cout << "\nEnter the score for test #2: ";
double test2;
std::cin >> test2;
std::cout << "\nEnter the score for the homework: ";
double hw;
std::cin >> hw;
std::cout << '\n';
double finalScore = calcFinalScore(test1, test2, hw);
std:: cout << "The student's final score is: " << finalScore << '\n';
printFinalScore(finalScore);
}
double calcFinalScore(double num1, double num2, double num3)
{
double final = ((num1 * 0.4) + (num2 * 0.4) + (num3 * 0.2));
return final;
}
void printFinalScore(double actualScore)
{
if (actualScore > 90)
{
std::cout << "Their final letter grade is: A\n";
}
elseif (actualScore < 90 && actualScore >= 80)
{
std::cout << "Their final letter grade is: B\n";
}
elseif (actualScore < 80 && actualScore >= 70)
{
std::cout << "Their final letter grade is: C\n";
}
elseif (actualScore < 70 && actualScore >= 60)
{
std::cout << "Their final letter grade is: D\n";
}
elseif (actualScore < 60)
{
std::cout << "Their final letter grade is: F\n";
}
}
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.
}
In your "printFinalScore" function the if statements are comparing a "double" to an "int". This may not always return the desired result.