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;
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;
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
#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";
}