using switch with string

This simple program I made is to rise a student database with only using name as the input. I've tried using it with integer and char and they worked!. This is my first time using switch with string. I wonder if it possible but I got this error -> "switch quantity not an integer" or it's really impossible?

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
#include <iostream>
#include <string>

using namespace std;

int main(){

    string student_name;

    cout << "Input the student's name!" << endl;
    cin >> student_name ;

    switch(student_name){
        case "Andi Aksan Akmal" :
            cout << "Andi Aksan Akmal" << endl;
            cout << "You wont ask" << endl;
            cout << "A+" << endl;
            break;
        case "Many Mandy Manmore" :
            cout << "Many Mandy Manmore" << endl;
            cout << "Department of Graphic Design" << endl;
            cout << "A++" << endl;
            break;
        case "Andi Sahriani Safitri" :
            cout << "Andi Sahriani Safitri" << endl;
            cout << "Department of Japanese Literature" << endl;
            cout << "A" << endl;
            break;
        default :
            cout << "There is no such name in database!" << endl;
        }


    }


any idea?
Last edited on
Maybe you could solve using 'enum'. Later I'll try to post something, yet meanwhile try to find something by yourself. :)
to be honest I don't even really get how to use enum, lately I posted my first assumption for the codes that might be "close" to the answer but I think that was stupid so I deleted it.

these are my new assumption, I guess it's nearly success but still having these errors :

- error : two or more data types in declaration of 'student_name'
- error : switch quantity not an integer

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
int main(){

    string enum{

// I am a little bit dissapointed because I can't use space with this codes
        Aksan = 0,
        Mandy = 1,
        Sahriani = 2
        } nama_siswa;

    cout << "Masukkan nama siswa!" << endl;
    cin >> nama_siswa ;

    switch(nama_siswa){
        case 0 :
            cout << "Aksan" << endl;
            cout << "C++ Literature :p" << endl;
            cout << "A+" << endl;
            break;
        case 1 :
            cout << "Mandy" << endl;
            cout << "Medical Engineering" << endl;
            cout << "A++" << endl;
            break;
        case 2 :
            cout << "Sahriani" << endl;
            cout << "Japanese Literature" << endl;
            cout << "A" << endl;
            break;
        default :
            cout << "No such name!" << endl;
        }


    }


also, with the same whole codes and the string type before the enume changed by "typedef" (I don't know exactly what is this, just saw it on a template that I googled) I got a different errors, but seems pretty good compare to those above

here after interchange string by typedef.
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
int main(){

    typedef enum{
        Aksan = 0,
        Mandy = 1,
        Sahriani = 2
        } nama_siswa;

    cout << "Masukkan nama siswa!" << endl;
    cin >> nama_siswa ;

    switch(nama_siswa){
        case 0 :
            cout << "Aksan" << endl;
            cout << "C++ Literature :p" << endl;
            cout << "A+" << endl;
            break;
        case 1 :
            cout << "Mandy" << endl;
            cout << "Medical Engineering" << endl;
            cout << "A++" << endl;
            break;
        case 2 :
            cout << "Sahriani" << endl;
            cout << "Japanese Literature" << endl;
            cout << "A" << endl;
            break;
        default :
            cout << "No such name!" << endl;
        }


    }


and I got these errors :
-error : expected primary expression before ';' token
-error : expected primary expression before ')' token

Last edited on
Other languages, such as D, would allow you to put a string in a switch(). C and C++ don't.

Anyway, what giuscri meant was probably:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
typedef enum {
    AndiAksanAkmal, // is 0
    ManyMandyManmore, // is 1
    AndiSahrianiSafitri // is 2
} nama_siswa;

int studentId; // 0, 1, 2

cout << "Student ID: ";
cin >> studentId;

switch (studentId)
{
    case AndiAksanAkmal:
    // ...
    break;
    case ManyMandyManmore:
    // ...
}

I see, that's why it should be "int" and I can't use "string" as the input ,,

just probably, is there any similar way to do this? just wondering (it's very complicated if I have to make lot of nested if-else statement).
One way would be to use an array of strings containing the acceptable values. Then do a sequential search through the array. If a match is found, you can use the index of the found item in order to control the action taken.

That may sound complex, but it's more expandable and easier to use with a large number of names than a long series of if-else statements.

And, as the cout message suggests, "There is no such name in database!", normally you wouldn't need to code the actual search as it would be handled by the database instead.
Last edited on
great, that makes sense! (haha I am a real noob)

thanks very much
A nasty but workable solution would be taking Chervil's idea farther, and use an std::map with the std::strings as key, and a function pointer as data. When you find the name, you can call a function associated with it.

Something simpler, would be using std::map with name as key, and things you print as data.

Example for second case:
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
#include <iostream>
#include <map>
#include <string>

int main()
{
	std::map<std::string, std::string> database;

	database["Aksan"] = "C++ literature";
	database["Mandy"] = "Medical engineering";
	database["Sahriani"] = "Japanese literature";
	std::cout << "Who do you want?\nAvailable: ";

	for (std::map<std::string, std::string>::const_iterator ci = database.begin();
		ci != database.end();
		++ci
		)
		std::cout << (*ci).first << ' ';

	std::cout << std::endl;

	std::string name;

	std::getline(std::cin, name);

	if (database.count(name) == 0)
		std::cout << "Sorry, `" << name << "' not in database.";
	else
		std::cout << name << ": " << (*database.find(name)).second;

	std::cout << std::endl;
}


First case would be very similar to this, but instead of printing the second member, you'd call it as a function.

Edit: tags.
Last edited on
For the sake of clarity, I was totally wrong! Or at least I was totally wrong for C++ standards before C++11. I was not sure of my answer, so sorry for that.

I"ve just found this:

http://www.anyexample.com/programming/cplusplus/example_of_using_enum_in_cplusplus.xml

where it's said that
there is no way to automatically convert string to enumerator (without additional array-lookup or switch/if series) in classic C++.

I would agree with you if you would find the last Catfish2's solution as the best one among the ones in this thread.

But I'm a noob, so consider this...
that's okay giuscri I am also a noob and we are learning together. also I agree Catfish2' codes as the most suitable one with what I'm trying to. I just need time to understand the whole codes. until now, I can't even figure out the role of double colon (::) in c++

thanks to all, everything's so helpful
:: is the resolution operator. You use it to get something out of a namespace, or a class, or a struct.

In C++, the contents of the headers are in the std namespace.
If you aren't using namespace std; then you need to write std:: to access functions and objects.
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/topic/com.ibm.vacpp7a.doc/language/ref/clrc05cplr175.htm
http://ideone.com/7hsKdJ
Topic archived. No new replies allowed.