Advertisement Jump to content
Sign in to follow this  

HIghlighting Specific Text

This topic is 3677 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've been trying to figure this out for a little while now but cannot come up with a solution. Essentially what I would like to do is be able to highlight words in a console application if they are a compass direction, such as NORTH. I'm currently playing around making a text adventure, within this game I have a class called Location which is used to create all of the location within the game. The code that I would like to modify is explained below

// The variable holds the description of the location
string m_strDescription;

This is the function responsible for loading the location's description. This function is passed a file pointer for opening and reading files and a pointer to a location created in another file.
Location::loadDescription(ifstream &getData, Location* pCurrentLocation)
void Location::loadLocationDescription(ifstream &getData, Location* pCurrentLocation)
	// Create some tempory strings for reading in data
	string strLine = "";
	string strTemp = "";

	string strLocation = "<" + m_strLocationName + ">";
	//Return pointer to the beginning of the file and clear the end of file flag

	while (getline(getData, strLine, '\n'))
		// If the current line is equal to the location the application is looking for read in the description
		if(strLine == strLocation)
			getline(getData, pCurrentLocation->m_strDescription, '*');

			getData >> strTemp >> pCurrentLocation->m_Locations[NORTH];
			getData >> strTemp >> pCurrentLocation->m_Locations[SOUTH];
			getData >> strTemp >> pCurrentLocation->m_Locations[EAST];
			getData >> strTemp >> pCurrentLocation->m_Locations[WEST];
			getData >> strTemp >> pCurrentLocation->m_Locations[NORTHEAST];
			getData >> strTemp >> pCurrentLocation->m_Locations[NORTHWEST];
			getData >> strTemp >> pCurrentLocation->m_Locations[SOUTHEAST];
			getData >> strTemp >> pCurrentLocation->m_Locations[SOUTHWEST];


The following function is then used to print the location's description on the screen. As you can see from this example I've experimented with trying to highlight any text that reads NORTH but because the function .find returns only returns true or false, all of the text is highlighted.
void Location::printDescription()
	// Print the location's description
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), REDTEXT);

	cout << "\n\n" << m_strDescription << "\n\n";

Does anyone have any suggestions as to how I could only highlight text that is a compass direction such as NORTH? -Sorry if any of this is poorly explained.

Share this post

Link to post
Share on other sites
I'm not familiar with a lot of the console functions, but it looks from that code that you turn flags on and off (like REDTEXT), which affects all of the following output. So, the first thing that jumps to mind is that you'll need to print part of the string without the highlighting, then turn the highlighting on, then print NORTH, then turn highlighting off, and then print the rest of the string.

Assuming we're talking about STL strings, string::find does not return a bool. It returns a size_t which indicates the position of the substring that you're looking for. It will return string::npos if the substring isn't found. The reason the above code is working the way you describe it is probably because you've always tested it with a string that doesn't start with the word "NORTH", and therefore find() always returns a non-zero value (even if the string didn't contain the word NORTH at all, because npos is usually set to -1, which is non-zero).

So if you have this:

std::string strDescription("Go six paces NORTH and then turn EAST.");

Then a call to strDescription.find("NORTH") will return a value of 13. You can then use string::substr in order to split the string up. strDescription.substr(0, 12) will return string("Go six paces ") and substr(13, 17) will return string("NORTH"). Once you have it split up as such, you can print them one at a time, turning the highlighting flags on and off as needed, in some sort of loop (shouldn't be too difficult to figure out the logic for the loop itself).

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!