I know how to change the console text color using this code

HANDLE hConsole;
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, ###)

But suppose I need to change colors frequently and on different output lines. Is there a shorter way to do it, like

cout << "text";

???

Rant: Google searching for C++ commands sucks royal :eek: /rant

No there is no shortcut because changing the color is an os-specific function and ansii standards try to stay clear of those issues. -- but you might be able to write your own ofstream overloaded function that will accomplish that.

But if I write the program so that others can use it on their computer then my overloaded function might not be portable?

It won't be portable anyway -- changing colors is os-specific, ms-windows is NOT the same as *nix or MAC. If you want portability among operating systems you will have to embeed proprocessing symbols in your overloaded function, something like this:

ofstream& operator<<(ofstream& os,const string& color)
{
#if defined(_WIN32)
// blabla
#elif defined(_UNIX)
// blabla
#elif defined(_MAC
// blabla
#endif
}

The above overload probably won't link because there is already an overload function with those parameters in <string>, so you will have to think some kind of parameters.

I think I'll settle on this sample code I made up that sends the string to a function which changes the color then outputs colored string then resets back to color of original text. Thanks for the input :)

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
void redcolor(string red)
{
    HANDLE hConsole;
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, 12);
    cout << red << endl;
    SetConsoleTextAttribute(hConsole, 15);
}
int main(int nNumberofArgs, char* pszArgs[])
{
    char red[256];
    cout << "Please enter a string.\n";
    cin.getline(red, 256);
    redcolor(red);
 
system("PAUSE");
return(0);
}

This way I can just say redcolor(some string); Is that what you were talking about? Is this portable?

Member Avatar for iamthwee

I think I'll settle on this sample code I made up that sends the string to a function which changes the color then outputs colored string then resets back to color of original text. Thanks for the input :)

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
void redcolor(string red)
{
    HANDLE hConsole;
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, 12);
    cout << red << endl;
    SetConsoleTextAttribute(hConsole, 15);
}
int main(int nNumberofArgs, char* pszArgs[])
{
    char red[256];
    cout << "Please enter a string.\n";
    cin.getline(red, 256);
    redcolor(red);
 
system("PAUSE");
return(0);
}

This way I can just say redcolor(some string); Is that what you were talking about? Is this portable?

That is not portable, do you know what portable means? Tee he he.

:o tee hee no I'm not sure what portable means. Just reading other threads I deduced that it meant that the code will work on other computers with software or os different from mine.
:o

Member Avatar for iamthwee

>Just reading other threads I deduced that it meant that the code will work on other computers with software or os different from mine.

Well yes, your code won't work on different operating systems. But are you planning to release your code for the general public? Probably not, so who cares, you're writing just for windows. So that's fine.

Well what the program is for is a drill in common french verbs, and my hope is to have it ready before summer classes start, so that maybe if some of my classmates are interested I can sell it for like $5 each so I have some food money :D . But anyways, I'm on windows, and most likely they will be using windows also, maybe a different version, like win xp home vs win xp office or whatever. So would it still pose a problem?

Member Avatar for iamthwee

Are you telling me you have to earn your food money. I'm onto social services...

Haha. Well, more or less. I worked for the university tutoring mathematics during normal fall and spring semester. But once the semester is over, I don't have a job. The tutoring center is open during the summer, but for some reason they are only letting graduate students tutor for summer. So I'm out of a job. And taking 7 hours of summer courses I REALLY don't want to have to find some other job unrelated to my degree (i.e. mcdonalds, retail, cashier...)
So the point is, even though I do have a meal plan, it is limited and I can only eat during certain hours (I think dinner ends at 7pm). Also, I still have bills to pay, blah blah blah...but I know that if the cash supply gets low, the first thing to suffer will be my diet. (Ramen Noodles, anyone?). So really I'm trying to weasel my way out of getting a summer job, I'll already have enough work in classes.

EDIT: but don't worry, if it gets bad I can always swallow my pride and ask my folks for money. but I hate doing it!!! :angry:

>> But anyways, I'm on windows, and most likely they will be using windows also, maybe a different version, like win xp home vs win xp office or whatever

It will be portable across all versions of MS-Windows since Win95. You should verify by looking up the functions at www.microsoft.com -- the function descriptions tell you which versions of MS-Windows are required.

>>EDIT: but don't worry, if it gets bad I can always swallow my pride and ask my folks for money
You can always get a real job flipping hamburgers, bussing tables, washing cars, etc. :cheesy:

I think a foot is worth like 10k on the black market. That should hold me over for a while...besides, once limb regeneration via stem cells is made public, I'll get my foot back. But, hey, if we'll be able to regenerate limbs, I guess you could say money would be growing on humans! then no one would have to work ever again. assuming the cost of regeneration is less than the profit of your limb on the black market.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.