My Program Does Not Calculate Percentages Properly

I was trying to do one of the exercises provided on the website (the one that tells what grade you got). I adapted it a little, to calculate percentages first, and then then display the grade, and I ran into a problem. The program wouldn't calculate percentages. To make sure that was the problem, I put the percentage bit into a new project, and tested it on its own. It did the same thing as on my original program. It said 100% if it was 100%, and said 0% if it was anything else. I looked around on the internet, but the few things I found I simply did not understand, being the newbie that I am. The code that I'm having the problem with is here, and any help at all would be much appreciated.
I'm sorry for the lack of comments, but this is probably simple enough to be understood without them. *fingers crossed*

#include <iostream>
#include <string>
using namespace std;

int percent (int a, int b)
{
float r;
r=a/b*100;
return (r);
}
int main()
{
int x, y;
float r;
cout << "Enter The Number of Marks You Got";
cin>> x;
cout<< "Enter the total amount you could get";
cin>> y;
r = percent (x, y);
if (r==100)
cout << "You got 100%! This is an A*! Congratulations!";

else if (r>=90)
cout << "You got an A*! That is great! Your score was" << r<< "%!";

else if (r>=80)
cout << "You got an A. This is a very good grade, but it has some room for improvement ;) Your score was " << x << " %!";

else if (r>=70)
cout << "You got a B. Not bad, but not the best. :D Your score was " << r << " %!";

else if (r>=60)
cout<< "You got a C. It's a pass, but you should put more work in next time. Your score was " << r<< " %!";

else if (r>=50)
cout<< "You got a D. You passed, but only just. Dont worry though, you'll do better next time! Your score was " << r<< " %";

else if (r>=40)
cout << "You got an E :( Its a fail, but only just. Your score was " << r<< " %";

else if (r>=30)
cout << "You got an F. F for FAIL! D:. Your score was " << r<< " %";

else if (r>=20)
cout<< "Your grade is a G. Study Harder!. Your score was " <<r<< " %";

else
cout<< "You either got a U, or this is an error. If you actually got a U: O.O. If it's an error: :D My bad!. Your score was "<< r << " %!";

cin.clear();
cin.ignore(255, '\n');
cin.get();
return (0);
}


Thanks in advance!
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
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>
using namespace std;

int percent (int score, int maxscore); // this is called function prototyping
// you pretty much copy and paste the function from below your int main () to up here
// and add a semi-colon ;

int main()
{
	int score, maxscore; // it's very important in c++ to properly name your variables
	
	cout << "Enter The Number of Marks You Got: ";
	cin >> score;
	cout<< "Enter the total amount you could get: ";
	cin >> maxscore;
	
	int percentgrade = percent (score, maxscore);

	if (percentgrade == 100)
		cout << "You got 100%! This is an A*! Congratulations!";

	else if (percentgrade >= 90)
		cout << "You got an A*! That is great! Your score was " << percentgrade << "%!";

	else if (percentgrade >= 80)
		cout << "You got an A. This is a very good grade, but it has some room for improvement ;) Your score was " << percentgrade << "%!";

	else if (percentgrade >= 70)
		cout << "You got a B. Not bad, but not the best. :D Your score was " << percentgrade << " %!";

	else if (percentgrade >= 60)
		cout<< "You got a C. It's a pass, but you should put more work in next time. Your score was " << percentgrade << "%!";

	else if (percentgrade >= 50)
		cout<< "You got a D. You passed, but only just. Dont worry though, you'll do better next time! Your score was " << percentgrade << "%";

	else if (percentgrade >= 40)
		cout << "You got an E :( Its a fail, but only just. Your score was " << percentgrade << "%";

	else if (percentgrade >= 30)
		cout << "You got an F. F for FAIL! D:. Your score was " << percentgrade << "%";

	else if (percentgrade >= 20)
		cout<< "Your grade is a G. Study Harder!. Your score was " << percentgrade << "%";

	else
		cout<< "You either got a U, or this is an error. If you actually got a U: O.O. If it's an error: :D My bad!. Your score was "<< percentgrade << "%!";

	cin.clear();
	cin.ignore(255, '\n');
	cin.get();
	return 0; // 0 doesn't have to be in brackets
}

int percent (int score, int maxscore) // let's put this down here for organizational sake
{
	return int (double (score) / double (maxscore) * 100); // convert numbers to doubles or floats before dealing with decimal places
// your statement would either return 1 or 0 with the way integers work.
// 37 / 40 = 0, since there are 0 decimal places with integers, so it'd constantly tell
// the user that they got 0%, which is a problem obviously

// so what I do here is I convert score and maxscore to doubles before dividing them.
// after that, I convert everything to an int so the compiler knows exactly what I'm doing and doesn't give me a warning message
}

Last edited on
Wow, this site works fast! Thank you so much this fixed the problem! But could you please explain briefly what this fixed? I want to be able to use this in other programs i would make.
It fixed your percent function, the comments are above. I also introduced function prototyping (cause it's awesome, of course).

Now what you should do is create a function to determine what grade the user got, instead of cluttering up your int main (), as well as a function that gets those grades from the user, so your int main () ends up with maybe 8-10 lines of code :)
Last edited on
I noticed it fixed the percent function, but i didnt know how. I didn't notice the comments before, so ya... I understand now. Thanks :D
Topic archived. No new replies allowed.