Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 14th, 2007, 11:16 AM   #1
gamerfelipe
Newbie
 
Join Date: Jan 2007
Location: Hershey, the sweetest place on earth
Posts: 19
Rep Power: 0 gamerfelipe is on a distinguished road
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
gamerfelipe is offline   Reply With Quote
Old Apr 14th, 2007, 11:33 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 14th, 2007, 11:44 AM   #3
RGCaldas
Newbie
 
Join Date: Nov 2006
Posts: 5
Rep Power: 0 RGCaldas is on a distinguished road
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
RGCaldas is offline   Reply With Quote
Old Apr 14th, 2007, 11:56 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 14th, 2007, 12:15 PM   #5
RGCaldas
Newbie
 
Join Date: Nov 2006
Posts: 5
Rep Power: 0 RGCaldas is on a distinguished road
House Car and City, would be the code He needs for each case.
RGCaldas is offline   Reply With Quote
Old Apr 14th, 2007, 12:20 PM   #6
gamerfelipe
Newbie
 
Join Date: Jan 2007
Location: Hershey, the sweetest place on earth
Posts: 19
Rep Power: 0 gamerfelipe is on a distinguished road
Well actually, house, car, and city aren't really anything. They're just examples

Thanks for the input
gamerfelipe is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
.NET Database Application ReggaetonKing Other Programming Languages 10 Jan 18th, 2007 4:08 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:18 AM.

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