mārt1cz
mārt1cz

Reputation: 693

The output result is not what I expected

#include <iostream>

using namespace std;

int main(void)
{
    const int Map_x = 30, Map_y = 30;

    for (int x = 0; x < Map_x; ++x)
        cout << "■";

    cout << endl;

    for (int y = 0; y < Map_y; ++y)
    {
        for (int x = 0; x < Map_x; ++x)
        {
            if (x == 0 || x == Map_x - 2)
                cout << "■";

            cout << " ";
        }

        cout << endl;
    }

    for (int x = 0; x < Map_x; ++x)
        cout << "■";

    cout << endl;

    return 0;
}

I want to use "■" character to print a big square, but seems like the right side of the square messed up, I'm pretty sure it's not the UNICODE problem and I think there's no problem with my code as well. After I replaced "■" character with "#", it did print what I expect which is a big square. I don't know why the right side of the square messed up with the "■" character.

my IDE: Visual Studio 2015 Professional Operating system: Windows 10 Pro 64-bit

here are some screenshots:

When I use "■"

This is what I want

Upvotes: 0

Views: 119

Answers (3)

iBug
iBug

Reputation: 37317

I wrote quite a few console games and I believe it's a rule of thumb to output 2 spaces at one time:

    for (int x = 0; x < Map_x; ++x)
    {
        if (x == 0 || x == Map_x - 2)
            cout << "■";

        cout << "  ";
//               ^^ TWO!
    }

Upvotes: 0

3Dave
3Dave

Reputation: 29081

Pick a different character. Try ASCII 254 ■ rather than a Unicode character.

Characters in the fixed-width font used by the Windows console are NOT square. They're roughly 8x16 including padding. You'd need to approximately double the number of characters for the horizontal axis to get something resembling a square.

See this link for a more extended character codes.

Make sure you're using a sane font for cmd.exe, as well.

I modified your source to:

#include <iostream>

using namespace std;

int main(void)
{
    const int Map_x = 30, Map_y = 30;
    char c = 254;

    for (int x = 0; x < Map_x; ++x)
        cout << c;

    cout << endl;

    for (int y = 0; y < Map_y; ++y)
    {
        for (int x = 0; x < Map_x; ++x)
        {
            if (x == 0 || x == Map_x - 2)
                cout << c;

            cout << " ";
        }

        cout << endl;
    }

    for (int x = 0; x < Map_x; ++x)
        cout << c;

    cout << endl;

    getchar();

    return 0;
}

And I get this:

enter image description here

Upvotes: 0

郑豆浆
郑豆浆

Reputation: 160

Acutally, "■" is not a char but a string literal. When you try to do char c = "■";, then the compiler would give an error as below. Use strlen(const char *str) in #include<cstring> header to measure its length, then I get 3.

1.cpp:7:14: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
     char c = "■";

Upvotes: 1

Related Questions