samedi 9 septembre 2023

something went wrong when I tried to apply #define in a function

I tried to apply #define in a conditional statement, however, it seems that something went wrong.

    // The mode is forward by default
    #define FOR

    if ( mode == "forward")
    {
        clog << "mode == forward" << endl;
    }

    else if (mode == "reverse")
    {
        clog << "Enter reverse" << endl;

        #define REV
        #undef FOR
    }
    else 
    {
        cerr << "NOT A VALID MODE (FORWARD OR REVERSE)" << endl;
    }

    #ifdef FOR
    cout << "Switch to forward mode." << endl;
    #endif

    #ifdef REV
    cout << "Switch to reverse mode." << endl;
    #endif

and the output is

mode == forward
Switch to reverse mode

Isn't this contradictory?

I think it may be the problem of compiling. After precompiling, the code looks like this

int main()
{
    string mode = "forward";
    if ( mode == "forward")
    {
        clog << "mode == forward" << endl;
    }

    else if (mode == "reverse")
    {
        clog << "Enter reverse" << endl;



    }
    else
    {
        cerr << "NOT A VALID MODE (FORWARD OR REVERSE)" << endl;
    }






    cout << "Switch to reverse mode." << endl;

}

I'm just new to C++, can anyone tell me why this could happen?

Aucun commentaire:

Enregistrer un commentaire