我的 waitSeconds() 函数采用整数来表示等待的秒数。我正在使用 Sleep(msec) 并在这一点上转换为秒我想尝试这样做并且知道它并不优雅。但是,我的程序不执行其余的函数调用,我很头疼。
最终,我想使用此函数调用的目的是使用我的 slowTriangle() 函数和 distressCall() 调用它,该函数会永远循环并暂停传递 waitSeconds 的参数。我希望这最后一部分是有道理的。无论如何,感谢您任何有经验的成员可以提供的指导。
#include <iostream>
#include <Windows.h>
using namespace std;
int dots(int count);
int dashes(int count);
void sendSOS();
void waitSeconds(int seconds2Wait);
int triangle(int rows);
int slowTriangle();
void distressCall();
int main()
{
dots(3);
dashes(3);
sendSOS();
cout << "\n\n ";
waitSeconds(1);
int triangle(4);
int slowTriangle();
void distressCall();
return 0;
}
int dots(int count) // counts DOWN the number of dots that the int is set as a parameter
{
for (; count >= 1; count--)
{
cout << "." ;
}
return 0;
}
int dashes(int count) // counts UP the number of dots that the int is set as a parameter
{
int i;
for (; count >= 1; count--)
{
cout << "-";
}
return 0;
}
void sendSOS()
{
dots(3);
dashes(3);
dots(3);
}
void waitSeconds(int seconds2Wait) //Sleeps for time specified
{
Sleep(1000 * seconds2Wait); //converts miliseconds to seconds
seconds2Wait = 2;
}
int triangle(int rows) //Prints a dot triangle
{
int i, j;
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
cout << ". ";
}
cout << "\n";
}
return 0;
}
int slowTriangle(int rows) //Prints a dot triangle with sleep paramter passed in
{
int i, j, seconds2Wait;
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
waitSeconds(3);
{
cout << ". ";
}
cout << "\n";
}
return 0;
}
void distressCall()
{
sendSOS();
waitSeconds(2);
}
最佳答案
正确的 C++ 答案已经在评论中:std::this_thread::sleep_for
也可以使用 WinAPI 方法 (Sleep
)。
你的“函数”似乎失败的真正原因是因为 int triangle(4)
在 main
中定义了一个新变量,初始化为 4。这个变量隐藏了全局 triangle
函数。
关于c++ - 使用 <Windows.h> 暂停时间并继续其他功能的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24112938/