How do you convert input into an integer then display them as % (double ) ?

How do you convert input into an integer then display them as % (double ) ?

For example, if user enters 89.4, the program prints out:
Percentage: 89.4% Grade: B Points: 3.00


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

using namespace std;
int main()
{
 int iscore;
 double dscore;
 iscore=dscore;
 cout << " \n\n\t\tPlease enter your score: ";
 cin >> iscore;
 cout << setprecision(2) << fixed; // We set setprecision value to 0 to round it to an integer (>= .5 rounds up, <.5 rounds down.)
	switch (iscore)
	{ // Beginning of switch.
		
		case 100:
		case 99: 
		case 98: 
		case 97:
		case 96:
		case 95:
		case 94:
		case 93:
		case 92:
		case 91:	
		case 90: cout << " \n\n\t\tYou Are Smart! \n \n\t\tPercentage : " << iscore <<"%   Grade A   Points: 4.0 " ; break;
		case 89:
		case 88:
		case 87:
		case 86:
		case 85:
		case 84:
		case 83:
		case 82:
		case 81:
		case 80:cout << " \n\n\t\tWow! That is very good! \n \n\t\tPercentage : " << iscore <<"%   Grade B   Points: 3.0 " ; break;
		case 79:
		case 78:
		case 77:
		case 76:
		case 75:
		case 74:
		case 73:
		case 72:
		case 71:
		case 70:cout << " \n\n\t\tYou can do better! \n \n\t\tPercentage : " << iscore <<"%   Grade C   Points: 2.0 " ; break;
		case 69:
		case 68:
		case 67:
		case 66:
		case 65:
		case 64:
		case 63:
		case 62:
		case 61:
		case 60:cout << " \n\n\t\tYou Are Not Smart! \n \n\t\tPercentage : " << iscore <<"%   Grade D   Points: 1.0 " ; break;
		default:
			cout << " \t\t\t    You have failed ! ";
	} // end of switch.
	
 return 0;
 
}
omg do you really think thats the right way to go about making a switch statement with all those cases?

Could you imagine if that was the only way to do what you did? A larger more complex project would be.....I can't even fathom how hard it would be to program...

Use an if statement if a grade is between 90 and 100 cout << "ablabalhba" << endl;

1
2
3
4
5
6
7
8
if(grade >= 90 && grade <= 100) 
{

}
else if(grade >= 80 && grade < 90)
{

}
Last edited on
Hi novellof,

It is not a good way to make this but this what my professor want me to do.

Here is what I have in the if statement

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

using namespace std;

int main()
{
	double score;
	   cout << "\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\tYou Are Smart! \n \n\t\tPercentage : " << score <<"%   Grade A   Points: 4.0 ";
	    
	else if (score >=80)
		cout << " \n\n\t\tGood Job! \n \n\t\tPercentage : " << score <<"%   Grade B   Points: 3.0 ";
		
	else if (score>=70)
        cout << " \n\n\t\tYou Can Do Better! \n \n\t\tPercentage : " << score <<"%   Grade C   Points: 2.0 ";
        
	else if (score>=60)
	    cout << " \n\n\t\tBad Grade! \n \n\t\tPercentage : " << score <<"%   Grade D   Points: 1.0 ";
	    
	else if ( score <60)
	   cout << " \n\n\t\tToo Bad! \n \n\t\tPercentage : " << score <<"%   Grade F   Points: 0.0  \n \n You have failed!";
		
		return 0;
	
} 


jesus....ahaha

is this like newspeak for programmers

ok so cast the double variable as an int in the switch statement

then when displaying just look up a formula to convert to GPA

. I dont understand your comment that is to the right of set precision

setprecision(2) means 2 decimal places.
Last edited on
I am still confused ????

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
1) you know how to do cin>> double datatype

2) Rounding: don't know if you are confused on this but here is my explanation

When you convert a double to an integer..you loose all the data to the right of the decimal point...this is called truncation. So if you have a double of 5.9 and casted that to an int you would have 5. You have to program the "algorithm" basically tell the computer how to round. Remember..When programming you have to tell the computer every step!
Any number that has X.5 or greater will round up after adding 0.5, and any number that is less than X.5 will be rounded down. This is what you do in your head without even realizing it.

EXAMPLE:
So for your program if the grade is 89.58 and you add 0.5 to it it becomes 90 if you cast that to an int...But if you casted that to an int before adding the 0.5 it would be 89..It would have not rounded up! Now you are thinking why adding 0.5? Well this is explicating telling the computer how to round! The computer has no conceptional understanding of what rounding is....or anything at all for that matter it only knows what you tell it...

If you were to add 0.5 to 89.48 this would not round up to 90...Casted to an int you would get 89...

So Formula for rounding is intGrade = doubleGrade + 0.5;


Last edited on
Hi,

Thank you very much, I understand it now.

Topic archived. No new replies allowed.