for(int i=23; i>=0; i--) {
cout.setf(ios::fixed);
cout.precision(1);
if (( dGrade >= 0 ) && ( dGrade <= 100 )){
cout << dGrade << "\n";
switch(dGrade) {
case (dGrade > 50):
cGrade = F;
break;
case ((dGrade <= 50) && (dGrade < 60)):
cGrade = D;
break;
case ((dGrade <= 60) && (dGrade < 70)):
cGrade = C;
break;
case ((dGrade <= 70) && (dGrade < 80)):
cGrade = B;
break;
case ((dGrade <= 90) && (dGrade < 100)):
cGrade = A;
break;
}
}
}
or... how would be the easiest way to do this ??
switches with doubles?
Okay i just got newbed by c++ .... is there a way to use doubles with a switch? I had this...
I assume double constants would work, but C/C++ switches in no way allow ranges. You'll have to use if/else statements:
if (dGrade > 50) cGrade = F;else if ((dGrade <= 50) && (dGrade < 60)) cGrade = D;else if ((dGrade <= 60) && (dGrade < 70)) cGrade = C;else if ((dGrade <= 70) && (dGrade < 80)) cGrade = B;else if ((dGrade <= 90) && (dGrade < 100)) cGrade = A;
Quote:Original post by Agony
I assume double constants would work
switch statements accept integral types only or user defined type for which there is an unambiguous conversion to integral type.
Quote:Original post by snk_kidQuote:Original post by Agony
I assume double constants would work
switch statements accept integral types only or user defined type for which there is an unambiguous conversion to integral type.
Gotcha. Even I was being too liberal with my assumptions. Thanks for the correction.
[I suppose I could've easily looked it up in the docs, but, meh, I'm in a weird mental funk today, and I wanted to beat everyone else to an answer so I could stay in the top 20 ranked non-mods/staff. [smile] I'm silly, I know.]
You really need to learn to get < and > the right way around...
> 50 gives you F ???
<=50 AND < 60 gives you D ???
Using dGrade divided by ten will vastly simplify the rest of your code...
> 50 gives you F ???
<=50 AND < 60 gives you D ???
Using dGrade divided by ten will vastly simplify the rest of your code...
switch(dGrade / 10) { case 6: cGrade = 'D'; break; case 7: cGrade = 'C'; break; case 8: cGrade = 'B'; break; case 9: cGrade = 'A'; break; default: cGrade = 'F'; }
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement