图书馆时间.H与C++

library time.h with the C++

本文关键字:C++ 时间 图书馆      更新时间:2023-10-16

我想显示消息"你好世界!"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是代码开头当前时间的偏移量。