473,507 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ Switch on Strings

1 New Member
i'm trying to get a switch statement to work with strings but I get an errror saying "switch quantity not an integer". Could someone please tell me how to fix the code below to make it work. P.S. It's jst a weird example i made not my assignment code.

Thanks heaps,
Roger



Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main(){
  5.     string name = "Todd";
  6.  
  7.     switch(name){
  8.  
  9.     case "Kimmy":
  10.          cout<<"Kimmy was here"<<endl;
  11.          break;
  12.     case "Todd":
  13.          cout<<"The Todd was here"<<endl;
  14.          break;
  15.     case "Sammy":
  16.          cout<<"Sammy was ere"<<endl;
  17.          break;
  18.     default:
  19.             cout<<"Lights on, nobody home"<<endl;
  20.             break;
  21.     }
  22.  
  23.     cout<<"Press any key to continue"<<endl;
  24.     getchar();
  25.     return 0;
  26.     }
Oct 2 '07 #1
1 7948
Laharl
849 Recognized Expert Contributor
Switch requires something that can be cast to an integer as its test variable, so it would work with any of the builtin types besides an array. You'll have to use if/else statements for this.
Oct 2 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
10882
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
13
6188
by: webzila | last post by:
Hello, I have to write a program for an 8051 micro-controller using micro-C to monitor Switch 1 and if the switch in pushed the message "switch 1 pushed" should be displayed in the LCD. Also the...
2
15592
by: Tomas Larsson | last post by:
Hi. I have some problems when using a static readonly declaration of a guid in a switch/case statement. I'll give you an example. public sealed class Activites { private Activites(){}
6
449
by: Mark Broadbent | last post by:
How on earth did Anders & co arrive at this awful switch/ case/ select statement? Not only does it lose the abilty to do an intelligent switch a-la VB eg. using >, <, list of values etc .... but...
5
3093
by: nobody | last post by:
With Visual C++ 2005 beta 2, I get the below compling error for the following code. I think this error is not acceptable to me because int::typeid is a constant and is known to compiler when...
7
22110
by: priyanka | last post by:
Hi there, I had a question. Is there any way of testing a string value in a switch statement. I have about 50 string values that can be in a string variable. I tried cheking them with the if...
11
10836
by: Peter Kirk | last post by:
Hi i have a string variable which can take one of a set of many values. Depending on the value I need to take different (but related) actions. So I have a big if/else-if construct - but what is...
21
7702
by: aaron80v | last post by:
Hi, A simple question: In ANSI-C, how to specify a string as the expression for switch statement. eg switch (mystr) { case "ABC" : bleh... break;
27
2524
by: sinbad | last post by:
how to implement a switch case for stringse...switch should happen depending on the strings... thanks sinbad
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.