Something wrong with a percentage formula?

Hi,

I have coded with program for my college assignment, but it looks like there is something wrong with my percentage formula. It always shows percentage as "0".

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

int main()
{
	int a, b, c, d, p;

	cout<<"Enter first test score: ";
	cin>>a;
	cout<<"Enter second test score: ";
	cin>>b;
	cout<<"Enter third test score: ";
	cin>>c;

	while (a>-1 && b>-1 && c>-1)

{		d=((a+b+c)/3);
		cout<<"Average score: "<<d<<endl;

		p=(((d)/(a+b+c))*(100));
		cout<<"Percentage: "<<p<<endl;

		if (p>80)
		{
			cout<<"Good job!";
		}
		else
		{
			cout<<"Work hard!"<<endl<<endl;
		}

		cout<<"Enter first test score: ";
	cin>>a;
	cout<<"Enter second test score: ";
	cin>>b;
	cout<<"Enter third test score: ";
	cin>>c;
	}
	return 0;
}

Last edited on
Percentage, or in this case "p" Has to be a float, not int.
Also. A better way of doing this program is using a do-while-loop. Like this -

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
int a, b, c;
float d, p;

	do
	{
		cout << "Enter first test score: ";
		cin >> a;
		cout << "Enter second test score: ";
		cin >> b;
		cout << "Enter third test score: ";
		cin >> c;

		d = ((a + b + c) / 3);
		cout << "Average score: " << d << endl;

		p = ((d) / (a + b + c))*(100);
		cout << "Percentage: " << p << endl;

		if (p>80)
		{
			cout << "Good job!";
		}
		else
		{
			cout << "Work hard!" << endl << endl;
		}

	} while (a>-1 && b>-1 && c>-1);
Last edited on
WOW!

This worked like a charm.

Thank you so much!
You're very welcome :) Goodluck to you.
Topic archived. No new replies allowed.