switch (grade)
{
case 'A':
cout << "the grade is A.";
break;
case 'B':
cout << "the grade is B.";
break;
case 'C':
cout << "the grade is C.";
break;
case 'D':
cout << "the grade is D.";
break;
case 'F':
cout << "the grade is F.";
break;
default:
cout <<"the grade is invalid.";
}
for this code, can some1 tell me how i can put the grades to numbers.
example: input grade .75
your letter grade is C
switch statements
Page 1 of 12 Replies - 1217 Views - Last Post: 25 March 2008 - 08:50 AM
Replies To: switch statements
#2
Re: switch statements
Posted 25 March 2008 - 08:43 AM
student_C++, on 25 Mar, 2008 - 08:16 AM, said:
switch (grade)
{
case 'A':
cout << "the grade is A.";
break;
case 'B':
cout << "the grade is B.";
break;
case 'C':
cout << "the grade is C.";
break;
case 'D':
cout << "the grade is D.";
break;
case 'F':
cout << "the grade is F.";
break;
default:
cout <<"the grade is invalid.";
}
for this code, can some1 tell me how i can put the grades to numbers.
example: input grade .75
your letter grade is C
I don't think you can do it with a switch statement, first off, switch statements only work with integer values - so you can't use .75 and you can't use relational operators in switch statements either.
you could write a really long switch statement for every single percent value - or you could just write an if else statement -
if(grade > 90) cout << "The grade is A\n"; else if(grade > 80) cout << "The grade is B\n";
and so on..
#3
Re: switch statements
Posted 25 March 2008 - 08:50 AM
bluewagon, on 25 Mar, 2008 - 08:43 AM, said:
student_C++, on 25 Mar, 2008 - 08:16 AM, said:
switch (grade)
{
case 'A':
cout << "the grade is A.";
break;
case 'B':
cout << "the grade is B.";
break;
case 'C':
cout << "the grade is C.";
break;
case 'D':
cout << "the grade is D.";
break;
case 'F':
cout << "the grade is F.";
break;
default:
cout <<"the grade is invalid.";
}
for this code, can some1 tell me how i can put the grades to numbers.
example: input grade .75
your letter grade is C
I don't think you can do it with a switch statement, first off, switch statements only work with integer values - so you can't use .75 and you can't use relational operators in switch statements either.
you could write a really long switch statement for every single percent value - or you could just write an if else statement -
if(grade > 90) cout << "The grade is A\n"; else if(grade > 80) cout << "The grade is B\n";
and so on..
thanks!
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|