Sign in to follow this  

c++ console application

This topic is 3744 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

I'am a beginner at programming and have been reading c++ without fear. I have been learning how to create console applications with dev-c++ compiler. Eventhough its been fun creating console applications, I have been wondering, is there any way to make the text a different color other than white? And also, I have been makeing note's in the console from the book using cout << "text sentence" << endl; is there any way to make one console application connect to another one? I mean, when the program say's, press any key to continue, instead of closing the program can it change to another console for more note's that i have made? if the answer to these questions are to long to type in the forums can you please post a web site i can look at to answer the questions?

Share this post


Link to post
Share on other sites
not sure if it works the same in dev c as im using vs express but in "wincon.h" it has these defines

#define FOREGROUND_BLUE 0x0001 // text color contains blue.
#define FOREGROUND_GREEN 0x0002 // text color contains green.
#define FOREGROUND_RED 0x0004 // text color contains red.
#define FOREGROUND_INTENSITY 0x0008 // text color is intensified.
#define BACKGROUND_BLUE 0x0010 // background color contains blue.
#define BACKGROUND_GREEN 0x0020 // background color contains green.
#define BACKGROUND_RED 0x0040 // background color contains red.
#define BACKGROUND_INTENSITY 0x0080 // background color is intensified.


in my .h file i included windows.h which should include this

then just created constant values for my colours

static const WORD _dk_red ( FOREGROUND_RED );
static const WORD _dk_green ( FOREGROUND_GREEN );
static const WORD _dk_blue ( FOREGROUND_BLUE );


created an inline function to change the colour

inline void setFgColour( WORD fgColour)
{
getInfo();
csbi.wAttributes &= bgMask;
csbi.wAttributes |= fgColour;
SetConsoleTextAttribute( hOut, csbi.wAttributes );
}

inline void setBgColour( WORD bgColour)
{
getInfo();
csbi.wAttributes &= fgMask;
csbi.wAttributes |= (bgColour << 4);
SetConsoleTextAttribute( hOut, csbi.wAttributes );
}

then pretty much just call these every time the colour needs changing

sorry if i have missed anything there has been a long time since i been through this code

can find the complete code in the draughts program in my blog

stowelly.blogspot.com

Share this post


Link to post
Share on other sites
Here's the thing. C++ (and most other programming languages) doesn't know anything about colors or your computer. As far as the language specification is concerned you don't need to have a monitor or keyboard. The cout and cin streams don't have to read from the keyboard and write to the console - it just happens to be what they do by default.

It's possible - and indeed quite easy to have cin read from a file and cout write to a simple text file. This means that C++ doesn't support any means of changing the console text because it's not meaningful in the context of writing to a file. A pure text file doesn't have fonts, or colors or anything like that, it's just a list of characters. As a result C++ doesn't know anything about these things either.

In order to change the console on windows you need to use operating system functions like the ones posted by Stowelly. This is why it looks so much more complicated that what you've seen before - you're stepping outside the realm of C++ and starting to interact with the operating system.

Share this post


Link to post
Share on other sites
There is a much simpler wrapper for SetConsoleTextAttribute here.


#include <windows.h>
void setcolor(unsigned short color) //The function that you'll use to
{ //set the colour
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon,color);
}

Share this post


Link to post
Share on other sites

This topic is 3744 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this