Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Switch statement and strings (http://www.programmingforums.org/showthread.php?t=12995)

gamerfelipe Apr 14th, 2007 10:16 AM

Switch statement and strings
 
Okay. I am a relatively new programmer here. I was wondering about the switch statement. Is it possible to use test a string in the switch statement like this:

:

switch(choice)
{
    case 'House': ....
    case 'Car': ...
    case 'City': ...
}


Thanks

DaWei Apr 14th, 2007 10:33 AM

No. In C, the switch expression must be integral, and the case expression must be a constant expression that is integral. In C++ they must be integral or have a class type that has an unambiguous conversion to integral type.

Furthermore, there is no such thing as 'House'. Single quotes are used to indicate single characters, such a 'H'. Double quotes are used to indicate strings of characters, such as "House."

RGCaldas Apr 14th, 2007 10:44 AM

But you can use something like this

char string[][] = {{"House"},{"Car"},{"City"}};
int op;

op = [funtion to get option]
switch (op){
case 0: ... House
case 1: ... Car
case 2: ... City
}


Hope it helps

DaWei Apr 14th, 2007 10:56 AM

It just confuses me. Is House a variable that you haven't shown a definition for? It certainly isn't the same thing as "House".

RGCaldas Apr 14th, 2007 11:15 AM

House Car and City, would be the code He needs for each case.

gamerfelipe Apr 14th, 2007 11:20 AM

Well actually, house, car, and city aren't really anything. They're just examples

Thanks for the input


All times are GMT -5. The time now is 10:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC