![]() |
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)Thanks |
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." |
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 |
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".
|
House Car and City, would be the code He needs for each case.
|
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