How to prevent writing ' n' in the console

Asked

Viewed 60 times

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 one problem: caaracters 'a' 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 around this behavior? Thank you!

  • The last line is actually input, no? Where the user is going to type something, then I don’t think it makes sense to write something in the input (or if it’s not the user, that’s where the pointer stopped). If I understand your problem.

1 answer

0


This question was answered in the chat Stack Burst.

In the chat I was given the function WriteConsoleOutputA which, I quote:

Writeconsoleoutput has no Effect on the cursor position.

Which means this function does not interfere with the cursor position.
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;
}

Browser other questions tagged

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