Click here to Skip to main content
15,867,962 members
Articles / Programming Languages / C++
Alternative
Tip/Trick

C++ -> Drawing Rectangles to Console

Rate me:
Please Sign up or sign in to vote.
2.63/5 (4 votes)
25 Aug 2011CPOL 16.2K   1   7
Smaller code:void DrawRect(int x, int y, int width, int height, int curPosX=0, int curPosY=0){ setxy(x, y); cout << char(201); cout.width(width); cout.fill (char(205)); cout << char(187); setxy(x,height+y); cout << char(200); cout.width(width); cout.fill...
Smaller code:
void DrawRect(int x, int y, int width, int height, int curPosX=0, int curPosY=0)
{
    setxy(x,       y); cout << char(201); 
     cout.width(width); cout.fill (char(205)); cout << char(187);
    setxy(x,height+y); cout << char(200); 
     cout.width(width); cout.fill (char(205)); cout << char(188);
    for(int i = y + 1; i < height+y; i++)
    {
        setxy(x,i);cout << char(186);
        setxy(x + width,i);cout << char(186);
    }
    setxy(curPosX,curPosY);
}


Using ostream::width() and ostream::fill() functions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Freelancer
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMy vote of 4 Pin
Manikandan106-Jun-14 20:56
professionalManikandan106-Jun-14 20:56 
GeneralRe: You don't need &lt;tchar.h&gt; for this one :)</tchar.h> Pin
Caner Korkmaz31-Aug-11 21:18
Caner Korkmaz31-Aug-11 21:18 
GeneralHello, The code is giving errors for cstduio and cstdlib fli... Pin
yogesh4ever29-Aug-11 19:39
yogesh4ever29-Aug-11 19:39 
GeneralRe: I am using the following includes in stdafx.cpp (Visual Stud... Pin
Jose David Pujo29-Aug-11 20:13
Jose David Pujo29-Aug-11 20:13 
GeneralIt's a good way :) Pin
Caner Korkmaz25-Aug-11 0:51
Caner Korkmaz25-Aug-11 0:51 
GeneralRe: Thanks! Pin
Jose David Pujo25-Aug-11 2:08
Jose David Pujo25-Aug-11 2:08 
GeneralI'm trying it now :) Pin
Caner Korkmaz25-Aug-11 0:46
Caner Korkmaz25-Aug-11 0:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.