User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 426,791 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,769 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 6474 | Replies: 15
Reply
Join Date: Dec 2004
Posts: 465
Reputation: Acidburn is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Help why can't you use a switch statment with a string?

  #1  
Jan 7th, 2005
Hello 2 questions.....

1) I just tried to code a switch statement with a stribg but my compiler throws up error messages....

2) Is they anyway I can convert a string to an int and then back to a string?

cheers
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: why can't you use a switch statment with a string?

  #2  
Jan 7th, 2005
Post some code...
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 199
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: why can't you use a switch statment with a string?

  #3  
Jan 7th, 2005
switch statements work only on integer values.
In Java the values you switch on also are required to be constant values (either integer literals or defined constant values), C++ might be more relaxed about that.

If the string represent an integer (for example "1") you can use itoa to turn it into an int.
If it doesn't there's no easy way, but you could use a hashtable with the strings as keys and integer constants as values and switch on the value found in that hashtable.
Reply With Quote  
Join Date: Dec 2004
Posts: 465
Reputation: Acidburn is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: why can't you use a switch statment with a string?

  #4  
Jan 7th, 2005
[php]
#include <iostream>
#include <cstring> //string header

using namespace std;

int main()

{

int a = 0; //set to 1 if found
int b = 0; //set to 1 if found

char string[] = "Hello";
char string2[10];

cin >> string2;

switch (string2)
{
case 'hello':

if (string2 == "hello")
cout << "Its identical to string" <<endl;
break;

case 'GOODBYE':

if (string2 == "GOODBYE")
cout << "string 2 says goodbye , string 1 says hello"<<endl;
break;

}

return 0;

}
[/php]

since this doesnt work I thougt that if i convert the string entered from char to numbers (ints) i could use an if

if a ==435
cout<< "true";
else
cout << "false";

kinda thing
Reply With Quote  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: why can't you use a switch statment with a string?

  #5  
Jan 7th, 2005
I see... For switch see jwenting's post.
For comparing strings, use strcmp not ==
Reply With Quote  
Join Date: Dec 2004
Posts: 465
Reputation: Acidburn is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: why can't you use a switch statment with a string?

  #6  
Jan 7th, 2005
umm... ok can you not convert the chars into numbers?
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,470
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 10
Solved Threads: 176
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Solution Re: why can't you use a switch statment with a string?

  #7  
Jan 7th, 2005
If you just want a more readable switch/case, simply use enumerations.
[php]// example of enumeration enum name { list, ....... }

#include <stdio.h> // in/out and file functions

enum days { sun,mon,tue,wed,thu,fri,sat }; // sun = 0 etc.

int main()
{
enum days week;

for (week = sun; week <= sat; week++) { // used like integer constants
// enumerations make switch/case easier to read
switch (week) {
case sun : puts("Sunday"); break;
case mon : puts("Monday"); break;
case tue : puts("Tuesday"); break;
case wed : puts("Wednesday"); break;
case thu : puts("Thursday"); break;
case fri : puts("Friday"); break;
case sat : puts("Saturday"); break;
}
}
getchar();
return 0;
}
[/php]
May 'the Google' be with you!
Reply With Quote  
Join Date: Dec 2004
Posts: 465
Reputation: Acidburn is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: why can't you use a switch statment with a string?

  #8  
Jan 7th, 2005
cool.... thanks mate now i can continue my conquer in the world of c++ I do hope you guys dont mind me posting this stuff just trying to deepen my knowledge of c


could this be adatped to say if xxxx is entered into string??
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,470
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 10
Solved Threads: 176
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Solution Re: why can't you use a switch statment with a string?

  #9  
Jan 7th, 2005
Originally Posted by Acidburn
cool.... thanks mate now i can continue my conquer in the world of c++ I do hope you guys dont mind me posting this stuff just trying to deepen my knowledge of c


could this be adatped to say if xxxx is entered into string??
Please make this conquest a little more clear ...
May 'the Google' be with you!
Reply With Quote  
Join Date: Dec 2004
Posts: 465
Reputation: Acidburn is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: why can't you use a switch statment with a string?

  #10  
Jan 7th, 2005
Originally Posted by vegaseat
Please make this conquest a little more clear ...

Sorry could this be adapted , so that a string of charatcers are entered, ie say Monday, and it returns something? I'm just wondering since you've used
"puts" what ever that means...

i was thinking something amoungst the lines

[php]
#include < iostream>
#include < cstring>


enum days { sun,mon,tue,wed,thu,fri,sat }; // sun = 0 etc.

int main()
{

char string[10];
cin << string;
enum days week;

for (week = sun; week <= sat; week++) { // used like integer constants
// enumerations make switch/case easier to read
switch (week) {
case sun :
cout << "Found sunday";
break;

case mon :
cout << "Found monday";
break;

case Tue :
cout << "found tuesday";
break;

default:
cout << "your string wasn't matched!";
break;
}
}
getchar();
return 0;
}
[/php]
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 7:05 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC