string in switch

Pages: 12
Hi,
Could we use string in switch?
for example:

string num;
getline(num,1000);

switch(num):
{
case'1'
cout<<"....."

case'2':
cout<<"!!!!!"
}
First of all '1' and '2' are not strings and can not be implicitly converted to strings. So you should at least change them to "1" and "2". After that you should also change a C++ compiler to a C# compiler and all will be O'k.:)
and if I want to do it with c++ compiler , what should I do?
(Excuse me for my bad English!!!)
Either you should write

switch(num[0])
{
case'1':
cout<<".....";

case'2':
cout<<"!!!!!";
}

or use if-else statements instead of switch.

Take into account that you may use num[0] even if string num is empty.:)
Last edited on
I want this:
for example:
if user inputs '1',cout<<"........."
(for example )if user inputs"156555; or anything else cout<<"wrong number!"
etc....
if ( num == "1" ) std::cout << ".........";
else if ( num == "2" ) std::cout << "!!!!!";
else std::cout << <<"wrong number!";
I didn't understand!
Sorry to interrupt, I just stumbled across this link, perhaps you've not seen this yet?

http://www.cplusplus.com/faq/sequences/strings/switch-on-string/
What did not you understand? I showed using of if-else statements.
Last edited on
Why do I have to see this link? It is you who should see this link because you do not understand how the switch statement works. All what I said above is valid.
Last edited on
I didn't understand!

that was for your edition in this post:
Take into account that you may use num[0] even if string num is empty.:)


I should use switch because my program have 10 cases!!
and I just wrote for you an example.
I wasn't referring to you vlad from moscow, I was referring to ft95 to reference the link for wider knowledge that's all. I am not disputing your advice to ft95...sorry if I caused a lost in translation.

@ft95
I should use switch because my program have 10 cases!!
and I just wrote for you an example.


I need not any example of using switch because I know how it works.:) And moreover take into account that the example you referenced to uses an integral expression not a std:::string.
Last edited on
uses an integral expression not a std:::string
but maybe user inputs several characters and we should see all of them and compare it with '1'(for one time)
did you get it?
@ft95
but maybe user inputs several characters and we should see all of them and compare it with '1'(for one time)
did you get it?


I have not understood what you said. Characters and std::string are different things. One more an expression in switch statement shall be of type of an enumeration or of integral type.
This is for a menu
user should input a number between and including 1 and 8
default(anything else) :cout<<"INVALID INPUT"
If you are speaking only about numbers represented as strings then there is a simple way to do the task. You can convert the string to for example an int variable and use that variable in the switch.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
std::string num;

std::getline( InStream, num );

int value = std::stoi( num );

switch ( value )
{
   case 1:
      // some code
      break;
   case 2:
     // other code
      break;
   default:
      std::cout << "Error. Invalid number.";
      break;
}
Last edited on
Thank yous so much
This thread is really something.

ft95 wrote:
Could we use string in switch?


The short answer is no. Switch statements can only operate on integral types.

There are a few alternatives to the problem. The simplest solution is to use if/else if/else statements instead of a switch.
http://cplusplus.com/doc/tutorial/control/#if

Alternatively, you can perform some kind of lookup or conversion to an integral type (using maps, for example) but this is typically not worth the effort.
Last edited on
If the requirement is a number between 1 and 8, then a plain char will do just fine. If you need to go up to 10 or 11, then an int, or possible a string converted to an int would work.

I'd vote for the char in this case - in fact that matches the code in the opening post: case'1' is testing a char value
Pages: 12