January 13th, 2013, 05:53 PM
Reseting the program by pressing ESC
I am quite newbie when it comes to programming in C language, so be sensible :)
I have some program which I have to test, but I have to go throug many I/O samples, so it's quite hard to run program, type data and run it again after shutting it all by myself.
Is there any possibility to loop the program that is written in CLI so that after pressing for instance ESC button the screen will clear and program will run again ?
I tried to use some loop like:
but it doesn't react to ESC button ;/
// THE ACTUAL CODE
Hopefully the solution would be easy to implement, but due to lack of experience I can't get this sorted on my own.
January 13th, 2013, 06:36 PM
For historical reasons, C was designed to work with terminals, many of which were line-oriented (meaning they wouldn't send any input to the main-frame/mini-computer until an enter line had been entered as indicated by the user pressing the Enter key), so the standard input functions are all line-oriented and don't start to process the input until the Enter has been pressed. Therefore, there is no standard C function that detects a single key having been pressed; expecting one would be like expecting a standard C function to detect a mouse click written decades before the concept of a mouse even existed.
However, there are ways to detect a single key presses, just no standard C way. These ways are specific to your operating system, so just exactly how you do it depends on what operating system you're using. Additionally, some compilers/development environments offer functions with which to perform this task so they will often vary from one compiler to the next.
So then, what operating system is this for and what compiler or development environment are you using? From your reference to CLI, I assume that it's .NET on Windows, but do please verify that. Only after we have that information can anyone even begin to hope to help you.
January 13th, 2013, 06:46 PM
Thanks for answer !
Program will run on Windows and the sourcecode will be build using DeVC++ I guess.
I did try to use some do while loop like so:
It supposed to exit this loop after pressing ESC button, but the trouble is I don't want only to shut down process of program, but also to RESTART IT and for this reason I think it is way too complicated for me to deal with it on my own ;/
// content of int main() function
} while (ch != 27)
January 14th, 2013, 03:05 PM
Windows application (AKA "GUI") or console application (AKA "DOS window" or "text")?
For a console application, the proper way would be with a "character-mode application". This link, http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx, will get you into Microsoft Developers' Network's (MSDN) library section on consoles and the functions it provides. If you Google around you should be able to find some tutorials. Using this approach will be portable across all Windows-based C compilers.
The quick-and-dirty way would be to use the old conio library which is supported by MinGW gcc, which is the compiler packaged with Dev-C++ (which is an old IDE that hasn't been supported for years). The conio.c source file is provided and if you read it you will see that it implements the conio functions using the character-mode functions that I mentioned above. You will also see that it's not a full implementation of the conio library, but the function you're looking for is there. It's been so long that I forget whether the conio.o object file already exists or if you'd have to compile conio.c to generate, but when you build an app with conio you will need to add conio.o to the link list along with whatever libraries you want to link in.
Of course, if you're doing Windows apps, then you'll need to write a handler for the WM_CHAR message.
January 15th, 2013, 06:31 AM
OK, another question.
What if I would like to shut down the program by simply pressing some key - for instance ESC.
I mean, after program will display output, I could press ESC to shut it down ?
Is this possible under DeVC++ with standard console application ?
Console application under Windows without any sort of GUI, the standard one.
January 15th, 2013, 10:21 AM
Yes, as I had just described.
The quick way would be to use conio, but that is not portable and many compilers do not support it.
Of particular interest would be getch(), which reads in a single keyboard key without waiting for Enter to be pressed. A variant is getche(), which also echos that key to the screen. However, if you've not pressed a key yet, then both of those will wait until you do, so if you have other things to be doing in the meantime that would be a problem -- eg, if you're in a loop and want to be able to escape out of it at any time. Then you could use kbhit() to test whether a key has been "hit" (ie, pressed) and then only call getch when it's guaranteed to read something.
You can Google those function names for more information and for examples of their use.
Again, in the long run it's best to learn the character-mode functions, but conio can serve as an alternative for now.
Last edited by dwise1_aol; January 15th, 2013 at 02:15 PM.