图书馆时间.H与C++
library time.h with the C++
我想显示消息"你好世界!"2分钟。 这是代码:
#include <iostream>
#include <time.h>
using namespace std;
int main(int argc, char const *argv[]) {
int gameTime;
clock_t temps;
cout << "Insert the time (minute) : ";
cin >> gameTime;
gameTime = gameTime * 60;
do {
cout << "Hello world !" << endl;
}while ( ((temps = clock()) / CLOCKS_PER_SEC) == gameTime );
return 0;
}
它不行!请帮忙
这一行:
while ( ((temps = clock()) / CLOCKS_PER_SEC) == gameTime );
告诉计算机在((temps = clock()) / CLOCKS_PER_SEC) == gameTime
解析为true
时重复。
当((temps = clock()) / CLOCKS_PER_SEC)
等于gameTime
时,((temps = clock()) / CLOCKS_PER_SEC) == gameTime
解析为true
。
这只发生一次,特别是当clock()
返回等于gameTime*CLOCKS_PER_SEC
的值时。
由于您希望代码重复,但保持代码重复的条件仅在一种特定情况下为真,因此您的代码可能永远不会循环,或者只要clock()
返回正确的值,如果它纯粹偶然地在开始时返回正确的值,则只会循环。
你需要重新思考你想做什么。提示:您希望在gameTime
大于当前时间时重复此操作。您还希望gameTime
是代码开头当前时间的偏移量。
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- 从持续时间构造std::chrono::system_clock::time_point
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- while循环中while循环的时间复杂度是多少
- 使用简单类型列表实现的指数编译时间.为什么
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 在已经使用Git的情况下减少编译时间
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 从文本文件中读取时钟时间和事件时间并进行处理
- 具有未知值时的时间复杂性
- 如何减少花费的时间
- C++在变量给定的指定时间内关闭电脑
- rcpp函数中的清理时间很长
- C++:floor unix时间戳到UTC月份
- 如何在c++中录制具有精确帧时间戳的视频
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- KMP算法和LPS表构造的运行时间
- 为什么std::互斥需要很长的、非常不规则的时间来共享
- 使用Boost Interprocess创建托管共享内存需要很长时间