LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2006, 11:04 AM   #1
pengu
Member
 
Registered: Dec 2005
Location: Canton, GA, USA
Distribution: Ubuntu 7.10, FreeBSD, Debian
Posts: 207

Rep: Reputation: 30
Question a string switch statement in c++


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
 
Old 02-24-2006, 11:33 AM   #2
swodniw
Member
 
Registered: Jan 2006
Posts: 35

Rep: Reputation: 16
Quote:
this fails to compile with the simple error
Code:
c.cpp: In function ‘int main()’:
c.cpp:37: error: switch quantity not an integer
The clue here is the error message. You can not switch on a string.


I thought it was apt that I answered this question when you sign says
Quote:
... windows cd backwords ...

Last edited by swodniw; 02-24-2006 at 11:40 AM.
 
Old 02-24-2006, 11:40 AM   #3
pengu
Member
 
Registered: Dec 2005
Location: Canton, GA, USA
Distribution: Ubuntu 7.10, FreeBSD, Debian
Posts: 207

Original Poster
Rep: Reputation: 30
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.
 
Old 02-24-2006, 11:43 AM   #4
swodniw
Member
 
Registered: Jan 2006
Posts: 35

Rep: Reputation: 16
Quote:
Originally Posted by pengu
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...
 
Old 02-24-2006, 11:51 AM   #5
pengu
Member
 
Registered: Dec 2005
Location: Canton, GA, USA
Distribution: Ubuntu 7.10, FreeBSD, Debian
Posts: 207

Original Poster
Rep: Reputation: 30
ok, if your sure...

i cant use an int because i want the user to enter a shape, (like "rectangle") and the program will jump to that function.

i suppose i will just use an if-else block then. unless there is a way to cast a string into a int.

sorry for my newbeiness... i'm learning my first programming language with "C++ programming in easy steps" as my only recource.
 
Old 02-24-2006, 11:53 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,751

Rep: Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929
You can use char but not string.
Is this homework?
 
Old 02-24-2006, 12:15 PM   #7
pengu
Member
 
Registered: Dec 2005
Location: Canton, GA, USA
Distribution: Ubuntu 7.10, FreeBSD, Debian
Posts: 207

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by michaelk
You can use char but not string.
is there any way to use a char array or vector


Quote:
Originally Posted by michaelk
Is this homework?
um... no.... i just said i'm self taught.
 
Old 02-24-2006, 01:14 PM   #8
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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.

Just a thought .. PSM
 
Old 02-24-2006, 01:20 PM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,751

Rep: Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929
http://www.phim.unibe.ch/comp_doc/c_...AX/switch.html
Console menu systems typically use a single number (0-9) or a single character and not a whole word. I did a quick search and found a couple of examples.

http://www.java2s.com/ExampleCode/De...uselection.htm
http://code.dreamincode.net/snippet41.htm
 
Old 02-24-2006, 03:58 PM   #10
Vagrant
Member
 
Registered: Nov 2001
Posts: 75

Rep: Reputation: 15
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.
 
Old 02-24-2006, 04:02 PM   #11
pengu
Member
 
Registered: Dec 2005
Location: Canton, GA, USA
Distribution: Ubuntu 7.10, FreeBSD, Debian
Posts: 207

Original Poster
Rep: Reputation: 30
ok, thx for the help. ill just use if then statements
 
Old 02-24-2006, 04:05 PM   #12
Vagrant
Member
 
Registered: Nov 2001
Posts: 75

Rep: Reputation: 15
Ok, but if you just use a char representation then the user doesn't have to type out each shape, its cumbersome and easy to mess up. My thoughts.
 
Old 02-24-2006, 06:03 PM   #13
Ictus
Member
 
Registered: Jan 2005
Location: Germany
Distribution: Fedora Core 4
Posts: 32

Rep: Reputation: 15
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.
 
Old 02-25-2006, 05:51 PM   #14
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
The easiest way is to break it down like the compiler would on its own:
Code:
if (choice == "help")                   help();

else if (choice == "shapes")            cout << shapes;
		
else if (choice == "circle")            circle();
		
else if (choice == "triangle")          triangle();
		
else if (choice == "rectangle")         rectangle();
		
else if (choice == "sphere")            sphere();

else if (choice == "rectangular prism") recprism();

else if (choice == "triangular prism")  triprism();

else if (choice == "pyramid")           pyramid();

else if (choice == "cone")              cone();

else cout << choice << " not yet implemented";
'switch' will only work with integral data types, AFAIK.
ta0kira
 
Old 02-25-2006, 09:58 PM   #15
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
a common thing to do would be to make a numbered menu,.

ex..
1. Square
2. Triangle
3. Circle
etc..

the user will enter a number and then you can use a switch on the number selection..

you can use an enum or defines if you want to code to be more readable..
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Switch statement on strings in structures? Mistro116@yahoo.com Programming 2 11-13-2005 07:18 PM
Switch Statement w/ Constant Variables Mistro116@yahoo.com Programming 1 10-04-2005 07:48 AM
Bad switch statement (problem in the code block?) nifflerX Linux - General 2 07-14-2004 03:45 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
switch statement linuxanswer Programming 2 11-29-2003 11:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration