rewrite the program below using a switch statement

I wonder if my codes meet all the requirements here yet ? and How do you rewrite the program below using a switch statement instead of the if-else chain?

Use an if-else statement to write the following program.
Here is a simplified grading system.
Grade Points Percentage
A 4.00 90-100
B 3.00 80-89
C 2.00 70-79
D 1.00 60-69
F 0.00 <60
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.)

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
  #include <iostream>
#include <iomanip>
#include<cmath>

using namespace std;

int main()
{
	double score;
		cout << "\n\n\t\t Please enter your score : " ;
	cin >> score;
		cout << setprecision(2) << fixed; // We set setprecision value to 0 to round it to an integer (>= .5 rounds up, <.5 rounds down.)
	//score = (int) score;
		 if ( score>=100)
		cout << " \n\n\t\tYou have entered an invalid number! \n \n      \t\t\tTry again! ";
		
    else if (score <=0)
	    cout << " \n\n\t\tYou have entered an invalid number! \n \n      \t\t\tTry again! ";
	    
	else if (score >=90)
	    cout << " \n\n\t\tCongat! \n \nPercentage : " << score <<"%   Grade A   Points: 4.0 ";
	    
	else if (score >=80)
		cout << " \n\n\t\tCongat! \n \nPercentage : " << score <<"%   Grade B   Points: 3.0 ";
		
	else if (score>=70)
        cout << " \n\n\t\tCongat! \n \nPercentage : " << score <<"%   Grade C   Points: 2.0 ";
        
	else if (score>=60)
	    cout << " \n\n\t\tCongat! \n \nPercentage : " << score <<"%   Grade D   Points: 1.0 ";
	    
	else if ( score <60)
	   cout << " \n\n\t\tCongat! \n \nPercentage : " << score <<"%   Grade F   Points: 0.0  \n \n You have failed!";
		
		return 0;
	
} 





How do you rewrite the program above using a switch statement instead of the if-else chain?

Keep the code that rounds the percentage to an integer! Use this integer with the switch statement. Think about how to do this in the best way.
You might make a list of multiple case values that all run the same block of code (see p. 169 for an example of this.)
Or, you might process the number a little more get to just one value per case. It’s up to you.
Include the default case: do not list every number from 0 to 59 as a separate case!
It’s okay to process the invalid input outside of the switch statement, i.e. with its own if statement.
Your assignment has the first hint.
Keep the code that rounds the percentage to an integer!


Here is the next:
1
2
3
4
int x = 1;
int y = 2;
int z = x / y;
// what is the value of z? 
Hi LowestOne,

Can you be more clear ?

How do you convert the input to an integer then display them as percentage % ?

Thanks
Your code already shows how to convert a double to an int:
int my_int = (int)my_double

My post was leading you to an answer to this:
Use this integer with the switch statement. Think about how to do this in the best way.


This is the type of question I can't really provide much information about without telling you exactly how to do it. This isn't a "do my homework" site, what have you done to try to solve the problem?
Hint:

Or, you might process the number a little more get to just one value per case.


each score's first digit is distinct from the others except for 60 and < 60, right? So how could you somehow extract and use the single first digit in a switch / case set of statements?
Last edited on
Topic archived. No new replies allowed.