I cant get enum this to work properly on Windows. While compiling on linux it returns expected value, but on windows it returns some random number.
typedef enum wezly {
elektrownie1,
konwencjonalne1,
niekonwencjonalne1,
weglowa1,
jadrowa1,
sloneczna1,
wiatrowa1,
geotermiczna1,
gazowa1,
wodna1,
maremotoryczna1,
maretermiczna1
};
wezly wybor_wezla(string opcja)
{
string bb;
bb = opcja;
if ((bb.compare("[elektrownie]")==0)||(bb.compare("[ELEKTROWNIE]")==0))
return elektrownie1;
else if ((bb.compare("[konwencjonalne]")==0)||(bb.compare("[KONWENCJONALNE]")==0))
return konwencjonalne1;
else if ((bb.compare("[gazowa]")==0)||(bb.compare("[GAZOWA]")==0))
return gazowa1;
else if ((bb.compare("[wodna]")==0)||(bb.compare("[WODNA]")==0))
return wodna1;
// (and so on...)
}
int main()
{
cout << wybor_wezla("[gazowa]");
}
When on linux i get 7, on windows its some random number….
Why can that be ?
Your code is missing a final
elseclause:When none of the
ifclauses are true, it falls off the end of the function without returning a value, which is Undefined Behavior. In this case, it’s returning a garbage value, but worse stuff could happen.Your first action should be to add a final
elseclause. This can return a default value, an error code, throw an exception,abort(), etc., but it cannot do nothing. If you know the value has to be one of a limited set of things, you could change the finalelse ifinto just anelseand assume that if it’s not the firstN-1things, it’s theNththing.Once you’ve done that, you need to figure out why your data isn’t falling into one of the expected cases and fix that.