Math help needed for a beginner?

Hi everyone. This is a homework assignment where we gotta show if a student's score pass or failed. Everything operates how I want it too EXCEPT for the math bit. if it's anything less than = to the max score, it shows 0% and goes to the else path. The student score is supposed to reflect in percentages.

For example, max score is "100," a passing score is "50," and the student scored "99." If I type in 100 it goes to congratulations and shows the percentage at 100%. 99 and below shows too bad and the score is 0%. I thought I had to do 100.0 to make it an integer and calculate properly, but that did nothing. Any help is appreciated!



#include <iostream>
using namespace std;

int main()
{
int studentscore = 0;
int maxscore = 0;
int passfail = 0;
int percent = 0;

cout << "Enter maximum score: " << endl;
cin >> maxscore;

cout << "Enter Percent Pass score: " << endl;
cin >> passfail;

cout << "Enter Student's test score: " << endl;
cin >> studentscore;

percent = (studentscore / maxscore) * 100.0;

cout << "Student scored " << percent << "%. \n" << endl;

if(percent >= passfail)
{
cout << "Congratulations! You passed!" << endl;
}
else if(percent < passfail)
{
cout << "Too bad. You failed..." << endl;
}
}
closed account (48T7M4Gy)
You are using integer arithmetic so 78/100 = 45/100 = 0
Maybe this will help:

percent = studentscore / maxscore * 100.0
Last edited on
kemort: Aren't I supposed to? I mean if I wrote, say. 654 and 999 and multiply it by 100, I'd get 65.465...% and so on. I should have at least 1 decimal place. Or is there some other form I don't know about (very likely considering how newb I am)

BlueSquirrelJQX: I did that and got the same result :(

For now, I'm keeping it easy with 100 and 99 to get 99%. I'll be happy if it'll do just that...
In order to get floating point results from the built-in division operator, one of the operands must be floating-point.

You can either change the type of studentscore or maxscore to double, or change line ~20 to
percent = static_cast<double>(studentscore) / maxscore * 100.0;
Last edited on
1
2
// percent = studentscore / maxscore * 100.0 ;
percent = studentscore * 100.0 / maxscore ;


Or to make things clearer, percent = ( studentscore * 100.0 ) / maxscore ;
closed account (48T7M4Gy)
@Corbenik Rest assured there's no magic.

int answer = 654/999 = 0 if the two variables for 654 and 999 are int's.

One solution is int answer = 654*100/999 if the answer variable is an integer but I would be careful about truncation errors.

I would write double answer = studentscore *100.0 / maxscore; and then round off the answer. There are other ways.
Enter maximum score: 
999
Enter Percent Pass score: 
50
Enter Student's test score: 
654
Student scored 65.4655%. 

Congratulations! You passed!
Program ended with exit code: 0
That did it!! Oh my goodness, thank you so much, everyone :D I've been at this for 4 days with no results.

I'll keep everything you said to heart, too. I don't know how double works, but I'll study that too.

Again, thanks so much. I really appreciate it!
One can stay in integer math in some cases:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
    int x = 654;
    int y = 999;
    int dec = 100;
    int r = (100 * dec * x) / y;
    std::cout << (r/dec) << '.' << (r%dec) << "%\n";
}

65.46%

That, however, does not round the output.
Topic archived. No new replies allowed.