### new to functions, decimal gets truncated off the final grade, and for why? 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.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556`` ``````#include using namespace std; double calcFinalScore(double, double, double); void printFinalScore(double); int main() { // local variables double test1, test2; // the two tests scores double hw; // the homework score double finalScore; // the student's final score 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; final = (num1 * 0.4) + (num2 * 0.4) + (num3 * 0.2); return final; } void printFinalScore(double actualScore) { if (actualScore > 90) { cout << "Their final letter grade is: A\n\n"; } else if (actualScore < 90 && actualScore >= 80) { cout << "Their final letter grade is: B\n\n"; } else if (actualScore < 80 && actualScore >= 70) { cout << "Their final letter grade is: C\n\n"; } else if (actualScore < 70 && actualScore >= 60) { cout << "Their final letter grade is: D\n\n"; } else if (actualScore < 60) { cout << "Their final letter grade is: F\n\n"; } }`````` You contract your calcfinalScore function will return a double, yet you are returning an int.

Change your local variable final to a `double` at line 29.

OR you can modify the function and not worry about an intermediate local variable:
 ``27282930`` ``````double calcFinalScore(double num1, double num2, double num3) { return ((num1 * 0.4) + (num2 * 0.4) + (num3 * 0.2)); }`````` @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. ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556`` ``````#include 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"; } else if (actualScore < 90 && actualScore >= 80) { std::cout << "Their final letter grade is: B\n"; } else if (actualScore < 80 && actualScore >= 70) { std::cout << "Their final letter grade is: C\n"; } else if (actualScore < 70 && actualScore >= 60) { std::cout << "Their final letter grade is: D\n"; } else if (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```

Same output with:
 ``27282930`` ``````double calcFinalScore(double num1, double num2, double num3) { return ((num1 * 0.4) + (num2 * 0.4) + (num3 * 0.2)); }`````` Hello morganniie,

In addition to what Furry Guy has said a few blank lines makes the code more readable.

When using "doubles in your program the "<iomanip>" header file along with line 16 if very handy.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````#include #include // <--- Added. For "setprecision" and "setw()". using namespace 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.

Andy
Topic archived. No new replies allowed.