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.:)
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.
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.
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.
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;
}
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.
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