Percent acting funny

Feb 20, 2017 at 5:37pm
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 Feb 20, 2017 at 5:38pm
Feb 20, 2017 at 5:56pm
What is it doing, exactly?
Last edited on Feb 20, 2017 at 5:56pm
Feb 20, 2017 at 6:00pm
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.
Feb 20, 2017 at 6:02pm
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 Feb 20, 2017 at 6:03pm
Feb 20, 2017 at 6:04pm
it adds right, but when it displays the percentage, it gets a smaller number with a lot of decimals that's nowhere near right.
Feb 20, 2017 at 6:06pm
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 Feb 20, 2017 at 6:08pm
Feb 20, 2017 at 6:28pm
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.