Rounding and converting to integer.. gahh

Ok so I'm in a programming 1 class working with c++. I have the following assignment:


Write a C++ program that:
asks for and accepts the Percentage earned with as a double (i.e. 75.45)
rounds it to an integer (>= .5 rounds up, <.5 rounds down.)
prints the original Percentage and the corresponding Grade and Points exactly as shown below.
prints an error message for any input that is less than 0 or greater than 100.

For example, if user enters 89.4, the program prints out:
Percentage: 89.4% Grade: B Points: 3.00
You must use an if-else statement to do this in your program.
Use fixed and precision output manipulators (see Ch. 3) to display the values with the exact precision shown above.

IMPORTANT:
Each if statement condition should contain only one comparison! read this again
This means code that is similar to this is NOT okay:  if (Percentage >= 80.00 && Percentage <90.00)
This code is not acceptable because the if statement condition above has two comparisons.
(Hint: If you order your if-else chain statements correctly, you will only need one comparison in each.)




I have the program working, but I'm pretty sure I'm not rounding how my professor would like it to. This is my code:

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



#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
	double percentage;
//	char grade;
	
	
	cout <<setiosflags(ios::fixed)<<setprecision(2)<< "What is the percentage of your grade?\n";
	cin>> percentage;

	
	
if (percentage <= 60){

cout << "Your grade is:F\t"<<"Your percentage is:"<<fixed<<percentage<<"\t"<<"Points: 0";
}
else if (percentage <= 69){

cout << "Your grade is: D\t"<<"Your percentage is:"<<fixed<<percentage<<"\t"<<"Points:1.00";
}
else if (percentage <=79){

cout << "Your grade is: C\t"<<"Your percentage is:"<<fixed<<percentage<<"\t"<<"Points:2.00";
}
else if (percentage <= 89){

cout << "Your grade is: B\t"<<"Your percentage is:"<<fixed<<percentage<<"\t"<<"Points:3.00";
}
else if(percentage >=90){

cout << "Your grade is:A\t"<<"Your percentage is:"<<fixed<<percentage<<"\t"<<"Points:4.00";
}
else{

cout<< "Invalid grade.";
}
}
	


So my issue here is the rounding, and then theres the converting the double percetnage to an integer. In my next assignment I have to write the program with a switch statement. And I'm struggling to figure out how to do this correctly. Any feedback will be much appreciated!
So my issue here is the rounding, and then theres the converting the double percetnage to an integer.
1
2
3
#include <cmath>
//...
int perc = std::round(percentage);
Hmm I get the error round is not a member of std. Because my professor is requiring it to be accepted percentage as a double, then convert it to an integer, I don't think an assignment expression will be acceptable.. gah I'm so confused on how to achieve this

cout <<setiosflags(ios::fixed)<<std::round(percentage)<< "What is the percentage of your grade?\n";
Last edited on
Figured it out. So simple lol... Thanks for the input

int perc = round(percentage);
Topic archived. No new replies allowed.