英文:
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 <iostream>
#include <thread>
#include <chrono>
#include <ncurses.h>
using namespace std;
int main() {
string text = "Hello, World!\nThis is a multi-line string.";
// Initialize ncurses
initscr();
// Enables immediate input mode for capturing key presses in real-time.
cbreak();
// Disables showing your input characters, so it won't be gibberish
noecho();
for (char c : text) {
if (c == ' ') {
cout << c << flush;
}
else {
getch();
cout << c << 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 == '\n') {
cout << '\r';
}
cout << c << flush;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论