Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C++ countdown + text color


ghost's Avatar
0 0

i have two simple questions, the first :: how can i change text color? i know that i can do this by using system(); command called "color" but this will change all text in the console, and i need only one word. the second question is, how to make a countdown, and when the count reaches zero (0) then start executing another function. i know how to do the loop, but i don't know how to "write" it. e.g. i heard something about str_replace(); but i don't know the right syntax of it and i don't want to use \b (backspace) thanks for any help


ynori7's Avatar
Future Emperor of Earth
0 0

i dont think you can change the color of just one thing.

as for the countdown, are you asking how to output the current count? based on what you said i'm assuming you want the new count to overwrite the old count instead of just writing it beside or beneath the old count (otherwise you would just use cout).

i'm not entirely sure if this works, as i have never tried, but you could use cout and then use system('cls') to clear the screen before outputting the next number.


ghost's Avatar
0 0

no no, i saw the console app written in c++ which can output different color for each word, but this game doesn't had open-source code to the countdown, only if i include the system("cls"); into my loop, else it's no use. it will only eat the memory for nothing. but i don't want this, because this will delete everything in my console, and i need some info still in there.


reaper4334's Avatar
Member
0 0

Okay, the colour thing can be done and I know how.. I'll just have to boot up my old HDD to find my source.. so I'll do that after this post, just one thing before I do.. are you doing this on Winows? 'cause that way uses the "windows.h" header and will only work on windows :P

For the countdown, I beleive you'll be wanting to use Threading. Haven't tried this in C++ myself, I'd recommend google for that one. Just search for a C++ threading tutorial :P

Reaper


ghost's Avatar
0 0

thanks, and yes, i'm running this on windows


reaper4334's Avatar
Member
0 0

Well obviously include windows.h

#include<windows.h>```

(do the usual using namspace.. etc)
Before creating a fucntion define the handle:
```markupHANDLE hConsole;```

Using which, you can change the colour of text. I think it's easier done with functions so here are the ones I use..
```markupvoid SetColourRed()
{
     hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
     SetConsoleTextAttribute
     (hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
}

void SetColourBlue()
{
     hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
     SetConsoleTextAttribute
     (hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
}

void SetColourGreen()
{
     hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
     SetConsoleTextAttribute
     (hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
}

void SetColourWhite()
{
     hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
     SetConsoleTextAttribute
     (hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
}

void SetColourNormal()
{
     hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
     SetConsoleTextAttribute
     (hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}```

Then just change the colour when you want.
Should work ;)

Reaper

ghost's Avatar
0 0

thank you


mido's Avatar
Member
0 0

http://www.cplusplus.com/articles/Sacha1.html See the first lines, they indicate how to output colored text.

From an elder post for me :

'if youre using Trubo C++ or Borland you can include graphics.h and conio.h and use the "textcolor()" and "textbackground" highvideo() window() etc…'


SET's Avatar

SET

Peumonoultramicroscopicsilico
0 0

How often do u want the color to change on ur text? And if i can find any of my old C++ programs i had way to wait x amounts of miliseconds like 1000 was a second. Hmm i will look that up.

Update

Found a old program one of my first hacking tools i ever made for countdowns i used

Header #include "windows.h"

Command Sleep(x); x = the amount of time 1000 = 1 second


ghost's Avatar
0 0

correct me if i am wrong, but you still need to use the \b character, right?


ynori7's Avatar
Future Emperor of Earth
0 0

you would unless you used a graphics engine and just refreshed the screen every frame.


ghost's Avatar
0 0

ok, so it looks i will not get rid of it… no prob and thanks for your help ;)