From a18a8ba38f99f05848fa152c08e5e0fc145b6c95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20S=C3=A1nchez?= Date: Tue, 16 Oct 2018 05:23:21 +0100 Subject: [PATCH] Added error handling for console input (C++) (#19249) * Update index.md * Update index.md Added additional sources --- .../cplusplus/input-and-output/index.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/client/src/guide/english/cplusplus/input-and-output/index.md b/client/src/guide/english/cplusplus/input-and-output/index.md index 20087e491a..9ebcb3d854 100644 --- a/client/src/guide/english/cplusplus/input-and-output/index.md +++ b/client/src/guide/english/cplusplus/input-and-output/index.md @@ -122,8 +122,46 @@ When chained, the extraction operator will first read data from `cin` to fill `a C's standard printf and scanf statements can also be used with c++ by importing '' header file. + +#### Error handling with `cin` +Sometimes the process of reading an input fails, commonly because we try to assign a content of certain type to a variable that cannot store it. If not handled properly, an error in input could cause `cin` to stop blocking the program flux, just accepting what remains on its buffer that failed to be read. If for example `cin` is in an infinite loop, it could begin repeating that sequence indefinitely without waiting for the user input, leaving the only choice of force quitting the program. + +When `cin` fails to read it raises a `failbit`3, so we need to handle that, `clear`4 the stream state from errors and `ignore`5 the remaining input, since a failure in reading will not clear the input buffer. Let's see an example: + +```C++ +int number; +cin >> number; + +if(cin.fail()) +{ + cin.clear(); // resetting failbit + cin.ignore(std::numeric_limits::max(), '\n'); // skipping input until line return + /* + ... + Do other needed handling, for + example asking for new input. + ... + */ +} +``` + +It is also possible to check the condition with `if (!cin)` instead of `if (cin.fail())`. Furthermore, we can check for errors at the same time we try to assign the input to the variable: + +```C++ +int number; + +if(!(cin >> number)) +{ + cin.clear(); + cin.ignore(std::numeric_limits::max(), '\n'); + // ... +} +``` ## Sources 1. http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/ 2. http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/ +3. https://en.cppreference.com/w/cpp/io/basic_ios/fail +4. https://en.cppreference.com/w/cpp/io/basic_ios/clear +5. https://en.cppreference.com/w/cpp/io/basic_istream/ignore