ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hi, I am trying to make a switch statement using a string. I can do the same thing with a long if-else statement but it would be less efficient. Here is my code.
Code:
int main() //find out what shape needs solvig
{
string e; //when to exit (see while loop)
string choice; // help, shapes, or 'figure'
string shapes; //list shapes implemented by this program
shapes = "rectangle";
e = "n";
while (e == "n")
{
title();
cout << "Typeing \"help\" will give help" << endl; //options...
cout << "Entering a shape name will jump to that shape" << endl;
cout << "Typing \"shapes\" will list included shapes" << endl;
cout << "Selection: "; //selected option
getline(cin, choice);
switch(choice)
{
case "help" : help(); break;
case "shapes" : cout << shapes; break;
case "circle" : circle(); break;
case "triangle" : triangle(); break;
case "rectangle" : rectangle(); break;
case "sphere" : sphere(); break;
case "rectangular prism" : recprism(); break;
case "triangular prism" : triprism(); break;
case "pyramid" : pyramid(); break;
case "cone" : cone(); break;
default : cout << choice << " not yet implemented"; break;
}
cout << "\nDo you want to exit? y/n ";
getline(cin, e);
}
return 0;
}
this fails to compile with the simple error
Code:
c.cpp: In function ‘int main()’:
c.cpp:37: error: switch quantity not an integer
is there any way to make this work?
thx in advance
thx for the fast reply, but i was wondering if there was a way to make it work... my dad used to program c and he said there was a way but he couldnt remember how.
thx for the fast reply, but i was wondering if there was a way to make it work... my dad used to program c and he said there was a way but he couldnt remember how.
Lol tell your dad he is wrong. For a start C doesn't have strings it uses arrays. For a quick fix ask the user to enter an int.
ie 0 to exit
1 to display...
1. You answered your own question in your first post: a C/C++ "switch/case" block only works with "int" types. If you want something that looks like a "switch/case" block and still uses strings (aka "C char[] arrays"), then you get to use a bunch of "strcmp()'s" in a block of "if/then/else" statements. Period.
2. An alternative, however, might be to forget about the "switch/case" block concept, and use an STL map instead.
You can switch on a single char, which is an integer type, maybe that's what your dad meant. You have your user just input a character representing the choice. This is the simplest method both for you and your users.
There's a possibility to use switch with strings. But i wonder if it's worth the work. ^_^
You can create your own String-class (inherited from std::string) and override the static_cast operator for int:
class mystring : std::string
{
// ...
public
operator int() { ... };
};
Within that overriden operator you can try to generate a unique integer for each string, for example with the CRC32 algorithm.
When using that string class in the switch statement u can easily use the normal strings, but actually it only works with the integer values.
That's the only possibility that comes into my mind right now.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.