Percent acting funny

Why is my percent giving me that strange number?

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
#include <iostream>
using namespace std;

int main()
{

int assignmentamount, assignment, pointsreceived, pointspossible;
int received = 0;
int possible = 0;
double percent;

cout << "How many assignments did you do? " << endl;
cin >> assignmentamount;
	
	for (int counter = 1; counter <= assignmentamount; counter++ )
	{
		cout << "Enter the amount of points received on assignment " << counter << endl;
		cin >> received;
		pointsreceived = pointsreceived + received;
		cout << "Enter the amount of points possible on assignment " << counter << endl;
		cin >> possible;
		
		pointspossible = pointspossible + possible;
		percent = (double)pointsreceived / pointspossible * 100;
		
		cout << "You have earned " << received << " out of " << possible << " points." << endl;
		cout << "Your percentage is " << percent << "%" << endl;
	}
	
return 0;
}


pointsreceived and pointspossible are the total of each.
Last edited on
What is it doing, exactly?
Last edited on
It takes the amount of assignments you input, add's the total points received and you tell it the total points possible. It will add all the received, divide it over all possible, then with that number, display it as a percentage.
no, I mean, what is it printing that you think is wrong, for what input?
does it get the right answer for 1 assignment, and for 2?
an example of the problem helps spot the problem faster.
Last edited on
it adds right, but when it displays the percentage, it gets a smaller number with a lot of decimals that's nowhere near right.
give me some values. I just ran it and it gave the right answer,
I did 60 out of 100, 40 out of 100, and it gave me 50 which is correct.
I also did 10 assignments 1-10 worth 10 points each and got the correct 55.
it seems fine to me.
Last edited on
These variables are not initialised , they contain garbage.

 
pointsreceived, pointspossible;


You know the saying - garbage in, garbage out.

Try setting an initial value of 0 to each.
Topic archived. No new replies allowed.