Click to See Complete Forum and Search --> : can anyone help me with this error message ?


PsSheba
February 5th, 2005, 07:07 AM
The following code yields an error message:

// temp1.cpp

#include <iostream>
using std::cout;

void myM(char *);

int main()
{
myM("june");
return 0;
}
void myM(char *month)
{
switch(month)
{
case 'june':
cout << "\nJune";
}
}



after compilation i recieve an error message saying:

error e2383 switch selection expression must be of integral type in function myM(char *)

can anyone tell me how to change the code so it doesnt yield an error message ?
Thanks !

ZuK
February 5th, 2005, 07:28 AM
it should work this way
#include <iostream>

enum month {
jan, feb, mar // ....
};

void myM(month m);

int main()
{
myM(jan);
return 0;
}
void myM(month m)
{
switch(m)
{
case jan:
std::cout << "\nJanuary";
}
}
K.

PsSheba
February 5th, 2005, 07:35 AM
But i'd rather stick to my code. The example i sent is only a part of a larger code and i cannot change it. Any idea whats wrong with the code i sent ?

ZuK
February 5th, 2005, 07:47 AM
But i'd rather stick to my code. The example i sent is only a part of a larger code and i cannot change it. Any idea whats wrong with the code i sent ?
you cannot do what you want
as the compiler tells you
switch selection expression must be of integral type in function myM(char *)
first mistake:
switch(month) // month is a pointer
second mistake:
case 'june': // 'june' is not a valid construct in C/C++

the closest you can get is something like
void myM(char *month)
{
std::string m(month);
if ( m == "june" )
cout << "\nJune";
else if ( m == "april" )
cout << "\nApril";
}
}
K

Andreas Masur
February 5th, 2005, 10:13 AM
But i'd rather stick to my code. The example i sent is only a part of a larger code and i cannot change it. Any idea whats wrong with the code i sent ?
The 'switch' (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/statem_10.asp) statement can only work on expressions which are of integral type or of a class type for which there is an unambiguous conversion to integral type which isn't the case in your example...

PsSheba
February 6th, 2005, 10:52 AM
Now the picture is clear !