You are viewing our Forum Archives. To view or take place in current topics click here.
[C++] HELP - switch statements & strings
Posted:

[C++] HELP - switch statements & stringsPosted:

-Deano
  • Rated Awesome
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Hey :satisfied:

I'm currently trying to go through a specific function that will colour the text in my console application.
I don't want to have to put 'SetConsoleTextAttribute........' every time I am changing the colour as it will be happening many, many times.

I had originally tried using enums to work through this but couldn't do that successfully so I have resorted to this.

Essentially using a character array in a switch statement.

I have omitted the other colour comparisons as solving the yellow switch will obviously be the solution for the rest.

colour = "yellow";

void textColour()
{

          //   This will change the colour to yellow successfully.
  if (colour == "yellow")
  {
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
  }
  return;
 
          //  If I try to use case "yellow":  it does not work.

  switch (colour)
  {
  case "yellow":
    {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
    }
  }
return;
}


By trying to use the switch statement, it wants to have a 'const char *' instead of 'char *' as seen [ Register or Signin to view external links. ]

How am I able to get it so that I can use the switch statement for this specific char array instead of having to perform multiple if-statements?
#2. Posted:
Imp
  • Shoutbox Hero
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Good one this.

Basically strings do not work in switch statements, without going into huge debateable discussion on it look here for a good explanation. [ Register or Signin to view external links. ]

Taken from this post, the way you can get it to work is below.


colour = "yellow";

enum string_code {
    eYellow,
    eRed,
    eGreen,
    eBlue,
    ...
};

string_code GetColurEnum (std::string const& inString) {
    if (inString == "yellow") return eYellow;
    if (inString == "red") return eRed;
    ...
}

void textColour() {
    switch (GetColurEnum(colour)) {
    case eyellow:
        SetConsoleTextAttribute(GetStdH andle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
        break;
    case ered:
        ...
    }
    return;
}
#3. Posted:
-Deano
  • PC Master Race
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Alright, thanks a bunch man. I managed to figure out how to get the enums working earlier, but your version makes me understand it better as well.

So pretty much, the only way to do switch with strings is to enum it out?
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.