Can anyone please give me some hint on how can I terminate an input session in a console app without terminating console/closing app? Consider this scenario at console, If the user is asked to input multiple line, the only way out would be to finish with all inputs or Ctrl + C to terminate the console. But I want to let user Press e.g. F9 to end this operation without making input, and then going back to previous page. Is that possible?
One way would be to have say a '.' as a break char and if this is entered then break from a loop etc etc. For non-os specific, you'll need to enter '.' and then a \n.
There are os specific ways of obtaining a char without also having to enter \n.
This will get one char from the keyboard without waiting for a \n to be entered. Say the ESC key is the key to press to end the operation (easier to use then a function key). Instead of using getline() to obtain a line of input, you can use something like:
This will display 'Terminated' if either ESC or F9 keys are pressed, otherwise displays what was entered.
Note that keys such as F9 produce 2 chars - first one is 0 and a second depending upon the key. F9 is C, f8 is B etc. If you want to use function keys like this, then you'll need to find out what chars are produced for what key combinations. Some special keys first produce 0xE0 (224) first.
// <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
You could also make use of line 3 by its-self as a way to pause the program, but that would require a clear input buffer to start with.
Thanks Andy, though my situation is more complicated (or its simple and I haven't worked it out yet)!
@seeplus
Although your solution for exiting an input prompt works, but it either can not be applied in my case or I haven't been able to implement it properly. In sample code below, I have setFname(), setIdnum() and many other set functions which include while loop for checking the right entry (for example certain length and characters for ID or Phone format or...). And then as you see, I use them inside other loops for entry, verify and database insertion.
How can I break out of such condition? Because once user is prompted for lets say Idnum, it is directed to accept only certain characters and length, if not an error, and then keep asking for Idnum. Same goes to all other set functions.
What you want is non-trivial to implement - it's almost like going back to the days of ms-dos/mini/mainframe programming of the 1980's (oh happy days)!
Because once user is prompted for lets say Idnum, it is directed to accept only certain characters and length, if not an error, and then keep asking for Idnum. Same goes to all other set functions.
What you need to do is to change .setIdnum() to use getInp() (or similar) from above to obtain keyboard input. .setIdum() also needs to return bool. Within .setIdum() after you use getInp() and the return value is true then .setIdnum() returns true. Otherwise parse the input from getInp() and return false when OK.
Where .setIdnum() is used, you need to test the return value and if true then break out of the loop (or other processing as required).
The same goes for all the other functions that obtain input!
@seeplus
You are right! it's meaningless. well that's what happens when one keep expanding on a small console app for school project. I should skip on adding to this console app and better to switch to an actual or Non-Trivial app ;)
I should skip on adding to this console app and better to switch to an actual or Non-Trivial app
Probably, yes. Now-a-days, no one writes console programs (unless as a specific console command). Everything is gui. The only reason that the console features so heavily when learning C++ is that standard C++ only uses the console for interactive input/output - it doesn't have in-built gui capabilities. This capability has to be provided by 3rd party libraries. Other languages eg. java (with swing), c#, vb.net et al do provide gui capabilities so for these languages gui is taught from the beginning. This can't currently be done with C++ - hence the heavy use of the console. There are various gui libraries available for C++ . Some (such as QT) are x-platform. Some are specific MS etc.
If you want to try some gui programming, then the book Programming: Principles and Practice Using C++ by Bjarne Stroustrup (inventor of C++) uses FLTK and provides guidance and examples of how to install and use.