summaryrefslogtreecommitdiffstats
path: root/C++/cinErrorExample/cinErrorExamples.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'C++/cinErrorExample/cinErrorExamples.cpp')
-rw-r--r--C++/cinErrorExample/cinErrorExamples.cpp42
1 files changed, 42 insertions, 0 deletions
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 <iostream>
+
+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;
+}