gpt4 book ai didi

c++ - 带有Ansi转义的C++ ctime

转载 作者:行者123 更新时间:2023-12-01 15:12:48 25 4
gpt4 key购买 nike

#include <iostream>
#include <string>
#include <ctime>

using namespace std;

#define WIDTH 118
#define HEIGHT 60

void clearScreen(void) {
cout << "\033[2J\n";
}

void moveCursor(int row, int column) {
cout << "\033[" << row << ";" << column << "H";
}

void waitDelay(int sec) {
long startTime = time(NULL);
while(1) {
long currentTime = time(NULL);
if (currentTime - startTime > sec) {
break;
}
}
}

int main() {
char message[] = "* * * B R E A K * * *";
int messageLength = (sizeof(message) - 1) / sizeof(message[0]);
int startMessageCoord = (WIDTH - messageLength) / 2;

clearScreen();
for (int i = 0; i < messageLength; ++i) {
moveCursor((HEIGHT - 1) / 2, startMessageCoord + i);
cout << message[i];
waitDelay(1);
}

moveCursor(HEIGHT, 0);
return 0;
}
我的代码仅在注释了“waitDelay(1)”行且idk原因时有效。我的waitDelay函数错误吗?
我希望该消息将逐字母输出,但是该程序将等待20秒(所有字符延迟),然后输出完整消息。

最佳答案

您的函数waitDelay运作良好。错误在这里cout << message[i]cout是缓冲流。您应该使用flush

关于c++ - 带有Ansi转义的C++ ctime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62485553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com