Advertisement

Help, What wrong with this?

Started by March 19, 2003 11:06 AM
6 comments, last by GameDev.net 21 years, 8 months ago
WHat is wrong with this, could you please tell me. switch (temperature) { case (temperature < -18) : cout << "It is very cold"; break; case (-17 < temperature && temperature < 0) : cout << "Its cold"; break; case (temperature = 0) : cout << "The freezing point of water"; break; case (0 < temperature && temperature < 11) : cout << "It is very cool"; break; case (10 < temperature && temperature < 21) : cout << "It is moderate"; break; case (20 < temperature && temperature < 31) : cout << "It is warm"; break; case (30 < temperature && temperature <41) : cout << "It is hot"; break; case (40 < temperature && temperature < 100) : cout << "It is extremely hot"; break; case (temperature = 100) : cout << "The boiling point of water"; break; default: cout << "Please enter a number lower than 101"; }
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
You can''t do comparisons in case labels, try an if else if chain instead...


// Website // Google // GameDev // NeHe // MSDN // OpenGL Extensions //
~neoztar "Any lock can be picked with a big enough hammer"my website | opengl extensions | try here firstguru of the week | msdn library | c++ faq lite
Advertisement
(temperature) is an int
(temperature < -18) gives you a boolean result

Your "switching" (casing?) an int and comparing it to a bool.
Get it?
your EQUAL TO cases? No need for them, amnd you got them wrong . Just put 0 or 100 there.

and i dont know if siwtch takes the > < <=

.lick
Also, with your above code, what happens at temperature of -18?
-Rob --> "Go sell crazy somewhere else, we're all stocked up here!"
Do this:

1. get rid of "switch(temperature)"
2. change all occurences of "case" to "if"
3. remove all occurences of ":"
4. change all occurrences of "break;" to "else"
5. change all occurrences of "=" to "=="
6. change "default:" to "else"
6. fix the bug with -18

Your code will work fine after that.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Advertisement
This:
switch (a){  case 1:    // some code    break;  case 2:    // some more code    break;  default:    // again more code    break;}   


is the same EFFECT (but maybe not the same compiled code) as this:
if (a == 1) { /* some code */ }else if (a == 2) { /* some more code */ }else { /* again more code */ }    


Switches can possibly be more optimized than if/else-if/.../else.

The only things that you can use with "case" are numbers (no variables). Strings won't work because their "value" is actually their position in memory.

[edited by - Nypyren on March 19, 2003 6:58:18 PM]

[edited by - Nypyren on March 19, 2003 6:58:41 PM]
This is how you want to do it.. indent the cout statements - formatting here isn''t cooperating. Looks like a homework question.. those aren''t allowed here....

if (temperature < -18)
cout << "It is very cold";
else if (temperature < 0)
cout << "Its cold";
else if (temperature == 0)
cout << "The freezing point of water";
else if (temperature < 10)
cout << "It is very cool";
else if (temperature < 20)
cout << "It is moderate"";
else if (temperature < 30)
cout << "It is warm";
else if (temperature < 40)
cout << "It is hot";
else if (temperature < 100)
cout << "It is extremely hot";
else if (temperature == 100)
cout << "The boiling point of water";
else
cout << "Please enter a number equal to or less than 100";

This topic is closed to new replies.

Advertisement