Try catch

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jackly84
    New Member
    • Mar 2010
    • 16

    Try catch

    how do i place a try catch Statement into this ?


    cout <<"Enter Student Number: ";
    cin >> record[n].student_number ;

    Making sure Student Number is only A Number ?

    i cant get this to work
    try
    {
    cout <<"Enter Student Number: ";
    cin >> record[n].student_number ;
    if(!(cin >=a))
    }
    catch
    {
    "Please Only Enter Numbers" << endl;
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you don't need to use try - catch
    check if the read of an integer works, if not clear the error, remove the faulty characters from the input stream and try again, e.g.
    Code:
        int i;
        cout << " enter a number ? ";
        while (!(cin >> i))
           {
           cout << " Error _ enter a number ? ";
           cin.clear();             // clear error condition
           cin.ignore(1000,'\n');   // remove faulty characters
           }

    Comment

    • jackly84
      New Member
      • Mar 2010
      • 16

      #3
      Thanks, the try catch thing was giving me a headache

      Comment

      Working...