Search code examples
c++headerincludeheader-files

C++ pre-processor #include


I'm trying to make my program into multiple languages, at start the user is asked if he wants language1 or language2. User input is stored in a variable and then using the if statement I get what language the user chose.

Like this :

std::cin >> language;

if(language == ENGLISH)
     {
     // Do something
     }
else if(language == SPANISH)
     {
     // Do something else
     }

What I did next is is I stored every function that I want to be translated into two header files, one English and one Spanish, both header files are the exact same, except every output is translated.

Now what i did is something like this

std::cin >> language;

if(language == ENGLISH)
     {
     #include "English.h"
     }
else if(language == SPANISH)
     {
     #include "Spanish.h"
     }

Now, #include is a pre-processor directive so it gets "executed" before the main function, any way around this ?


Solution

  • Now, #include is postprocessor directive so it gets "executed" before the main function

    It is a preprocessor directive. It is not "executed" at runtime; The source is pre-processed before compilation.

    any way around this ?

    There is no way to run pre-processor after the program has been compiled and executed.

    A better approach is to not duplicate the function definitions, but instead call a function to translate messages before printing output. This translation function should map an argument string into a translated one.