Handle Ctrl+Z input

Why is it that, for any C++ console application, when a user is prompted for input and enters Ctrl+Z it causes the program to loop infinitely or crash?

i.e.

Please enter a number between 1 and 10:
^Z //user enters Ctrl+Z and hits Enter
Your input is invalid.
Your input is invalid.
Your input is invalid.
Your input is invalid.
Your input is invalid. . . etc.

Any ideas on how to handle this?
What are you trying to do with cntrl+z? Undo? You're going to need to use another library or gui for handling ctrl + z AFAIK.
This sounds like an error in the code that reads from the console.

On Windows console, specifically, entering Ctrl+Z followed by Enter puts std::cin into eof state, which, in typical input loops such as "while(std::cin >> value) { ..." breaks out of the loop. Perhaps you used some other loop structure?
I'm just trying to validate input and keep my program from crashing. The program is asking for a string. The user is giving it Ctrl+Z (^Z), which it REALLY doesn't like.

Take this program for example:

#include <iostream>
#include <string>
using namespace std;

int main()
{
string input;
cout << "Please enter a number between 1 and 10:" << endl;
cin >> input;
cout << "You entered: " << input << endl; //user enters Ctrl+Z and hits enter, results in program termination

cin.ignore();
cin.get();
return 0;
}//end of main method

As per the comments, the program crashes.

In this example, I've built a rational number class. I'm using an overloaded >> operator to call methods to validate and parse the string before handing the data to the appropriate constructor (as per my course assignment's rubric). The function will return a bool of false if it doesn't pass validation.

Rational rnA;
cout << "Please enter a rational number:" << endl;

while(cin >> rnA == false)
{
cout << "That is not considered valid input. Please enter a rational number in the form of X, X/Y, or X Y/Z:" << endl;
} etc.

If the user enters Ctrl+Z when prompted for input, the program will cout the error message infinitely.
// ... results in program termination
As per the comments, the program crashes.

It doesn't crash, it terminates: ctrl+z makes cin >> input set eofbit on std::cin, which you didn't check for or clear, which means cin.get() also returns without waiting and the program reaches return 0;

If the user enters Ctrl+Z when prompted for input, the program will cout the error message infinitely.

That's what it's written to do.

Try:
1
2
3
4
5
6
7
while(cin >> rnA == false)
{
   if(cin.eof()) break; // quit if Ctrl+Z
   cin.clear(); // otherwise clear error states
   cin.ignore( numeric_limits<streamsize>::max(), '\n'); // discard bad input
   cout << "That is not considered valid input. Please enter a rational number in the form of X, X/Y, or X Y/Z:\n";
}
Sorry, I meant terminates as per the comments :).

That code fixes it, though I'm not sure I understand this line:

cin.ignore( numeric_limits<streamsize>::max(), '\n');

numeric_limits<streamsize>::max() - returns the max value for any numeric type and you are clearing that from the stream?
max size of a stream aka the inputstream (cin). Then it ignores so if there is anything left over in the buffer it ignores it. Say for example you are inputting a character and you input asd then s and d are stuck in the buffer but if you ignore the maximum amount of things that could possibly be in the buffer then you are 100% positive to ignore s and d. You could put 2 instead of numeric_limits but what happens when you put in asdf or anything longer than 3.
Great! Thanks to all for the help!
Topic archived. No new replies allowed.