01 // Behandlung von E/A-Fehlern
02 // Datei: strerr01.cpp
04 #include <stdlib.h>
05
#include <iostream.h>
07 int main ()
08 {
09 int x;
11 if (system ("cls"))
// Bildschirm löschen
12 cout << "\x1b[H\x1b[2J" << flush;
13 cout << "Fehlerbehandlung
der I/O-Klassen\n";
15 do
16 {
17 int error;
19 cout <<"\nGeben Sie eine Zahl
ein : ";
20 cin >> x;
21 if (!cin) // entspricht: cin.fail()
22 {
23 cerr<<"\nFehlerzustand:
";// Fehler blockiert
24 error = cin.rdstate(); // Fehlerzustand holen
25
if (error & ios::goodbit)
26 cerr << "GOODBIT";
27 if (error & ios::eofbit)
28
cerr << "EOFBIT";
29 if (error & ios::failbit)
30 cerr << "FAILBIT";
31 if
(error & ios::badbit)
32 cerr << "BADBIT";
33 cerr << " erkannt.\n";
34 cin.clear(0);
// Freigabe
35 cin.sync(); // Synchronisation mit stdin
36 }
37 else
38 break;
// Einlesen erfolgreich
39 }
4