switch statement with the string being tested

P

priyanka

Hi there,

I had a question. Is there any way of testing a string value in a
switch statement. I have about 50 string values that can be in a string
variable. I tried cheking them with the if else statements but it
looked pretty ugly and the string values to be tested is still
increasing. The switch statement seems to be a better choice then the
if else statement. But it seems that the switch statement accepts
integer values as the test condition. Can anybosy help me here or give
some idea ?

For eg,

switch(nameofInstitution){
case UofCanada: cout << " U of canada" << endl;
break;
case UofIndia: cout << " U of India" << endl;
break;

...................
.................
.....................

about 50 such cases
default: cout << " Sorry, this university is not listed"
<< endl;
die();
break;
}

Thank you,
Priya
 
A

axel22

priyanka je napisao/la:
Hi there,

I had a question. Is there any way of testing a string value in a
switch statement. I have about 50 string values that can be in a string
variable. I tried cheking them with the if else statements but it
looked pretty ugly and the string values to be tested is still
increasing. The switch statement seems to be a better choice then the
if else statement. But it seems that the switch statement accepts
integer values as the test condition. Can anybosy help me here or give
some idea ?

For eg,

switch(nameofInstitution){
case UofCanada: cout << " U of canada" << endl;
break;
case UofIndia: cout << " U of India" << endl;
break;

..................
................
....................

about 50 such cases
default: cout << " Sorry, this university is not listed"
<< endl;
die();
break;
}

Thank you,
Priya

Well, you could make a function that generates some integer value (or a
hashvalue) based on the parameter string, and then compare these values
inside a switch statement like this:

switch (generateValue(nameOfInstitution)) {
case 11241: ...
default:
}

Trouble is, you'd have to be sure your function generates unique value
for each string.
 
T

Thomas J. Gritzan

priyanka said:
Hi there,

I had a question. Is there any way of testing a string value in a
switch statement. I have about 50 string values that can be in a string
variable. I tried cheking them with the if else statements but it
looked pretty ugly and the string values to be tested is still
increasing. The switch statement seems to be a better choice then the
if else statement. But it seems that the switch statement accepts
integer values as the test condition. Can anybosy help me here or give
some idea ?

You can't test for string values in a switch statement.

Maybe you could use an associative container: std::map.
Depends on what you want to do.
 
A

axel22

Thomas J. Gritzan je napisao/la:
You can't test for string values in a switch statement.

Maybe you could use an associative container: std::map.
Depends on what you want to do.

Yes, probably a better solution. And since the map is implemented as a
rb tree if I'm not mistaken, probably a fast enough as well.
 
P

peter koch

priyanka skrev:
Hi there,

I had a question. Is there any way of testing a string value in a
switch statement. I have about 50 string values that can be in a string
variable. I tried cheking them with the if else statements but it
looked pretty ugly and the string values to be tested is still
increasing. The switch statement seems to be a better choice then the
if else statement.
If switch-statements had worked on strings (they might in a newer
revision), it might be better than an if-then-else (although there is
not much of a difference). But the best solution would be some
collection - e.g. a std::map.
For eg,

switch(nameofInstitution){
case UofCanada: cout << " U of canada" << endl;
break;
case UofIndia: cout << " U of India" << endl;
break;

..................
................
....................

about 50 such cases
default: cout << " Sorry, this university is not listed"
<< endl;
die();
break;
}
Could be compacted to:
map::iter i = universitymap.find(nameofInstitution);
if (i == universitymap.end())
{
cout << " Sorry, this university is not listed" << endl;
}
else
{
cout << i->second << endl;
}

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top