0

How do i terminate this loop as soon as Enter(Carriage return) in pressed? I had tried getch()!='\r' (as loop condition) but it require to press key to start another iteration and beat the purpose of stopwatch.

//To Create a stopwatch
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
int main ()
{
    int milisecond=0;
    int second=0;
    int minutes=0;
    int hour=0;
    cout<<"Press any key to start Timer...."<<endl;
    char start=_getch();
    for(;;)       //This loop needed to terminate as soon as enter is pressed
    {
        cout<<hour<<":"<<minutes<<":"<<second<<":"<<milisecond<<"\r";
        milisecond++;
        Sleep(100);

        if(milisecond==10)
        {
            second++;
            milisecond=0;
        }
        if(second==60)
        {
            minutes++;
            second=0;
        }
        if(minutes==60)
        {
            hour++;
            minutes=0;
        }
    }
return(0);
}

provide the terminating condition for loop?

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 1
    is there a problem with using ctrl-z or ctrl-c? – Adalcar Nov 17 '16 at 21:49
  • ctrl-z just terminated the whole program but i need to work after work termination of loop. To store the time of stop – Aakash kumar Goryan Nov 17 '16 at 21:57
  • Can't be done without platform-specific calls. More here: [Capture characters from standard input without waiting for enter to be pressed](http://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed?) – user4581301 Nov 17 '16 at 22:06
  • I correct myself. If you are willing to have a lag of up to one `Sleep(100)`, you can use a [`std::thread`](http://en.cppreference.com/w/cpp/thread/thread) to set an a [`std::atomic`](http://en.cppreference.com/w/cpp/atomic/atomic) boolean you test in a `while` in place of the `for` loop. – user4581301 Nov 17 '16 at 22:11

3 Answers3

1

As @BnBDim said kbhit() will work for that. In case you are working with linux you can copy paste kbhit.h y kbhit.cpp from http://linux-sxs.org/programming/kbhit.html and add it to your project.

blackout
  • 84
  • 9
0

Calling getch blocks your program waiting for input, instead you could use kbhit() to check if a button has been pressed and then call getch().

while(true)
{
    if(kbhit())
    {
        char c = getch();
    }
}

In order to use both functions you must include <conio.h> which isn't part of the C++ standard.

BnBDim
  • 136
  • 5
0

A simple cross-platform version:

#include <iostream>
#include <future>
#include <atomic>
#include <thread>

int main()
{
    // a thread safe boolean flag
    std::atomic<bool> flag(true); 

    // immediately run asynchronous function
    // Uses a lambda expression to provide a function-in-a-function
    // lambda captures reference to flag so we know everyone is using the same flag
    auto test = std::async(std::launch::async, 
                           [&flag]()
        {
            // calls to cin for data won't return until enter is pressed
            std::cin.peek(); // peek leaves data in the stream if you want it.
            flag = false; // stop running loop
        });

    // here be the main loop
    while (flag)
    {
        // sleep this thread for 100 ms
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
        // do your thing here
    }
}

Documentation:

  1. std::atomic
  2. std::async
  3. Lambda Expressions
  4. std::this_thread::sleep_for
user4581301
  • 33,082
  • 7
  • 33
  • 54