新行从我的hackertyper程序中的上一行结束的地方开始。

huangapple go评论53阅读模式
英文:

The newline starts where the previus line ends in my hackertyper program

问题

以下是翻译好的部分:

我正在开发一个项目,当按下键盘上的键时显示字符,就像hackertyper网站上一样。但是当显示另一行时,它会将其对齐到前一行的末尾。

以下是应该显示的行:

Hello,          World!
This is a multi-line string.

以下是它们实际显示的方式:

Hello,          World!
                      This is a multi-line string

以下是代码:

#include <iostream>
#include <thread>
#include <chrono>
#include <ncurses.h>
using namespace std;

int main() {
    string text = "Hello,          World!\nThis is a multi-line string.";
    // 初始化ncurses
    initscr();
    // 启用立即输入模式,以实时捕获按键
    cbreak();
    // 禁用显示输入字符,以避免显示乱码
    noecho();

    for (char c : text) {
        if (c == ' ') {
            cout << c << flush;
        }
        else {
            getch();
            cout << c << flush;
        }
    }

    endwin();
    return 0;
}
英文:

I am working on this that shows a character when a key is pressed, like on the hackertyper website. But when another line is shown, it aligns it at the end of the previus line.

Here's how the lines should show:

Hello,          World!
This is a multi-line string.

Here's how they're shown:

Hello,          World!
                      This is a multi-line string

And here's the code:

#include &lt;iostream&gt;
#include &lt;thread&gt;
#include &lt;chrono&gt;
#include &lt;ncurses.h&gt;
using namespace std;

int main() {
    string text = &quot;Hello,          World!\nThis is a multi-line string.&quot;;
    // Initialize ncurses
    initscr();
    // Enables immediate input mode for capturing key presses in real-time.
    cbreak();
    // Disables showing your input characters, so it won&#39;t be gibberish
    noecho();

    for (char c : text) {
        if (c == &#39; &#39;) {
            cout &lt;&lt; c &lt;&lt; flush;
        }
        else {
            getch();
            cout &lt;&lt; c &lt;&lt; flush;
        }
    }

    endwin();
    return 0;
}

答案1

得分: 0

我通过在else语句内部添加另一个if语句来解决了这个问题,像这样:

getch();
if (c == '\n') {
    cout << '\r';
}
cout << c << flush;
英文:

I fixed the issue by adding another if statement inside the else statement like this:

getch();
if (c == &#39;\n&#39;) {
    cout &lt;&lt; &#39;\r&#39;;
}
cout &lt;&lt; c &lt;&lt; flush;

huangapple
  • 本文由 发表于 2023年5月14日 00:51:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定