How to prevent '\ n' being written to the console

0

Hello, I have this code:

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

using namespace std;

bool gotoxy(const WORD x, const WORD y) {
    COORD xy;
    xy.X = x;
    xy.Y = y;
    return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), xy);
}

int main() {
    system("MODE CON COLS=20 LINES=30");
    gotoxy(0, 29);
    for(int i=0; i < 20; ++i) {
        cout << "a";
    }
    return 0;
}

This code works fully, with a problem: 'a' characters are not printed on the last line but on the penultimate one. I think this behavior is due to a line break that is inserted. Does anyone know a way to get around this behavior? Thanks!

    
asked by anonymous 10.07.2018 / 21:29

1 answer

0

This question was answered in the chat Stack Overflow . In the chat I was given the WriteConsoleOutputA function, which I'll cite:

  

WriteConsoleOutput has no effect on the cursor position.

What this means is that this function does not interfere with the position of the cursor. Here is an example of how to use this function, according to the initial question:

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

using namespace std;

int main() {
    system("MODE CON COLS=40 LINES=30");
    HANDLE wHnd;
    int i;
    CHAR_INFO charInfo[40];

    wHnd = GetStdHandle(STD_OUTPUT_HANDLE);

    // play with these values 
    COORD charBufSize = {40, 1};         // do not exceed x*y=100 !!!
    COORD characterPos = {0, 0};        // must be within 0 and x*y=100
    SMALL_RECT writeArea = {0, 29, 39, 39};


    for (i = 0; i < (40); i++) {
        charInfo[i].Char.AsciiChar = ' ';
        charInfo[i].Attributes = BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY;
    }

    WriteConsoleOutputA(wHnd, charInfo, charBufSize, characterPos, &writeArea);
    sleep(5);
    return 0;
}
    
11.07.2018 / 18:23