Some DOS text questions :)

This is a discussion on Some DOS text questions :) within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm a newbie C++ programmer, but I have much programming experience. Well I've heard in the Include-file DOS.H there ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    7

    Question Some DOS text questions :)

    Hello, I'm a newbie C++ programmer, but I have much programming experience.

    Well I've heard in the Include-file DOS.H there should be some setxy() thing where you can set where you want to write (eeh my english is not that good )

    Anyways, it doesn't function. How do I:

    1. Clear screen
    2. Set the X and Y postioon
    3. Change text colour?

    This was very easy stuff in the QB language I used before, but seems pretty hard here. Can someone show me? I'm gonig to make a little Text-RPG to practice C++.

    Best regards, Dan

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I suggest doing a search of the board (use the search button in the upper right part of the screen) using the keywords:

    1) gotoxy()
    2) text color
    3) clear screen or clrscr();

    You could also look in the FAQ board for the topics of interest.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    7
    You mean gotoxy(x,y) and clrscr() are the functions? THanks!

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    clrscr() is a non-std function. It will clear the screen. But your compiler may not have it available. It can be used as a search parameter if you want.

    I don't use gotoxy(). It may be goto() instead of gotoxy(), I'm not sure, but it should be searchable as well. You might use cursor position as a different search term, too.

  5. #5
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    Try this:

    Code:
    void dcs(string szText, int X, int Y, WORD color)
    {	
    	HANDLE OutputH;										// This will be used for our handle to the output (basically, holds permission to change the output settings)
    	COORD position = {X, Y};							// Create a COORD and set it's x and y to the X and Y passed in.
    
    	OutputH = GetStdHandle(STD_OUTPUT_HANDLE);			// Get a OUTPUT handle to our screen.
    
    	SetConsoleTextAttribute(OutputH, color);			// Set the text attribute to what ever color that was passed in.
    														// The function SetConsoleTextAttribute takes (the handle to the output, and the color);
    	SetConsoleCursorPosition(OutputH, position);		// Set the cursor position to the desire position passed in.
    
    	cout << szText;										// Now print out what we wanted to at the position that was passed in.
    }
    This war, like the next war, is a war to end war.

  6. #6
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    Then use:
    Code:
    	dcs("Red",  0, 0, FOREGROUND_RED);		// Draw "Red" in the color RED.  Draw "White" in the color WHITE.  If we OR ("|") the colors together, it becomes white.
    	dcs("White",10, 1, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    	dcs("Blue", 20, 2, FOREGROUND_BLUE);	// Draw "Blue" in the color BLUE.
    
    	dcs("Red",  0, 4, BACKGROUND_RED);		// Draw "Red" with a background of RED.  Draw "White" with a background of WHITE.
    	dcs("White",10, 5, BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
    	dcs("Blue", 20, 6, BACKGROUND_BLUE);	// Draw "Blue" with a background of BLUE.
    														// Draw a Red text color with a green background.
    	dcs("Color Text!", 0, 8, FOREGROUND_RED | BACKGROUND_GREEN);
    														// This puts the "Press any key to continue..." on the next line.
    	dcs("", 0, 9, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    system("pause");
    This places color text anywhere you want it to go, in the int X and int Y area.

    You can do it with character placement as well.

    Just switch the DCS to DCC, and switch string szText to char szChar. Then manipulate it

    Code:
    int player_x = 5;
    int player_y = 10;
    
    dcc(2,  player_x, player_y, FOREGROUND_RED);
    that draws a solid smiley face, with the color red at the players location.

    Good luck!
    This war, like the next war, is a war to end war.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    7
    I did this:

    Code:
    #include <iostream.h>
    
    
    int main()
    {
        HANDLE OutputH;									
    	COORD position = {5, 5};					
    
    	OutputH = GetStdHandle(STD_OUTPUT_HANDLE);		
    
    	SetConsoleTextAttribute(OutputH, 5);	
    													
    	SetConsoleCursorPosition(OutputH, position);	
    
    	cout << "Hello World";						
    }
    And it worked, thanks a lot Now i just need to learn how to clear the screen

  8. #8
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    oh, sorry, theres an easier way for drawing single characters!
    Code:
    int player_x = 5;
    int player_y = 10;
    COORD playercoord = {player_x, player_y};
    
    void dcc(char szChar, int loc, WORD color)
    {
     HANDLE OutputH;
    COORD position = loc;
    
    OutputH = GetStdHandle(STD_OUTPUT_HANDLE);
    
    SetConsoleTextAttribute(OutputH, color);
    
    SetConsoleCursorPosition(OutputH, position);
    
    cout << szChar;
    }
    
    dcc(2, playercoord, FOREGROUND_RED);
    This war, like the next war, is a war to end war.

  9. #9
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    peice o' cake

    system("cls");

    or:

    void cs()
    {
    system("cls");
    This war, like the next war, is a war to end war.

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    ^^ don't forget to include <stdlib.h>

  11. #11
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    and <string>
    This war, like the next war, is a war to end war.

  12. #12
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Originally posted by Jamsan
    ^^ don't forget to include <stdlib.h>
    New C++ headers? yep, use: #include <cstdlib>

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    7
    I didn't include any of them, and it still worked, why?

  14. #14
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Most probably because the headers you included already included inside of them the ones you need. Be a good programmer and make sure you have the headers you want. #include them in your code.

  15. #15
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    Yea, don't include anything but what you need. It clutters the program, and takes more time to compile. Good eye, Speedy.
    This war, like the next war, is a war to end war.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text Enhancement from DOS console
    By 3DMatrix in forum C++ Programming
    Replies: 8
    Last Post: 06-06-2004, 09:12 AM
  2. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 07:23 PM
  3. static text in DOS
    By Goof Program in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-29-2002, 02:57 PM
  4. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21