Use of c++ ASCII characters

Asked

Viewed 417 times

0

Good afternoon, I have these codes in c++ and I would like you to explain to me why these codes are sending to the console seemingly random characters:

cpp testing.

#include <iostream>
#include "engine.h"

int main() {
    char x;
    Engine engine;
    engine.startup();
    engine.render();
    std::cin >> x;
    return 0;
}

engine. h

#include <iostream>
#include <unistd.h>
#include <string>

struct symbols {
            unsigned char vertical        = 186;
            unsigned char horizontal      = 205;
            unsigned char up_corner_left  = 201;
            unsigned char up_corner_right = 187;
            unsigned char dw_corner_left  = 200;
            unsigned char dw_corner_right = 188;
            } symbol;

class Engine {
private:
    char size[100] = "MODE CON COLS=80 LINES=40";
    unsigned char screen[40][80];

public:
    void startup() {
        for (int i = 0; i < 40; ++i) {
            for (int x = 0; x < 79; ++x) {
                if (i == 0 || x == 0) {
                    screen[i][x] = 201;
                }
            }
        }
        system(size);
    }

    void render() {
        for (int i = 0; i < 40; ++i) {
            for (int x = 0; x < 80; ++x) {
                std::cout << screen[i][x];
            }
        }
    }
};

Note: Instead of using ASCII I could use utf-8 (which I prefer), but I also don’t know how to use utf-8. So if you can get me up and running with utf-8 as well, I’d appreciate it (windows 7 Ultimate, 32-bit version).

EDITION

I managed to make a "frame" around the console and hide the mouse thanks to the comment from Penachia, but there’s still a blank line at the end. For that, you could decrease the size of the console, but the first line doesn’t appear. Here comes the edited code, someone can help me with this?

#include <iostream>
#include <windows.h>
#include <unistd.h>
#include <string>

void hidecursor () {
    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO info;
    info.dwSize = 100;
    info.bVisible = FALSE;
    SetConsoleCursorInfo(consoleHandle, &info);
}

struct symbols {
    unsigned char space             = 32;
    unsigned char vertical          = 186;
    unsigned char horizontal        = 205;
    unsigned char up_corner_left    = 201;
    unsigned char up_corner_right   = 187;
    unsigned char dw_corner_left    = 200;
    unsigned char dw_corner_right   = 188;
} symbol;

class Engine {
private:
    char size[100] = "MODE CON COLS=80 LINES=41";
    unsigned char screen[40][80];

public:
    Engine() {
    }
    void startup() {
        for (int i = 0; i < 40; ++i) {
            for (int x = 0; x < 80; ++x) {
                screen[i][x] = symbol.space;
            }
        }
        hidecursor();
        for (int i = 0; i < 40; ++i) {
            for (int x = 0; x < 80; ++x) {
                if (i == 0) {
                    if (x == 0) {
                        screen[i][x] = symbol.up_corner_left;
                    } else{
                        if (x == 79) {
                            screen[i][x] = symbol.up_corner_right;
                        } else {
                            screen[i][x] = symbol.horizontal;
                        }
                    }
                }
                if (i == 39) {
                    if (x == 0) {
                        screen[i][x] = symbol.dw_corner_left;
                    } else{
                        if (x == 79) {
                            screen[i][x] = symbol.dw_corner_right;
                        } else {
                            screen[i][x] = symbol.horizontal;
                        }
                    }
                }
                if (i > 0 && i < 39) {
                    if (x == 0 || x == 79) {
                        screen[i][x] = symbol.vertical;
                    }
                }
            }
        }
        system(size);
    }

    void render() {
        for (int i = 0; i < 40; ++i) {
            for (int x = 0; x < 80; ++x) {
                std::cout << screen[i][x];
            }
        }
    }
};
  • 1

    Because the startup function only fills the variable screen when i or x are equal to 0, for other cases screen hasn’t been initialized so it has memory junk.

  • True, I’ll try to arrange according to your tip.

  • Hello friend I have this solution: <a>https://answall.com/a/300381/103802</a>

1 answer

0


1st declare the library:

#include <locale.h>

2º place at the beginning of the main function:
setlocale(LC_ALL,""); // Este comando pega o idioma da maquina que está rodando.

//setlocale(LC_ALL, "Portuguese"); // Caso o idioma da sua máquina não seja Portugues, use este.

#include <iostream>
#include <locale.h>

using namespace std;

int main()
{
    setlocale(LC_ALL,"");

    cout <<"Acento é legal"<< endl;

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.