Public Group

# c++ console application

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

## 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 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 |= fgColour;
SetConsoleTextAttribute( hOut, csbi.wAttributes );
}

inline void setBgColour( WORD bgColour)
{
getInfo();
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 on other sites
holy heck man, iam not understanding very much of this answer, looks like i better keep reading my book but thanks anyways.

##### 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 on other sites
thanks for the tip, i guess the question i asked was a little over my head. its not going to be easy to learn c++

##### 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);}

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633680
• Total Posts
3013304
×