Substitute Words During Runtime?

Is there a way to substitute words into the code like #defines based on variables during runtime?
I want to be able to set the text colour of a console application based on an integer input, without having to manually hardcode the 100+ possibilities.
Nope there is no such thing as changing the code during runtime in c++. u can do it with assembly but what you want to do can be done in a simpler way. check this out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <windows.h>
using namespace std;

enum Color {FBLUE=1,FGREEN=2,FRED=4,FDARK=8,
           FCYAN=1|2,FPINK=1|4,FYELLOW=2|4,FWHITE=1|2|4,
           BBLUE=16,BGREEN=32,BRED=64,BLIGHT=128,
           BCYAN=16|32,BPINK=16|64,BYELLOW=32|64,BWHITE=16|32|64,
           DEFAULT=1|2|4|8};

class ConsoleGraphicsEngine //LOL, like it's DirectX or something...
{
      HANDLE console;
     
      public:
      ConsoleGraphicsEngine()
      {console=GetStdHandle(STD_OUTPUT_HANDLE);}
           
      void set_color(Color c)
      {
           WORD attribute=c;
           SetConsoleTextAttribute(console,c);
      }
};

ostream & operator<<(ostream & os, Color c)
{
        ConsoleGraphicsEngine cge;
        c=Color(c^FDARK);
        cge.set_color(c);
        return os;
}

Color operator|(Color c1,Color c2)
{
      Color c=Color(int(c1)|int(c2));
      return c;
}

int main()
{
    cout << FGREEN << "hello" << FWHITE << "," << endl;
    cout << (FYELLOW|BBLUE|BLIGHT) << "everyone" << FRED << "!" << endl;
    cout << FWHITE << "I am the mighty " << FBLUE << "dr" <<FYELLOW
         << "4" << FBLUE << "gon" << FWHITE << "_" << FRED << "l" << FYELLOW
         << "0" << FRED << "rd" << endl;
    cout << DEFAULT;
    
    int choice;
    cout << "choose a color:\n(1)blue\n(2)green\n(3)red" << endl;
    cin >> choice;
    
    Color color;
    switch(choice)
    {
          case 1:
               color=FBLUE; break;
          case 2:
               color=FGREEN; break;
          case 3:
               color=FRED;
    }

    cout << color << "printing with your favourite color!" << endl;
    cout << DEFAULT;

    system("pause");
    return 0;
}
Last edited on
Thanks! I know what I was doing wrong now, I was trying to make an enum that went 1, 2, 3, 4, 5 ... for the colors.
Topic archived. No new replies allowed.