managing typical black dos output screen in c++

This is a discussion on managing typical black dos output screen in c++ within the C++ Programming forums, part of the General Programming Boards category; hello everyone. i am new in c & c++. i want to know is their any built in function in ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    25

    managing typical black dos output screen in c++

    hello everyone. i am new in c & c++. i want to know is their any built in function in c++(i am using dev c++) to print output vertically on o/p screen.
    for ex:-
    at first i want to print(1,2,3 vertically and then a message horizontally)
    1
    2
    3
    three elements

    and after that
    4
    5
    6
    7
    four elements

    but the problem is i want 4 and 1 at the same horizontal level.so the net o/p will be

    1 ----------------- 4
    2 ----------------- 5
    3 ----------------- 6 //-------- represents space
    three elements ---- 7
    ------------------- four elements.

    i know using \n or endl i can go to a new line but the problem is how to go back to the top.
    Last edited by shikhardeep; 10-24-2011 at 12:31 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    25
    thanks a lot

  4. #4
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    I piced this together some years ago with examples from books and a few other sites, hope it helps you.
    ConLib.h
    Code:
    /* 'ConLibf.h' */
    #pragma once
    #include <windows.h>
    #include <iostream>
    
    enum ConColor
    {
    	ConBlack  = 0,
    	ConRed    = 1,
        ConGreen  = 2,
        ConYellow = 3,
    	ConBlue   = 4,
    	ConPurple = 5,
    	ConCyan   = 6,	
    	ConWhite  = 7,
    };
    
    class ConObject
    {
        public:
            WORD    m_TextColor;
            WORD    m_BackgroundColor;
            COORD   Position;
            ConObject();
            ~ConObject();
            void SetBackgroundColor(WORD Color);
            void SetTextColor      (WORD Color);
    		void SetDefaultColor   ();
            void SetPosition       (COORD Position);
    		void SetPosition       (WORD x, WORD y);
    
    		std::string data;
    };
    
    class ConLib: public ConObject
    {
        private:
            // Screen and keyboard handles
            HANDLE m_Screen;
            HANDLE m_Keyboard;
    
        public:
            ConLib();
            ~ConLib();
            // Set attributes
            void SetTitle          (char* Title);
    
            // Output methods
            void Clear(void);
            void OutputString( char* String);
    
            // Input methods
            void Read( char* Buffer, DWORD BufferSize);
            __int32 GetKey(void);
    
            ConLib & operator << (COORD Pos);
            ConLib & operator << (char* pString);
            ConLib & operator >> (__int32& key);
    		ConLib & operator << (ConObject& obj);
    };
    ConLib.cpp
    Code:
    /* 'ConLib.cpp' */
    #include "ConLib.h"
    ////////////////////
    //    ConObject   //
    ////////////////////
    ConObject::ConObject()
    {
    	m_TextColor = ConWhite;
    	m_BackgroundColor = ConBlack;
    }
    
    ConObject::~ConObject()
    {
    }
    
    //Set BackGround Color
    void ConObject::SetBackgroundColor( WORD Color )
    {
        m_BackgroundColor = 0;
    
        if ( Color & ConRed )
            m_BackgroundColor |= BACKGROUND_RED;
        if ( Color & ConGreen )
            m_BackgroundColor |= BACKGROUND_GREEN;
        if ( Color & ConBlue)
            m_BackgroundColor |= BACKGROUND_BLUE;
        
    }
    
    //Set Text Color
    void ConObject::SetTextColor( WORD Color )
    {
        m_TextColor = 0;
        if ( Color & ConRed )
            m_TextColor |= FOREGROUND_RED;
        if ( Color & ConGreen )
            m_TextColor |= FOREGROUND_GREEN;
        if ( Color & ConBlue)
            m_TextColor |= FOREGROUND_BLUE;
        
    }
    
    //Set default black and white colors
    void ConObject::SetDefaultColor()
    {
    	this->SetBackgroundColor( ConBlack );
    	this->SetTextColor( ConWhite );
    }
    
    //Set Cursor position
    void ConObject::SetPosition( COORD Pos )
    { 
        Position = Pos;  
    }
    
    void ConObject::SetPosition( WORD x, WORD y )
    {
    	COORD xy;
    	xy.X = x;
    	xy.Y = y;
    	this->SetPosition(xy);
    }
    
    
    ////////////////////
    //     ConLib     //
    ////////////////////
    ConLib::ConLib()
    {
        m_Screen   = GetStdHandle( STD_OUTPUT_HANDLE );
        m_Keyboard = GetStdHandle( STD_INPUT_HANDLE );
        this->SetTextColor( ConWhite );
        this->SetBackgroundColor( ConBlack );
    }
    
    ConLib::~ConLib()
    {
    }
    
    //Set Window Title
    void ConLib::SetTitle( char* Title )
    {
        SetConsoleTitle( Title );
    }
    
    //Clear the screen
    void ConLib::Clear(void)
    {
        COORD Start;
        DWORD Written;
        WORD  Color = 0;
        Start.X = 0;
        Start.Y = 0;
        FillConsoleOutputAttribute( m_Screen, Color, 
                                        80*25, Start, &Written );
        FillConsoleOutputCharacter( m_Screen, ' ', 80*25, Start, &Written );
        SetConsoleCursorPosition( m_Screen, Start );
    	this->data.clear();
    }
    
    
    //Print a string to the screen
    void ConLib::OutputString( char* String )
    {
        DWORD Written;
        WriteConsole( m_Screen, String, (DWORD)strlen(String), &Written, NULL );
    }
    
    // Read a string from the keyboard
    void ConLib::Read( char* Buffer, DWORD BufferSize)
    {
        DWORD Read;
        ReadConsole( m_Keyboard, Buffer, BufferSize, &Read, NULL);
    }
    
    // Get a key from the keyboard
    __int32 ConLib::GetKey(void)
    {
        DWORD Read;
        INPUT_RECORD Event;
    	if( GetNumberOfConsoleInputEvents( m_Keyboard, &Read) ){
    		if( Read ){
    			ReadConsoleInput( m_Keyboard, &Event, 1, &Read);
    
    			if( Event.EventType == KEY_EVENT )
    			{
    				if( Event.Event.KeyEvent.bKeyDown )
    					return Event.Event.KeyEvent.wVirtualKeyCode;
    			}
    		}
    	}
        return 0;
    }
    
    ConLib & ConLib::operator << (COORD Position)
    {
        this->SetPosition( Position );
        return * this;
    }
    
    ConLib & ConLib::operator << (char* pString)
    {
        this->data.append( pString );
        return * this;
    }
    
    ConLib & ConLib::operator >> (__int32& key)
    {
        key = this->GetKey();
        return * this;
    }
    
    ConLib & ConLib::operator << (ConObject& obj)
    {
    	SetConsoleTextAttribute( m_Screen, obj.m_TextColor | obj.m_BackgroundColor );
    	SetConsoleTextAttribute( m_Screen, obj.m_TextColor | obj.m_BackgroundColor );
    	SetConsoleCursorPosition( m_Screen, obj.Position );
    
    	this->OutputString( (char*)obj.data.c_str() );
    	return * this;
    }
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    Prefer std::string instead of char*, or if you truly must, use const char*.
    Make the read function return a std::string, using an internal buffer instead.

    >>this->OutputString( (char*)obj.data.c_str() );
    This is plain wrong.
    It returns const char* because of a reason. Hence, make OutputString accept a std::string or const char*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Quote Originally Posted by Elysia View Post
    >>this->OutputString( (char*)obj.data.c_str() );
    This is plain wrong.
    It returns const char* because of a reason. Hence, make OutputString accept a std::string or const char*.
    Ya, must have missed that, this was piced together years ago.
    Edit:
    よくきくがいい!わらわはてんさいだからね
    what is the "Sai I Haten"?
    I used google translator.
    Last edited by Nor; 10-25-2011 at 05:50 PM.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  7. #7
    C++ Enthusiast M.Richard Tober's Avatar
    Join Date
    May 2011
    Location
    Georgia
    Posts
    56
    Quote Originally Posted by Nor View Post
    what is the "Sai I Haten"?
    "Do you like my hat?"

    Polite and enigmatic, just as you'd expect from Elysia.
    Eventually, I decided that thinking was not getting me very far and it was time to try building.
    Rob Pike, "The Text Editor sam"

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    Quote Originally Posted by Nor View Post
    Ya, must have missed that, this was piced together years ago.
    Edit:

    what is the "Sai I Haten"?
    I used google translator.
    The google translator is wrong.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you run a job without output going to screen?
    By Fremontknight1 in forum Linux Programming
    Replies: 3
    Last Post: 06-12-2007, 12:19 PM
  2. Screen goes black...mouse problem...
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 11-12-2002, 10:47 PM
  3. dos output screen
    By niroopan in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2002, 10:03 AM
  4. output screen
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-03-2002, 04:03 PM

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