C++ 图书馆时间.h和C++;
我想显示消息“Hello world!”2分钟。 代码如下:C++ 图书馆时间.h和C++;,c++,C++,我想显示消息“Hello world!”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 >>
#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;
}
#包括
#包括
使用名称空间std;
int main(int argc,char const*argv[]{
智力游戏时间;
时钟时间;
cout>游戏时间;
游戏时间=游戏时间*60;
做{
cout这条线:
while ( ((temps = clock()) / CLOCKS_PER_SEC) == gameTime );
告诉计算机重复,((temps=clock())/CLOCKS\u PER\u SEC)==gameTime
解析为true
((temps=clock())/CLOCKS\u PER\u SEC)==gameTime
解析为true
当((temps=clock())/CLOCKS\u PER\u SEC)
等于gameTime
时
这种情况只发生一次,特别是当clock()
返回的值等于gameTime*CLOCKS\u PER\u second
时
由于您希望代码重复,但保持代码重复的条件仅在一种特定情况下为真,因此只要clock()
在开始时返回正确的值,您的代码可能永远不会循环,或者只循环一段时间
你需要重新思考你要做什么。提示:你希望它重复,而游戏时间
比当前时间大。你还希望游戏时间
在你的代码开始时是当前时间的偏移量。你需要一个。时钟()
不是从0开始。时钟()当你启动你的程序时,/CLOCKS\u peru__second
几乎100%肯定大于120。我可以建议你使用一个std::chrono::staid\u clock
?和我必须使用的其他函数一样。我想在给定的时间内做很多事情。我不能这样做!