A Question on C++ DOS Console Programming

Started by
17 comments, last by Radagar 21 years, 10 months ago
Hey Everyone! I''m new to C++, teaching myself, and learning how OOP and Classes work. I have three questions. 1) Where can I get a good set of routines for clearing the screen and displaying colored ASCII text? This type of stuff is simple to do in BASIC and EUPHORIA. I''m currently Creating a text-based Zork-like Adventure RPG and I would like to spruce it up a little. No Graphic applications are needed, just colored text and easy clearing of the screen. 2) When using Classes for OOP, I''ve been structuring like this... A ROOMS class which contains all the flags and settings of the room, including the map x,y coords. A Map Array containing many ROOMS A CREATURE class which will control the player and the NPC''s WEAPON, ARMOR, and ITEM classes to track all the stats on said objects. But.. I''m having trouble getting it all to come together.. Any suggestions? 3) Finally, as I''m used to Strings in BASIC, I''m having a LOT of trouble getting them to work right in C++. I can''t get a function to return a string right, and I''m a little confused about how the name of a string is simply a pointer to the first element of that string. Pointers and Linked Lists are still a little fuzzy too. Does anyone have any easy-to-follow code snippets that show good string use? Thanks!
WyrmSlayer RPG - In Early Development
Advertisement
if you are using BORLAND'S compiler then you can use conio.h and dos.h to respectively insert colored texts and clear the screen using system("cls");

using mvc++ i dunno how to do to insert colored texts but the dos.h works the same way too...

[edited by - Metal Typhoon on June 10, 2002 2:02:08 PM]
Metal Typhoon
quote:Original post by Metal Typhoon
if you are using BORLAND''S compiler then you can use conio.h and dos.h to respectively insert colored texts and clear the screen using system("cls");

using mvc++ i dunno how to do to insert colored texts but the dos.h works the same way too...

[edited by - Metal Typhoon on June 10, 2002 2:02:08 PM]


I am using MSVC++ 6.0 (DevStudio). I''m having trouble getting the ''system'' command to work, and the conio option doesn''t work with MSVC++. Any other suggestions would be appreciated. Thanks!



~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
For Example, the following doesn''t work in my MSVC++ Compiler. It Compiles, but does not clear the screen.


  #include <iostream.h>#include <process.h> //For the system command, not dos.h#include <stdio.h> //For the fflush commandint main(){	cout << "testing\n";	fflush (stdout); //Help files said this was required	system("cls");	cout << "did it work?\n";	return 0;}  


Help?

~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
Here are fast clear screen and color functions...


    void clrscr(){	static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);	COORD coord = {0, 0};	DWORD dw;	CONSOLE_SCREEN_BUFFER_INFO csbi;	DWORD dwSize;	GetConsoleScreenBufferInfo(hConsole,&csbi);	dwSize = csbi.dwSize.X * csbi.dwSize.Y;	FillConsoleOutputCharacter(hConsole, 							   ' ', 							   dwSize, 							   coord, 							   &dw);	FillConsoleOutputAttribute(hConsole,							   csbi.wAttributes,							   dwSize,							   coord,							   &dw);	SetConsoleCursorPosition(hConsole, coord);}void setForeColor(int c){	static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);	WORD wAttrib = 0;	CONSOLE_SCREEN_BUFFER_INFO csbi;	GetConsoleScreenBufferInfo(hConsole,&csbi);	wAttrib = csbi.wAttributes;	wAttrib &= ~(FOREGROUND_BLUE | 				 FOREGROUND_GREEN | 				 FOREGROUND_RED | 				 FOREGROUND_INTENSITY);	if (c & 1)		wAttrib |= FOREGROUND_BLUE;	if (c & 2)		wAttrib |= FOREGROUND_GREEN;	if (c & 4)		wAttrib |= FOREGROUND_RED;	if (c & 8)		wAttrib |= FOREGROUND_INTENSITY;	SetConsoleTextAttribute(hConsole, wAttrib);}void setBackColor(int c){	static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);	WORD wAttrib = 0;	CONSOLE_SCREEN_BUFFER_INFO csbi;	GetConsoleScreenBufferInfo(hConsole,&csbi);	wAttrib = csbi.wAttributes;	wAttrib &= ~(BACKGROUND_BLUE | 				 BACKGROUND_GREEN | 				 BACKGROUND_RED | 				 BACKGROUND_INTENSITY);	if (c & 1)		wAttrib |= BACKGROUND_BLUE;	if (c & 2)		wAttrib |= BACKGROUND_GREEN;	if (c & 4)		wAttrib |= BACKGROUND_RED;	if (c & 8)		wAttrib |= BACKGROUND_INTENSITY;	SetConsoleTextAttribute(hConsole, wAttrib);}  



clrscr accepts no parameters... and it is much quicker than system("cls"). The two color functions sets either the background color or the foreground.. You have the give the function the color you want.. here are the colors (just a list of defines i wrote to make things easier:

        #define BLACK	0#define BLUE	1#define GREEN	2#define CYAN	3#define RED	4#define PURPLE	5#define BROWN	6#define WHITE	7#define GRAY	8#define LBLUE	9#define LGREEN	10#define LCYAN	11#define LRED	12#define LPURPLE	13#define YELLOW	14#define BWHITE	15    


There you go!

[edited by - Coaster Kev on June 10, 2002 2:47:13 PM]
That''s all fine and good, but only applicable if he really meant the Win32 console, and not the actual DOS. And, remember, that only the command processor on Windows 9x is implemented using DOS (command.com, to be specific). The command processor is a full Win32 app on NT, and the actual subsystem on both systems is not DOS.
I am using a Win32 Console App, so this should work, but there is one problem. The code above is reporting quite a few errors that I''m not able to work through.

I think I''m just missing an include. I threw your code above into a header file along with the #defines for the colors. What files will I need to include in the header to make the above code work?

Here are a few of the compiler errors if it helps...

c:\program files\microsoft visual studio\vc98\include\chris.h(20) : error C2146: syntax error : missing '';'' before identifier ''hConsole''
c:\program files\microsoft visual studio\vc98\include\chris.h(20) : error C2065: ''hConsole'' : undeclared identifier
c:\program files\microsoft visual studio\vc98\include\chris.h(20) : error C2065: ''GetStdHandle'' : undeclared identifier
c:\program files\microsoft visual studio\vc98\include\chris.h(20) : error C2065: ''STD_OUTPUT_HANDLE'' : undeclared identifier
c:\program files\microsoft visual studio\vc98\include\chris.h(21) : error C2065: ''COORD'' : undeclared identifier
c:\program files\microsoft visual studio\vc98\include\chris.h(21) : error C2146: syntax error : missing '';'' before identifier ''coord''
c:\program files\microsoft visual studio\vc98\include\chris.h(21) : error C2065: ''coord'' : undeclared identifier

~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
You''ll need to include windows.h
I'm learning, just like the best of us...Ok, now assume a spherical cow... :)
MANY Thanks Coaster Kev, and Thanks Soulkeeper as well. This works for my purposes. I had to figure out the RIGHT way to flush the stream first, but got it ( cout.flush() ). Now I can clear the screen and display text in color.

WOOHOO!

~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
I dont know if has been said but to clear the screen you use system("cls"); Im pretty sure....

This topic is closed to new replies.

Advertisement