Nayana Adassuriya
Nayana Adassuriya

Reputation: 24766

print a filled square in console

I need to print a filled square in Linux terminal using my C++ program (1cm x 1cm size). I tried to use ASCII 254 (■), but in terminal it print as garbage character. I'm not sure how to print extended ASCII character using c++. Here are two methods I have tried to print extended ASCII. but not succeed.

First method

for(int i=128; i< 255; i++ )
{
 std::cout << static_cast<char>(i) << std::endl;
}

Second method

unsigned char temp = 'A'
for(int i=65; i< 255; i++ )
{
 std::cout << temp++ << std::endl;
 std::wcout << temp << std::endl;
}

Any suggestion or alternative Idea?

Upvotes: 8

Views: 27774

Answers (5)

Praveen Gali
Praveen Gali

Reputation: 19

To get output as you want, try this:

#include<iostream>
#include<windows.h>
using namespace std;

void setconsolecolor(int textColor, int bgColor) 
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (textColor + 
(bgColor * 16)));
}

int main()
{
cout<<"The chunk of Blocks in the colors : \n";
cout<<"                                    ";
setconsolecolor(0,9);
cout<<"   ";
setconsolecolor(0,4);
cout<<"   ";
setconsolecolor(0,8);
cout<<"   \n";
setconsolecolor(0,0);
return 0;
}

Check OUTPUT here.

Upvotes: 2

Hemanth Kollipara
Hemanth Kollipara

Reputation: 1141

Like Sebastian Kuczyński has suggested , we could use that to do great stuff like bar graphs , hostogram etc. Its very cool.

Code

printf("\n\nHistogram of Float data\n");
    for (i = 1; i <= bins; i++)
    {
        count = hist[i];
        printf("0.%d |", i - 1);
        for (j = 0; j < count; j++)
        {
            printf("%c", (char)254u);
        }
        printf("\n");
    }

Output

Histogram of Float data
0.0 |■■■■■■■■■■■■■■■■■■■■■■
0.1 |■■■■■■■■■■■■■■■■
0.2 |■■■■■
0.3 |■■■■■■■■■■■■■■
0.4 |■■■■■■■■
0.5 |■■■■■■■■■■■■■■■■
0.6 |■■■■■■■■■■
0.7 |■■■■■■■
0.8 |■■■■■■■■■■■■■■■
0.9 |■■■■■■■

Upvotes: 1

Andrew
Andrew

Reputation: 9

Try this :

char t = -2;
cout << t;

Upvotes: 0

Sebastian Kuczyński
Sebastian Kuczyński

Reputation: 170

Or try just:

std::cout << (char)254u;

Upvotes: 6

Jens A. Koch
Jens A. Koch

Reputation: 41776

Try using the unicode cout << "\u25A0";

http://www.fileformat.info/info/unicode/category/So/list.htm

Upvotes: 11

Related Questions