0
101 Aug 25, 2006 at 17:57

Hey folks, another question for you guys…

I can write a basic switch statement no problem. But I am trying a more complex one that evaluates a players game score in 4 ‘tiers’ out of 100, but it is not working so…

Heres the code first.

//Using switch statement to add more than 2 responses to player score

#include <iostream>

using namespace std;

int main ()
{
int score;

cout << "What was your score?";
cin >> score;

switch (score)
{
case (score <=25):
cout << "\nOuch, less than 25...!";
break;

case (score <=50):
cout << "\nYou score aint great mate..";
break;

case (score <=75):
cout << "\nYour pretty good, wel done man!";
break;

case (score <=100):
cout << "\nYou got to the top!!!";
break;

default:
cout << "\nYou cant score higher than 100!!! Cheater!!!!";
}

cin.ignore();
cin.get();

return 0;
}


My Complie log hits the first case statement and reports a case label does not reduce to an integer constant.

Ok, now, question one;

1) can I put a test into the value of each case?? If not, thats the simple answer, but if thats the case, what function would you use to achieve the same idea. If you can do the testing on to two..

2) my only other thought of why this isnt working is that the value is not realy an integer, as I believe that switch statements only work with integers, so it wouldnt work??

If its not one of the above two, can someone give me some help as to why its not working??? Thanks folks.

As an aside, I’m currently working with Dev C++ and using the book Beginning C++ Game Programming by Micheal Dawson, but I’m only just getting into the 3rd Chapter on Arrays….

Cheers

Kal

#### 3 Replies

0
167 Aug 25, 2006 at 18:21

You can’t use ranges in switch statements. Not supported by the language, sorry. As the compiler error suggests, each case has to be an integer constant - they are not boolean expressions like in if statements. You’ll just have to use a series of if/elses for this.

0
101 Aug 25, 2006 at 18:23

Before I get started, look at the definition of a switch statement:

Definition: A switch is a conditional statement that compares an expression against a list of cases. Each case must be a constant of an integral type. If a case label is not terminated with a break statement, execution falls through. Without breaks, all code from the first case which is true down to the end of the switch statement will be executed. A default label can be used to provide code to be executed if no cases match

Basically, the switch statement just goes through a list and tests to see if two values are the same. So lets say you casted ‘(score <= 25)’ to an integer, you would get back a 1 or a 0. This here is the main reason we don’t want to use conditionals (‘(score <= 25)’) in a switch statement, it’s because they evaluate to a boolean value (true or false, 1 or 0) which would mean we would be trying to compare our value to a 1 or a 0 which does not give us the correct results.

For what you are trying to do you have to use an if statement; try the following:

#include <iostream>

using namespace std;

int main ()
{
int score;

cout << "What was your score?";
cin >> score;

if (score <= 25)
{
cout << "\nOuch, less than 25...!";
}
else if (score <= 50)
{
cout << "\nYou score aint great mate..";
}
else if (score <= 75)
{
cout << "\nYour pretty good, wel done man!";
}
else if (score <= 100)
{
cout << "\nYou got to the top!!!";
}
else
{
cout << "\nYou cant score higher than 100!!! Cheater!!!!";
}

cin.ignore();
cin.get();

return 0;
}


I hope this helps you!
- dega

0
101 Aug 25, 2006 at 18:27

Yep, cheers for the Help :happy:

Kal