Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
C#
void flight_operator::get_define_flight_num(char namecode){
    cout<<"Enter flight code:  ";
    cin>>namecode;
    switch(namecode){
        case "MH": case "mh":
            cout<<"Make Airlines\n"<<endl;
            break;
        case "FY": case "fy":
            cout<<"Fire Airlines\n"<<endl;
        default:
            cout<<"Invalid code!"<<endl;
            break;
    }

}

C++

Posted

1 solution

What is not clear? A case label should be an integer constant. It could be one of integer types, enumeration type, something like this, but not a string, not an array, or any other more complex object or a pointer to object. You cannot use your "MH", "FY" and the like.

You could find some explanation in thousands of places: http://bit.ly/1lLKRN4[^].

The whole idea of using immediate constants, especially string, it very bad. This kind of code is not supportable. This is actually a big programming anti-pattern:
http://en.wikipedia.org/wiki/Magic_string_(programming)#Magic_strings_in_code[^],
http://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants[^] (yes, magic numbers, too);
see also: http://en.wikipedia.org/wiki/Anti-pattern#Programming[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900