c++ error handling

jamesmac14

Junior Member
Jul 6, 2005
9
0
0
So I am taking in an int from the console in stream and when I type in a string instead, my program goes into an infinite loop. How can I handle this error in code?

(code example would look something like this)


{
int myInt = 0;

cout<<"Enter in an int here."<<endl;
cin>>myInt; //if input here is "aStringNotInt" then my program crashes.
}
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
Originally posted by: jamesmac14
So I am taking in an int from the console in stream and when I type in a string instead, my program goes into an infinite loop. How can I handle this error in code?
}

I'd always take a string as input, then you can do commands on the string to convert to int, or to determine if there is alpha present. Otherwise, I don't know of a simple way to restrict alpha in a cin >> but I hardly ever do console programs so I'm not very knowledgeable here.

 

stevf

Senior member
Jan 26, 2005
290
0
0
if you input a non int to that code then that input stream goes into a fail state. If that code is in a loop then it keeps executing because the program then just ignores that stream. You can test the state of the stream. You can use the .clear() function to reset the stream.

There are several ways to help avoid this situation. One, as brandonb mentioned is to get all input as string, test it, then either tell user that input wasnt an int and ask again or convert it to the int and continue. Another is to test the state of the input stream after reading it and see if it is in a failure state and then reset it and get input again.

Stream functions you might want to review as they can be used to help validate input

.clear()
.get()
.ignore()
.peek()
.putback()
.getline()
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |