Problem with simple code

A course has 3 assignments, each of which is of worth 100 points. Assignments are worth 25% of the
total course grade points. Write a user friendly program that asks a student to enter his/her assignment
scores and holds those values in three variables. Display the total of the assignment scores and user’s
assignment grade percentage in the course. [Logically decide the datatype of variables being used].
Eg: 80,90,100 => Total = 270 and Grade = (Total/300)*25
OUTPUT FORMAT
Total = ___
Assignment grade percentage = ___ %

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream> 

using namespace std;

int main()
{
	double Test1, Test2, Test3;					
	int Total, Average;
	Total = (Test1 + Test2 + Test3);
	Average = (Total / 300) * 25; 			
	
	cout << "Please enter your three test scores: " << endl;
	cin >> Test1;
	cin >> Test2;
	cin >> Test3;

	cout << "Total = " << Total << endl;
	cout << "Assignment grade point = " << Average << endl;
	
	return 0;
	}


My output comes out to be 0 for Total and Average each time! What is wrong with my code? :(
Because you are defining them before the variables actually have any value to them. Simply make it so that the input comes before you define total and average.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	double Test1, Test2, Test3;					
	int Total, Average;

        cout << "Please enter your three test scores: " << endl;
	cin >> Test1;
	cin >> Test2;
	cin >> Test3;

	Total = (Test1 + Test2 + Test3);
	Average = (Total / 300) * 25; 			
	
	cout << "Total = " << Total << endl;
	cout << "Assignment grade point = " << Average << "%" << endl;
	
	return 0;
}


Do you understand why that is?
Last edited on
Yes! Now I understand that Test1 and the others couldn't be used until I assigned a number to them! The Total is working now, but the Average is still giving me 0. I don't understand why since Total already has a value.
Because you are doing integer division to calculate 'Average'. The answer comes out to zero point something when you divide by 300. The decimal part is truncated and you are left with 0, and 0 multiplied by anything gives you 0.

Just change the data type of average into a double or float. Or change the '300' to '300.0'.

Topic archived. No new replies allowed.