•
•
•
•
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
![]() |
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation:
Rep Power: 18
Solved Threads: 199
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.
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.
•
•
Join Date: Dec 2004
Posts: 465
Reputation:
Rep Power: 4
Solved Threads: 5
[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
#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
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,470
Reputation:
Rep Power: 10
Solved Threads: 176
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]
[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!
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,470
Reputation:
Rep Power: 10
Solved Threads: 176
•
•
•
•
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??
May 'the Google' be with you!
•
•
Join Date: Dec 2004
Posts: 465
Reputation:
Rep Power: 4
Solved Threads: 5
•
•
•
•
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]
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the C++ Forum
- Previous Thread: Have an error in my program
- Next Thread: Keyboard input or "stuck in a loop"



Linear Mode