You cannot use 'switch' on anything except integers**.
you can do this:
unsigned short * sp;
string s;
cin >> s;
sp = (unsigned short *)(&s[0]);
switch *sp
etc. you need to print out and use the constants for "EN" and "FR" in the switch.
s = "EN";
sp = (unsigned short *)(&s[0]);
cout << *sp << endl;
s = "FR";
sp = (unsigned short *)(&s[0]);
cout << *sp << endl;
This will not work if you go past 8 characters using 64 bit integers (or whatever your system supports in the future, some have 128 already) to store the 'text'.
If you ever need to do that, you will need a different solution.
**integers include bool, char, int, long, and so on.
this is a little poorish. Switches can lift performance (avoiding iterative compares for strings, and such as well as performing like a lookup table instead of a jump statement) but if you do not need a performance gain, its unclean. I showed it just because it is almost always possible to rig code to work in a switch if you absolutely need it.
it is better to just say
if(s=="EN") //compares 'E' and 'N' instead of only 1 integer. Gets worse fast for long strings.
code
else
if(s == "FR")
code
else
default code..
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
std::string choixlangue;
cout << "Francais ou anglais" << endl;
cout << endl;
cout << "FR or EN" << endl;
cin >> choixlangue;
if (choixlangue == "FR" || choixlangue == "fr")
{
cout << "Vous avez choisi la langue Francaise" << endl;
}
elseif (choixlangue == "EN" || choixlangue == "en")
{
cout << "You chose English as the language" << endl;
}
else
{
cout << "Vous deviez choisir entre seulement 2 langues, vous n'avez pas d'autre choix" << endl;
}
}
Note you could make it have a switch statement as well, but you then need to have connecting logic to convert the string into an enum, and I don't see the point of that, but could show an example of it if you want.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
std::string choixlangue;
cout << "Francais ou anglais" << endl;
cout << endl;
cout << "FR or EN" << endl;
cin >> choixlangue;
enum langue {XX, FR, EN};
langue lang = XX;
if (choixlangue == "FR" || choixlangue == "fr")
{
lang = FR;
}
elseif (choixlangue == "EN" || choixlangue == "en")
{
lang = EN;
}
switch (lang)
{
case FR:
cout << "Vous avez choisi la langue Francaise" << endl;
break;
case EN:
cout << "You choosed english as language" << endl;
break;
default:
cout << "Vous deviez choisir entre seulement 2 langues, vous n'avez pas d'autre choix" << endl;
break;
}
}
PS: It might not be obvious to a beginner, but your code doesn't have Windows-specific features in it (it is multi-platform), so you might get more responses if you post in the Beginners section, and not in an Operator System-specific section.
Su user will not be able to choose the language by using the command EN or FR, only by using 1 or 2 ?
A standard "s w i t c h" statement requires an integer. Text cannot be directly converted to an integer for words. You can force words into integers, or avoid s w i t c h.
that is a lot of energy to get your enum. You did not like the pointer magic? :(
etc. you need to print out and use the constants for "EN" and "FR" in the switch.
One minor point: if you do it this way, your code will rely on the endian-ness (the order in which the computer stores bytes in a short) of your computer. In other words, the code becomes less portable. It's easy to fix, but I thought I'd mention it.
Switches can lift performance
This is absolutely true and very important to remember, but in this case, it doesn't matter. Since this code is part of the user interface, performance doesn't matter. On a modern computer the program will compare the strings and start running the corresponding code long before the Enter key hits the bottom of the keyboard.
yea its bound by endian (and unicode issues as well). Its not really a great way to do it. If you provide both keys, it will trip on NE and RF. If you tailor it to your hardware, its not as portable.
this is all there is to it Ganado. It force crams the first 2 bytes in the string's char array as a 16 bit integer, which you can use to switch. The string to value is essentially free, one assignment statement.
There's a possible use-case for multicharacter literals here. The usual disclaimers apply, but this program violates strict aliasing as-is so there's already implementation-dependence (since -fno-strict-aliasing is required).
1 2 3 4 5
switch(*sp)
{
case'en': // maybe a little clearer
cout << "english\n";
}