From 9d53d8857eaa1c9405894a88ca75bc4657e42f35 Mon Sep 17 00:00:00 2001 From: msglm Date: Sat, 14 Jan 2023 05:31:48 -0600 Subject: Inital Commit --- C++/cinErrorExample/cinErrorExamples.cpp | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 C++/cinErrorExample/cinErrorExamples.cpp (limited to 'C++/cinErrorExample/cinErrorExamples.cpp') diff --git a/C++/cinErrorExample/cinErrorExamples.cpp b/C++/cinErrorExample/cinErrorExamples.cpp new file mode 100644 index 0000000..741e987 --- /dev/null +++ b/C++/cinErrorExample/cinErrorExamples.cpp @@ -0,0 +1,42 @@ +// Program: cin error examples +// Ch 3: Input/Output + +// The purpose of this program is to look at some possible errors +// that might occur if a user enters incorrect input + +#include + +using namespace std; + +int main() +{ + int int1, int2; + double dbl; + char ch; + + cout << "TESTING INPUT ERRORS" << endl; + cout << "Tested by: Anonymous" << endl; + cout << "This program is designed to read 2 integer values, followed \n"; + cout << "by a double value, followed by a character value.\n\n"; + + /* INPUT FAILURE & FAIL STATE + An attempt to read invalid data, such as trying to put a letter into an int + or double, results in input failure. The input stream enters a state called + the "fail state." Once it enters fail state, all further I/O statements using + that stream are ignored. However, the program continues to execute with whatever + values are stored in variables and produces incorrect results. + */ + + // Run this program several times and purposely provide wrong data types + // to see what kind of output is produced. + cout << "Input, separated by spaces: "; + cin >> int1 >> int2 >> dbl >> ch; + + // Output + cout << "int: " << int1 << endl; + cout << "int: " << int2 << endl; + cout << "Double: " << dbl << endl; + cout << "char: " << ch << endl; + + return 0; +} -- cgit v1.2.3