Use of ASCII characters with c ++

0

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

teste.cpp

>
#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 (option I prefer) but also do not know how to use utf-8. So, if I could get it to work with utf-8 as well, thanks (windows 7 ultimate, 32-bit version).

EDITION

I was able to make a "frame" around the console and hide the mouse thanks to the Penachia comment, but there is still a blank line at the end. For this, it could decrease the size of the console, but the 1st line does not appear. Here's the edited code, can anyone 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];
            }
        }
    }
};
    
asked by anonymous 19.04.2018 / 18:10

1 answer

0

1st declare the library:

#include <locale.h>

2nd 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;

}
    
20.05.2018 / 02:48