Help with a case break C++ console application

I want, that the user get the possibility to write words instead of number like 1 or 2, but im not able, i don't know how, can you help me ?

#include <iostream>

using namespace std;

int main()
{

int choixlangue(2);
cout << "Francais ou anglais" << endl;
cout << "" << endl;
cout << "FR or EN" << endl;
cin >> choixlangue;

enum langue {FR, EN};
switch (choixlangue)
{
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;

}






return 0;
}
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..
Last edited on
Su user will not be able to choose the language by using the command EN or FR, only by using 1 or 2 ?
You can have it so the user can type EN or FR. The important part of jonnin's post is the part after "it is better to just say".

Here's a bit more complete example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
using namespace 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;
    }
    else if (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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
using namespace 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;
    }
    else if (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.
Last edited on
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? :(
Last edited on
1
2
3
sp = (unsigned short *)(&s[0]);

switch *sp

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.
Perhaps if you showed a full example of the pointer magic I might like it more.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 #include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>

using namespace std;
int main()
{
   string en;
   cin >> en;
   unsigned short *sp = (unsigned short*)( &(en[0]));  
   //cout << *sp; //get your enum values
   switch(*sp)
   {
    case 20037:
	case 17742: //endian
	   cout << "english\n";
	   break;
	case 21062:
	case 18002: //endian 
	cout << "french\n";
	   break;
	default: cout << "not supported\n";
   }
}
Last edited on
Heh, neat, @jonnin.

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";
}


Last edited on
I forgot you could to that. That is pretty!
You can avoid all the type punning and implementation specific details.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
using namespace std;

// need c++11 for this, otherwise use a #define macro
constexpr int make_code(char c1, char c2) {
    return (int)c1 << 8 | c2;
}

int main()
{
    std::string choixlangue;

    cout << "Francais ou anglais" << endl;
    cout << endl;
    cout << "FR or EN" << endl;
    cin >> choixlangue;

    int lang = make_code(choixlangue[0],choixlangue[1]);

    switch (lang)
    {
      case make_code('f','r'):
        cout << "Vous avez choisi la langue Francaise" << endl;
        break;
      case make_code('e','n'):
        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;
    }
}

Registered users can post here. Sign in or register to post.