【发布时间】:2021-09-09 19:14:23
【问题描述】:
我的目标是一个一个地输出一串字符。但是当我运行它时,它会错开并最终只输出整个字符串,而字符之间没有延迟。我目前正在 Mac 操作系统上运行此代码。
#include <iostream>
#include <thread>
#include <chrono>
/**
Asks user to input name.
@param usrName Displays the the prompt asking for their name.
*/
void displaySequence(std::string usrName);
int main() {
std::string prompt = "Name: ";
std::string clientName;
displaySequence(prompt);
std::cin >> clientName;
return 0;
}
void displaySequence(std::string usrName) {
for (int i = 0; i < (int) usrName.length(); i++) {
std::cout << usrName.at(i) << " ";
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
}
【问题讨论】:
-
20 毫秒是一个非常短的延迟。每秒 50 个字符。这就是你想要的吗?
标签: c++ c++11 visual-c++ c++17