using switch with string

Nov 20, 2012 at 7:39am
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 Nov 20, 2012 at 7:47am
Nov 20, 2012 at 7:49am
Maybe you could solve using 'enum'. Later I'll try to post something, yet meanwhile try to find something by yourself. :)
Nov 20, 2012 at 8:36am
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 Nov 20, 2012 at 9:16am
Nov 20, 2012 at 9:14am
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:
    // ...
}

Nov 20, 2012 at 9:34am
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).
Nov 20, 2012 at 9:48am
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 Nov 20, 2012 at 9:50am
Nov 20, 2012 at 10:23am
great, that makes sense! (haha I am a real noob)

thanks very much
Nov 20, 2012 at 10:30am
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 Nov 20, 2012 at 10:30am
Nov 20, 2012 at 12:36pm
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...
Nov 21, 2012 at 2:33am
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
Nov 21, 2012 at 7:57am
:: 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.