Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8752619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:16:13+00:00 2026-06-13T13:16:13+00:00

I’m trying to compare two strings One string is a normal string that I

  • 0

I’m trying to compare two strings

One string is a normal string that I got from an istringstream:

string command;
iss >> command;

and the other is a string that I get from my map.

But it does not seem to be working.

The idea is that I have a map<string, string>, the value is to be an
escape code to change the terminal output colour.

I have a map in my main that I put all the keys and values into
(which works, I’ve tested that) so I pass the map into this function.

I also have defined colours at the top of my program:

#define BLACK "\033[22;30m"
#define RED "\033[22;31m"
#define GREEN "\033[22;32m"
#define BROWN "\033[22;33m"
#define BLUE "\033[22;34m"
#define PURPLE "\033[22;35m"
#define CYAN "\033[22;36m"
#define GREY "\033[22;37m"
#define DARK_GREY "\033[01;30m"
#define LIGHT_RED "\033[01;31m"
#define LIGHT_GREEN "\033[01;32m"
#define YELLOW "\033[01;33m"
#define LIGHT_BLUE "\033[01;34m"
#define LIGHT_PURPLE "\033[01;35m"
#define LIGHT_CYAN "\033[01;36m"
#define WHITE "\033[01;37m"

This is the part that seems not to be working and confusing me:

string getColor(string command, map<string, string> &m)
{
  string color;
  if((m.find(command)->second).compare(RED) == 0)
  {
    color = RED;
    return color;
  }
  // ...and so on for all the other colors
}

I would have made this a switch, but C++ doesn’t allow switches on strings.

So the problem is, even if the both strings are the same, it’s not working like that.

I realize that m.find() is returning an iterator at the position of what we are looking for.
But then doing m.find()->second should get to the value right?
And the value is a string, isn’t it?

So in short, I’m not sure why the comparison isn’t working.

EDIT:

Ok, here is the config file I was talking about:

bold           \e[0;31m 
italic         \e[0;34m
underline      \e[0;32m 
default        \e[0;37m

so the user input would be something like:

(default this is a(bold simple) example.)

so when I get the command “default”, I will search in my map for that string, and then get the colour code associated with it.

then I will change the output colour to the colour in the map associated with default.
then I will change the output colour to the colour associated in the map with “bold” to print out the word “simple”, and then I will go back to the previous colour to print out the rest of the sentence.

but because when I do this:

cout << config.find("bold")->second << " hi" << endl;

it doesn’t change the colour, it prints out:

\e[0;31m hi

instead of changing the output colour :/

so I thought I’d do a comparison, because something like this:

cout << RED << "hi" << endl;

will print out hi in the colour red.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:16:14+00:00Added an answer on June 13, 2026 at 1:16 pm

    It sounds like you are trying to do a very complicated map lookup. Wouldn’t this work?

        const string & getColor(string command, const map<string, string> & m)
        {
            map<string,string>::const_iterator i = m.find(command);
            return i == m.end() ? BLACK : i->second;
        }
    

    Of course, this does not address the issue of supposedly equal strings not comparing equal. You will need to post a more complete example for us to be able to answer that, I think. But the first answer about embedded null characters is mistaken. What you have is embedded escape characters, which is fully correct. The only thing I would do different there is to use const char * RED = etc. instead of #defines, since variables are easier to deal with than macros.

    Edit: Seems like the answer I was referring to here disappeared.

    Edit2: How does this compare to what you are trying to do? It works for me, at least:

        #include <cstdio>
        #include <string>
        #include <iostream>
        #include <sstream>
        #include <map>
        using namespace std;
    
        #define BLACK "\033[22;30m"
        #define RED "\033[22;31m"
        #define GREEN "\033[22;32m"
    
        const string & getColor(const string & command, const map<string, string> & m)
        {
            map<string,string>::const_iterator i = m.find(command);
            return i == m.end() ? BLACK : i->second;
        }
    
        int main() {
            map<string,string> cols;
            cols["BLACK"] = BLACK;
            cols["RED"]   = RED;
            cols["GREEN"] = GREEN;
    
            string line, cmd, val;
            while(getline(cin, line))
            {
                stringstream ss(line);
                if(ss >> cmd >> val && cmd == "color:")
                    cout << getColor(val, cols);
                else cout << line << endl;
            }
        }
    

    The input

    This text is default color
    color: RED
    This text is red
    color: GREEN
    This text is green
    

    should produce the colors indicated.

    Edit3: In light of your new information, it seems like the real problem is that your config file contains does not contain actual escape characters, just the characters ‘\’ and ‘e’. Make sure your file actually contains real escape characters. How to generate these varies from editor to editor. In vim, you would type an escape by doing ctrl-V followed by escape. In the worst case, you can generate this by printing them from your own C program – you already have the defines, after all: fprintf(conffile, "bold %s\n", BOLD).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.