函数使用<windows.h>停止时间并继续执行其他函数</windows.h>

时间:2014-06-09 02:30:39

标签: c++ sleep

我的waitSeconds()函数采用整数表示等待的秒数。我正在使用睡眠(毫秒)并转换为秒,此时我想尝试这样做,并知道它不优雅。但是我的程序没有执行其余的函数调用,而且我正在敲打。

最终,我想要使用此函数调用的是使用我的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);
}

3 个答案:

答案 0 :(得分:3)

评论中已经有正确的C ++答案:std::this_thread::sleep_for WinAPI方法(Sleep)也是可能的。

你的&#34;功能的真正原因&#34;似乎失败是因为int triangle(4)main中定义了一个新变量,初始化为4.此变量隐藏了全局triangle函数。

答案 1 :(得分:1)

#include <ctime>
void pause (unsigned int seconds)
{
    time_t goal = seconds + time(0);
    while (goal > time(0));
}

答案 2 :(得分:-1)

#include <iostream>
#include <ctime>

using namespace std;

int dots(int count);
int dashes(int count);
void sendSOS();
void waitSeconds(int seconds2Wait);
int triangle(int rows);
int slowTriangle(int rows);
void distressCall();

int main()
{
    dots(3);
    dashes(3);
    sendSOS();
    cout << "\n\n";
    waitSeconds(1);
    triangle(4);
    slowTriangle(4);
    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
{
    for (; count >= 1; count--)
    {
        cout << "-";
    }
    return 0;
}
void sendSOS()
{
    dots(3);
    dashes(3);
    dots(3);
}
void waitSeconds(int seconds2Wait)                  //Sleeps for time specified
{
    time_t goal = seconds2Wait + time(0);
    while (goal > time(0));
}
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;
    for (i = 1; i <= rows; ++i)
    {
        for (j = 1; j <= i; ++j)
        {
            waitSeconds(3);
            cout << ". ";
        }
        cout << "\n";
    }
    return 0;
}
void distressCall()
{
    sendSOS();
    waitSeconds(2);
}

按照建议实现暂停功能,现在按预期工作。感谢我们写下ctime暂停功能的朋友。我还修复了一些语法错误:) 如果你想让distressCall()永远运行,就这样:

void distressCall()
{
    while(true)
    {
        sendSOS();
        waitSeconds(2);
    }
}

强烈建议您实现转义机制,但是:)。