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?
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
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
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.
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.
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.
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++