exception handling problem here..

hi how do i write an exception handling to handle a code where int = t2 and if i enter any input other than int for my t2, exception will come out.

heres my code and currently im am able to handle my value but not identifier (char, int)

1
2
3
4
5
6
7
    do
    {
     cout << "Cycle Time(in seconds, 15 - 20):";
     cin >> t2;
     if(t2 < 15 || t2 > 20)
     cout << "Invalid Input,(15 - 20 only), enter again\n";
    }while(t2 > 20 || t2 < 15);
i'm not quite sure why you want use an exception, but you should try to avoid them in regular cases.

regards
If you're asking how to recover from a user entering non-digits when you're trying to read in an int:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
do
{
     cout << "Cycle Time (in seconds, 15-20): " ;

     cin >> t2 ;

     if ( !cin ) // got some bad input!
     {
          cin.clear() ;   // reset state of cin.
          while (cin.get() != '\n')  // clear the input in the stream.
               ;
     }

} while (t2 > 20 || t2 < 15 ) ;


Exceptions wouldn't be appropriate in this case.

Last edited on
thanks cire.. u helped me alots..
my lecturer tested my program and i had this vulnerability she said she will deduct marks.. somehow i need to fix it and thanks im done with my project =D
Topic archived. No new replies allowed.