Thread: input from the user/exception handling

  1. #1
    Unregistered
    Guest

    input from the user/exception handling

    i am writing a small c++ program that accepts input from the user. the input MUST be an integer.

    i wanna do something like this (this is written in "Java-style"):
    try
    {
    int inputNumber;
    cin >> inputNumber;
    }
    catch(NumberFormatException e)
    {
    cout << "you'll have to input an integer....." << endl;
    }

    how do i do this in C++?
    Can anyone help me

    thanks.

    Allan


    btw, i'm using borland builder

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    exceptions are overkill for this....
    instead look into cin.good() cin.fail() and cin.clear().
    cin.bad() can be useful too.

    another strategy is to get all input as a string and then parse that string.Slower but efficiency probably not so important here.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    As Stoned Coder has pointed out if all you want to do is this, then exceptions would be pointless. If you're want to use exceptions for different types of input throughout your program and it's larger than a few functions/classes, then you could create an exception class -

    Code:
    #include <iostream>
    
    using namespace std;
    
    class InputError
    {
    	static char* errors[];
    public:
    	static char* reterror(int i){
    		//error checking required
    		return errors[i];
    	}
    };
    
    //add more errors
    char* InputError::errors [] = {"you'll have to input an integer....."};
    
    int main()
    {
    	try
    	{
    		int i;
    		cin >>i;
    		if(cin.fail())throw InputError::reterror(0);
    	}	
    	catch(char* err)
    	{
    		cout << err << endl;
    	}
    }
    zen

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    besides more tragic problem with doing that way is you cannot recover back to where you asked for the input whereas with either of my strategies you can.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Unregistered
    Guest
    first of all, thanks for mention the cin.bad() function,
    but when i use (in a way like this):

    if(cin.good()==true)
    {
    bla bla bla bla bla bla bla bla
    }
    else
    {
    cout << "wrong input";
    }

    the problem is now, when i go back to this function (calls it again) the previous entry seems to be in memory, so whatever i do, i get the error message.

    this happens even if i use fflush(stdin) or cin.clear().

    i can call all the other functions without a problem.

    do u know what to do?

    thanxs in advance

    allan

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    void clearstream(istream& is)
    {
    is.clear();
    while(is.get() != '\n'&& !is.eof())continue;
    is.clear();
    }

    something like that should do it.....

    alternatively there is always cin.ignore()
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Unregistered
    Guest
    thanx for the help!

    now it works

    allan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  2. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  3. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  4. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM
  5. Help with input handling.
    By Will in forum C++ Programming
    Replies: 5
    Last Post: 05-09-2002, 01:00 PM