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.


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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
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:
27
28
29
30
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.
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#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";
   }
   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:
27
28
29
30
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.


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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <iomanip>    // <--- 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.