![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2007
Location: Hershey, the sweetest place on earth
Posts: 19
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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."
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2006
Posts: 5
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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".
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Nov 2006
Posts: 5
Rep Power: 0
![]() |
House Car and City, would be the code He needs for each case.
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Jan 2007
Location: Hershey, the sweetest place on earth
Posts: 19
Rep Power: 0
![]() |
Well actually, house, car, and city aren't really anything. They're just examples
Thanks for the input |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| .NET Database Application | ReggaetonKing | Other Programming Languages | 10 | Jan 18th, 2007 3:08 PM |