• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# A Question on C++ DOS Console Programming

## Recommended Posts

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!

##### Share on other sites
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]

##### Share on other sites
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
~~~~~~~~~~~

##### Share on other sites
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
~~~~~~~~~~~

##### Share on other sites
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]

##### Share on other sites
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.

##### Share on other sites
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
~~~~~~~~~~~

##### Share on other sites
You''ll need to include windows.h

##### Share on other sites
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
~~~~~~~~~~~

##### Share on other sites
I dont know if has been said but to clear the screen you use system("cls"); Im pretty sure....

##### Share on other sites
"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?"

That''s because you aren''t using string, you are using char*. Here''s what you are looking for

  #include <string> //note no .husing namespace std; //just always do this if you include//a header that doesn''t have a .hstring first; //declare a string, just like declaring and intfirst = "whoo"; //assign, no using strcpy for me!string second("hoo!"); //make a variable and initializestring third = "yipee!"; //traditional syntax for the same thingfirst+=second; //now first is "whoohoo!"second = first+" "+third; //now second is "whoohoo! yipee!"if(first==second) //no need to use strcmp   do_something();if(first<second) //no need to use strcmpoh and here''s a nice one:string string_array[10];#include <algorithm>sort(string_array,string_array+10);//sorts the array//note you should be using a "vector" which is a//superior array that can resize and so much more//and the same sort function can sort both arrays and vectors//now there is a problem with string, some functions//that you will want to use take a char*, not a string.//to remedy that string has a member function c_str()string filename("data.txt");ofstream(filename.c_str());

to learn more about strings, vectors, sort, ofstream and so much more read The C++ Standard Library: A Tutorial and Reference by Nicolai M. Josuttis.

##### Share on other sites
A problem....

  #include <string>using namespace std;string first;first = "whoo";cout << first;

This doesn''t work! I get the following Compiler Errors. Again, I''m using MSVC++ 6.0 with a Win32 Console App. Project.

c:\my documents\string test\st.cpp(6) : error C2501: ''first'' : missing storage-class or type specifiers
c:\my documents\string test\st.cpp(6) : error C2371: ''first'' : redefinition; different basic types
c:\my documents\string test\st.cpp(4) : see declaration of ''first''
c:\my documents\string test\st.cpp(6) : error C2440: ''initializing'' : cannot convert from ''char [5]'' to ''int''
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
c:\my documents\string test\st.cpp(8) : error C2143: syntax error : missing '';'' before ''<<''
c:\my documents\string test\st.cpp(8) : error C2501: ''cout'' : missing storage-class or type specifiers
c:\my documents\string test\st.cpp(8) : error C2143: syntax error : missing '';'' before ''<<''
Error executing cl.exe.

~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

##### Share on other sites
If you''re using MSVC and you have the MSDN library, look under the Contents in Platform SDK->Windows Base Services->Files and I/O->Consoles and Character Mode Support for all the information you need to know about writing to a Win32 DOS console.

MSDN library is your friend, though it''s a pain to find what you want in it.

##### Share on other sites
the #include and using namespace std; should be on different lines. I just messed up when formating. The reason is that preprocessor directives (things that start with #) aren''t really C++ code and so they use different rules. So the fact that they are on the same line matters.

##### Share on other sites
quote:
Original post by Anonymous Poster
the #include and using namespace std; should be on different lines. I just messed up when formating. The reason is that preprocessor directives (things that start with #) aren''t really C++ code and so they use different rules. So the fact that they are on the same line matters.

The formatting messed up on my machine as well. They are on seperate Lines..

and to Cgoat... I''m looking there now. MSDN Libary is a pain, but I''ll try it =)

~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

##### Share on other sites
So, anyone know why my string code above didn''t work? seems simple enough... I''m thinking the ''#include <string>'' doesn''t actually have a ''string'' type variable... But I''m not sure.

any suggestions?

~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

##### Share on other sites
correct, it doesn''t, unless you write:
using namespace std;

Make sure that the include and namespace lines are outside of your functions or whatever and that you code is in a function. Also remember that in MSVC++ and most other compilers C++ files end in .cpp, not .c

  #include <string>using namespace std;int main(){    string s;}

##### Share on other sites
''first'' has already been defined in std::. Pick a different name.

##### Share on other sites
You could also do:

char *peh = "Foobar";

or

char peh[255] = "La la la la";

It''s all you need

• ### Forum Statistics

• Total Topics
628277
• Total Posts
2981778

• 10
• 11
• 17
• 11
• 9