PDA

View Full Version : C/C++ wait for user keystroke



rba1988
April 4th, 2009, 12:27 PM
Hello.

I'm developing a C++ console application in Ubuntu 8.10 that runs a loop to display certain information. However, the only way to break away from the loop is to press ctrl+c. The program terminates entirely.

How would I implement the loop in such a way that if the user presses the key "q", the program will first display some statistical summary and then quit. Or if the user presses the key "p" the loop will break and the user prompts if they would want to continue. If so, the loop starts again.

Any ideas on how to do this under Ubuntu? Thanks in advance.

dwhitney67
April 4th, 2009, 12:31 PM
Is this a trick question?

Pretty much one of the first thing covered in any C or C++ book/tutorial is input/output from the terminal. To collect input in C++, you would use std::cin.



char choice;

std::cout << "Enter a letter, 'p' or 'q': ";
std::cin >> choice;
std::cin.ignore(100, '\n');

...

_Purple_
April 4th, 2009, 12:33 PM
In case of C, getchar() can be used.

rba1988
April 4th, 2009, 01:38 PM
Is this a trick question?

Pretty much one of the first thing covered in any C or C++ book/tutorial is input/output from the terminal. To collect input in C++, you would use std::cin.



char choice;

std::cout << "Enter a letter, 'p' or 'q': ";
std::cin >> choice;
std::cin.ignore(100, '\n');

...


yes but that would make the whole program stop right? If I put that code within the loop, it would wait until a user puts an input. I want it to wait for the user to hit "q" or "p" while the loop is still running. It has to display information in real time and can be stopped anytime during the process.

rba1988
April 4th, 2009, 01:40 PM
Is this a trick question?

Pretty much one of the first thing covered in any C or C++ book/tutorial is input/output from the terminal. To collect input in C++, you would use std::cin.



char choice;

std::cout << "Enter a letter, 'p' or 'q': ";
std::cin >> choice;
std::cin.ignore(100, '\n');

...


Maybe I should use an example to make my question clearer. When you run the command "top", it displays certain info of processes and changes over time. But when you hit "q", the application quits. I need to know how to code that so when you hit "q", it will show some information before exiting.

dwhitney67
April 4th, 2009, 02:38 PM
Maybe I should use an example to make my question clearer. When you run the command "top", it displays certain info of processes and changes over time. But when you hit "q", the application quits. I need to know how to code that so when you hit "q", it will show some information before exiting.

What you should have done in the OP is provide ALL of your requirements.

Basically what you want is non-blocking I/O. You should consider using ncurses for the application you describe. You can print text anywhere on the terminal, and accept non-blocking input, just like the 'top' application.

slavik
April 4th, 2009, 02:42 PM
you can also intercept ctrl+c. :) (look into signals)

Arndt
April 4th, 2009, 03:10 PM
What you should have done in the OP is provide ALL of your requirements.

Basically what you want is non-blocking I/O. You should consider using ncurses for the application you describe. You can print text anywhere on the terminal, and accept non-blocking input, just like the 'top' application.

Using separate threads comes to mind, too.

rba1988
April 5th, 2009, 02:55 PM
What you should have done in the OP is provide ALL of your requirements.

Basically what you want is non-blocking I/O. You should consider using ncurses for the application you describe. You can print text anywhere on the terminal, and accept non-blocking input, just like the 'top' application.

Ok I will look into that. Thanks for your help.:D

rba1988
April 5th, 2009, 03:02 PM
Using separate threads comes to mind, too.

Multithreading? Ok. I'll try to look into that too. Thanks.:D