我正在自学C++,但我的代码中有一个计时器.正在尝试在前一条消息后3秒对其进行定制
I am teaching myself C++ and got stuck with a timer in my code. Attempting to cout a message 3 seconds after the previous one
我在尝试定制一条延迟3秒的消息时遇到了问题。我可能试图通过使用后藤来艰难地做到这一点。如果有人有任何建议,我们将不胜感激。谢谢
代码:
#include <iostream>
#include <string>
using namespace std;
int seconds = 0;
int timer = seconds + 1;
int clock = timer;
int main() {
string namevar;
cout << "Enter your name: " << flush;
cin >> namevar;
std::cout << "Welcome " << namevar << ". This program was created to teach the user how to code using C++" "n";
clockf1:do {
seconds = timer;
if (clock < 3);
timer = seconds + 1;
goto clockf1;
}
while (clock == 3);
cout << "When learning any coding language the first step is to understand how to print Hello World. So let us begin!" << flush;
}
实际上,您可以用这种方式编写以等待3秒:
#include <bits/stdc++.h>
using namespace std;
using namespace this_thread;
using namespace chrono;
int sec = 0;
int main(){
string namevar;
cout << "Enter your name: " << flush;
cin >> namevar;
cout << "Welcome " << namevar << ". This program was created to teach the user how to code using C++" "n";
do{
sleep_for(1s);//sleep for 1 sec
sec++;
cout << sec <<endl;
}while (sec<3);
cout << "When learning any coding language the first step is to understand how to print Hello World. So let us begin!" << flush;
return 0;
}
您可以在前面的文章中阅读更多关于this_thread
和chrono
的内容。我也在复习c++,我可以向你推荐这本书。
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
using namespace std;
using namespace this_thread;
using namespace chrono;
const string term = "Hello World!";
int main() {
string namevar;
cout << "Enter your name: " << flush;
cin >> namevar;
std::cout << "Welcome " << namevar << ". This program was created to teach the user how to code using C++" "n";
sleep_for(2s);
cout << "When learning any coding language the first step is to understand how to print Hello World. So let us begin!" << endl;
}
相关文章:
- boost::进程间消息队列引发错误
- 在线编译器中的分段C++没有打印消息
- C++错误消息*成员参考.**初学者*
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 如何通过参数抛出错误消息
- 从服务器传输到客户端的消息不会出现
- ROS2 动态消息模板
- C++秘密消息学校作业
- 自一周开始以来的秒数?
- glad 导致 glfwSwapBuffers 返回错误消息
- C++入门 5 版:类消息和文件夹
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 如何处理从一个对象传递到另一个在C++中具有公共抽象类的对象的消息
- Gstreamer:每 5 秒使用多文件墨水保存图像/jpeg
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 由于无效的 ValidateRgn() 子窗口不会收到WM_PAINT消息
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- C++每 x 秒调用一次函数
- 如果我没有收到来自客户端的消息,如何每 20 秒打印一次日期时间