Exception handling

Discussion in 'C++' started by senthil.march, May 31, 2012.

  1. senthil.march

    senthil.march New Member

    Joined:
    Jun 1, 2012
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    hi guys,
    i declare a variable as int and if i get d input from user and and if user gives float value r character instead of int program shows undesired output and it goes on executin.. cannot stop d program...... i want to write exception handling for dis.... how can i write??pls help me.....
     
  2. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    With numbers, you could try something like

    Code:
    int x;
    do {
       // prompt for and store input
       if(std::cin.fail()) {
          std::cin.clear();
          while(std::cin.get() != '\n'){}
          std::cout << "that wasn't a number\n";
       }
    } while(....);
    That *should* help with bad input from chars or punct. A floating point would probably survive but be truncated to its integer part.

    If you needed something more, you could probably use the stringstream class along with a string to read in the data. From there, parse the string to make sure it's an integer - if it is, use istringstream member to store into your int variable.
     
  3. senthil.march

    senthil.march New Member

    Joined:
    Jun 1, 2012
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    hey thank u very much for replyin... but i cant understand d code... could u explain me pls....
     
  4. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    The do-while loop sits and waits for a test condition to be true (or false depending on what you're wanting to do). For example, say you want a user to enter an integer that is between 1 and 50.

    Code:
    int num = 0; // initialize to 0; should cin fail on bad input,
                     // it will have a good value for the test condition
    
    do {
       std::cout << "Enter an integer (1-50): ";
       std::cin >> num;
    
       if(std::cin.fail()) { 
    
          // if a user enters bad data, cin will be in a fail state
          // to restore it, clear the eof and fail bits using clear()
    
          std::cin.clear();
    
          // because the stream failed, there is probably unwanted
          // data in the keyboard buffer.  To "clean" it out, loop
          // through using cin.get until a newline char is encountered.  
    
         while(std::cin.get() != '\n'){}  
    
          // if all went okay, the input stream (cin) should be
          // restored, so let the user know the input was bad
    
          std::cout << "invalid input\n";
        }
    } while(num < 1 || num > 50); // only want range of 1-50
    With cpp, I usually don't include the std namespace. If you add using namespace std; to your source, then you wouldn't use std::cin, std::cout, etc. You'd only need cin, cout, etc.

    Hope that helps.
     
  5. senthil.march

    senthil.march New Member

    Joined:
    Jun 1, 2012
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    ya thanx a lot :) :) i understood the program. if u giv an character value cin.fail() become true but if u enter any integer followed by character it cin.fail() doesnt become true.. for example if u giv (7e)as input to num, cin.fail() doesnt become true and in num the value is stored as (7) .and in keyboard buffer the value (e) is stored... i dont want my program to do lik dis... my program shuld say error for dis type of value also...wat can i do for dis??thanks for ur immense help :)



    p.s:my english is very bad....if u cant understand clearly sorry pa....
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice