1

こんにちは、while ループで 1 回出力を取得しようとしています

While(1){
    if( current->tm_hour == 10 && current->tm_min == 0  ){
        Start_Function();
        std::cout <<  "Started" << std::endl;
    }

    if( current->tm_hour == 12 && current->tm_min == 0  ){
        End_Function();
        std::cout <<  "Ended" << std::endl;
    }

    Sleep(5000);
}

そして、スリープを使用して5秒ごとにリフレッシュします

だから私は現在の時間と分= 10&00のときに欲しい

出力が開始され、関数が1回だけ呼び出され、更新が続行されます

4

2 に答える 2

2

どうですか:

bool start_called = false, end_called = false;
While(1){
    if( current->tm_hour == 10 && current->tm_min == 0 && !start_called  ){
        Start_Function();
        std::cout <<  "Started" << std::endl;
        start_called = true;
    } else
        start_called = false;

    if( current->tm_hour == 12 && current->tm_min == 0 && !end_called ){
        End_Function();
        std::cout <<  "Ended" << std::endl;
        end_called = true;
    } else
        end_called = false;

    Sleep(5000);
}

ファンクターを使用するともっとうまくいく可能性がありますが、それはもう少し進んでいます。

于 2012-11-22T02:55:56.560 に答える
-1

編集:@JoachimPileborgのコメントに照らして

問題となるのは出力ではなく、関数が呼び出されるべきではないときに関数が複数回呼び出される(そして出力が出力される)ということです。– Joachim Pileborg

代替ソリューション

int hasStarted = 0, hasEnded = 0;
While(1){
if( current->tm_hour == 10 && current->tm_min == 0 && !hasStarted  ){
Start_Function();
    std::cout <<  "Started" << std::endl;
    hasStarted = 1;
}

if( current->tm_hour == 12 && current->tm_min == 0  && !hasEnded ){
End_Function();
    std::cout <<  "Ended" << std::endl;
    hasEnded = 1;
}

Sleep(5000);
}
}

上記のコードは、各操作を1回だけ実行し、更新を続行するように強制します...

私の元のコメント:

ご存知のように、コマンドライン/ターミナルでは、出力が継続的に出力されます。使用しているオペレーティングシステム(window / linux / mac)に応じて、解決策は簡単な場合とそうでない場合があります。

gotoxy()関数を調べることをお勧めします

http://www.programmingsimplified.com/c/conio.h/gotoxy

Windowsの場合は「conio.h」ライブラリ、Linuxの場合は「ncurses.h」によって提供されます。

「ncurses.h」にはありませんがgotoxy()、同じことを行う方法を提供します。

于 2012-11-22T02:47:22.463 に答える