Exiting a infinity loop using user input?

Jul 29, 2016 at 6:21am
I am new to c++ programming and please forgive me if I made silly mistakes.

In real world scenario we would always have an emergency exit/stop button if some thing goes wrong in a process. I also want to simulate similar emergency exit code in c++ command line.

I have used this stackoverflow.com/a/5845305/996366 code for writing my code.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <unistd.h>

unsigned int microseconds;

int main()
{
    int i=0;
    int input;
    std::cout<<"please enter 911 to exit loop: ";
    while (std::cin.good())
    {
        int input;
        input = std::cin.peek();
        if( input == 911 )
        {
            break;
        }
        else
        {
            std::cout<<i<<"\n";

            i=i+1;
        }
        usleep(microseconds);
    }

    return 0;
}


unless other vise 911 (a validation integer) is inputed by the user the code should print increment value of i in infinite loop. Some where in the code I made a logical error which caused improper working of the code?
Last edited on Jul 29, 2016 at 6:22am
Jul 29, 2016 at 6:42am
You are continually peeking at the same character (which cannot take on the value 911.)
Jul 29, 2016 at 1:30pm
Why don't you just use :
1
2
int input;
std::cin >> input;
Jul 29, 2016 at 2:43pm
Are you trying to achieve non-blocking console I/O (i.e. detecting individual keystrokes)? This is not possible using functions from the standard library.
Jul 29, 2016 at 2:48pm
The problem with above code is I have to manually enter number on every loop. What I want is , I want to exit a loop which is already active when an user input 911.

real world problem
Lets assume I am using loop function for downloading some files from thousands of url and lets say in the middle of the looping I noticed some thing and want to halt the looping in the middle. The easiest way would be to close the program itself but I am looking for a programmatic way to exit the loop by validating from a user input(911/EXITPROG..etc)
Last edited on Jul 29, 2016 at 2:49pm
Jul 29, 2016 at 3:33pm
Using getche() is an advisable solution.

Let me show you :
1
2
3
c3 = c2; c2 = c1;
c1 = getche();
if(c1 == '1' && c2 == '1' && c3 == '9') break;
Jul 29, 2016 at 3:33pm
Does that help? :)
Jul 29, 2016 at 3:38pm
Yes, that would require non-blocking I/O. Like I said, that's not possible with iostreams.

It's possible to detect when the user hits Ctrl+C and perform a clean shutdown (i.e. stop loop, call destructors, etc.) instead of an instant termination. I've done this a few times because properly doing what you're trying to do is overkill in most situations. To do this, you use signal() to override the handler for SIGTERM. In the handler, you flip a bool (note that the handler may be run in a different thread than main()) that you check in the main loop. When the bool changes to the signaled state, you break out and exit.

If you still want to do it with actual keystrokes, you'll need to use one of the curses libraries.
Topic archived. No new replies allowed.