473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Switch statement help

I'm writing a program and I've made a switch statement. I have several quesions:

1. I have a string variable and when I try to use that string variable with the switch function I get a compiling error that says: switch quantity not an integer
How to I get my switch statement to allow a string variable?

2. Can I make it accept 2 variables (i.e. If i wanted the person to type "go here" each word going into a seperate string variable)? ... maybe like this...

string hi, hi2;
...
switch(hi, hi2){
...etc.

3. Once inside the switch, how do I make a double word(like the ex. above, "go here") response?
ex:

switch(hi, hi2);{
case "go here": ...

-or-

switch(hi, hi2);{
case "go" "here": ...

or is it something completely different.


NOTE: I'm super new at C++ and I'm sorry if I'm not making a lot of sense, hopefully you'll get what I'm asking. Also since I'm new at this, please try to use basic C++ terminology that I would be able to understand.

Thank you.
Dec 11 '06 #1
3 3025
sivadhas2006
142 100+
Hi,

In C++, you can't pass a string variable to the switch statement.

You can pass only the integer variable.

Here is an example of using the switch statement.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream.h>
  3.  
  4. #define  CHOICE_INCHES  1
  5. #define  CHOICE_METERES 2
  6. #define  CHOICE_QUIT    3
  7.  
  8. int main ()
  9. {
  10.    int
  11.       nChoice = 0;
  12.  
  13.    do
  14.    {  
  15.       cout <<"Learn using switch case and do-while loop." << endl;
  16.       cout <<"------------------------------------------" << endl;
  17.       cout <<"\n1. Inches\n2. Meteres\n3. Exit\n";
  18.       cout<<"\nPlease Enter the choices as shown above : ";
  19.       cin >> nChoice;
  20.  
  21.       switch(nChoice)
  22.       {
  23.          case CHOICE_INCHES:
  24.          {
  25.             cout << "Inches\n\n";
  26.             break;
  27.          }
  28.  
  29.          case CHOICE_METERES:
  30.          {
  31.             cout << "Meters\n\n";
  32.             break;
  33.          }
  34.  
  35.          case CHOICE_QUIT:
  36.          {            
  37.             break;
  38.          }
  39.  
  40.          default:
  41.          {
  42.             cout<< "Sorry, You have entered invalid choice.\n\n";
  43.          }
  44.  
  45.       }
  46.    }
  47.    while(nChoice != CHOICE_QUIT);
  48.  
  49.    return 0;
  50. }
  51.  
  52.  
Regards,
M.Sivadhas.
Dec 11 '06 #2
Thanks. I think I've got a back up plan ;)
Dec 11 '06 #3
Ganon11
3,652 Expert 2GB
You could always take the first character of your first string and compare that. Characters can be represented as integers and, thus, can be used in switch statements.
Dec 12 '06 #4

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

Similar topics

35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
17
by: prafulla | last post by:
Hi all, I don't have a copy of C standard at hand and so anyone of you can help me. I have always wondered how switch statements are so efficient in jumping to the right case (if any)? Can...
11
by: Scott C. Reynolds | last post by:
In VB6 you could do a SELECT CASE that would evaluate each case for truth and execute those statements, such as: SELECT CASE True case x > y: dosomestuff() case x = 5: dosomestuff() case y >...
19
by: rdavis7408 | last post by:
Hello, I have four textboxes that the user enters the price per gallon paid at the pump, the mileage per gallon and I would like to then calculate the cost per gallon and use a switch statement to...
2
by: Macca | last post by:
Hi, I have a switch statement that has 5+ case statements. Each of these case statements copies form one array to another. Rather than doing a separate try..catch statement for each case...
4
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...
5
by: Phuff | last post by:
Hey all, I need some direction help. I have a switch case statement that is seemingly my only option right now, but its too large and not easy to maintain the code. Here goes... I have part...
12
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
2
osward
by: osward | last post by:
Hello there, I am using phpnuke 8.0 to build my website, knowing little on php programing. I am assembling a module for my member which is basically cut and paste existing code section of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.