Public Group

# switches with doubles?

This topic is 4972 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Okay i just got newbed by c++ .... is there a way to use doubles with a switch? I had this...
	for(int i=23; i>=0; i--) {

cout.setf(ios::fixed);
cout.precision(1);

if (( dGrade >= 0 ) && ( dGrade <= 100 )){

break;
break;
break;
break;
break;
}
}
}

or... how would be the easiest way to do this ??

##### Share on other sites
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;

##### Share on other sites
Quote:
 Original post by AgonyI 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.

##### Share on other sites
Quote:
Original post by snk_kid
Quote:
 Original post by AgonyI 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.]

##### Share on other sites
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...
			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';			}

1. 1
2. 2
3. 3
4. 4
Rutin
18
5. 5

• 11
• 21
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
631406
• Total Posts
2999896
×

## Important Information

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!