1

I have a question that might be very simple to many of you however, I have not found an answer to my question.

I have the program below that is working properly. This code turns a number into a floating number and integer.

Let's say you entered 5.4, the program will give you 5.4 for double and 5 for integer.

Now I need to add a throw catch statement to my program in case the user enters a text instead of a number ("If the conversion fails, throw an exception and allow the user to re-enter the value.").

This is the pseudocode of what I need to do.

try {
        if(num ==character)
            throw;
        cout << "\n The number entered " << num << "invalid, please enter again";
    }
    catch
    {
    }

and I implemented something like this, however it did not work. I set the 'a' variable character thinking the user has to enter a text in order to get that message. However it did not work and gave some errors.

try
    {
      char a;
      if (num == a) 
        throw num;  
    }
    catch(int e)
    {
      cout << "A number of " << a << " is invalid." << endl;
      cout << "Please re-enter a number: ";
      cin << num  
    }

I am very new with this "try,throw,catch" terms. I would be happy if you help me through this, thanks.

#include <C:\\CSIS1600\MyCppUtils.cpp>
#include <iostream>
#include <string>
using namespace myNameSpace;
int main()
{   
    runner("is running");
    cout << "Enter a number :  ";
    string num;
    getline(cin, num);

    cout<< "double " << getValidDouble(num) << endl;
    cout<< "integer " << getValidInt(num) << endl;

    system("pause");
    return 0;
}

#include<iostream>
#include<string>
using namespace std;

namespace myNameSpace
{
    string num;

    void runner(string str)
    {
        cout <<"runner-3() is running.."<<endl;
    }

    int getValidInt(string n)
    {

        int valueint;
        valueint=atoi(n.c_str());
        return valueint;
    }

    double getValidDouble(string n )
    {

        double valuedouble;
        valuedouble = atof(n.c_str());
        return valuedouble;
    }
} 
UniQuadrion
  • 149
  • 1
  • 2
  • 13
  • What specifically didn't work? Can you elaborate on what went wrong? – templatetypedef Nov 03 '13 at 22:51
  • 1
    please don't throw anything other than `std::exception` or its subclass... – Bryan Chen Nov 03 '13 at 22:55
  • related: [don't use `atoi`](http://stackoverflow.com/q/8871711/509868) – anatolyg Nov 03 '13 at 22:56
  • Firstly, I think you've headed slightly in the wrong direction. `atoi` and `atof` don't tell you whether the string is a valid number or not. You need functions that do tell you that, such as `strtol`, `strtod` or `boost::lexical_cast`. Secondly, you must never use an uninitialized value in C++, so whatever you think `num == a` does, it doesn't do what you think :-) – Steve Jessop Nov 03 '13 at 22:57
  • Finally: `num` is a string. Your code throws `num`. But your catch clause catches `int`. So even if your code were otherwise correct, and was throwing in the right situations, your catch clause wouldn't catch what you're throwing. – Steve Jessop Nov 03 '13 at 22:59
  • I am sorry i little got messed up in my coding, what i tried to implement in my throw-catch and try is that if the user enters a text or a word, i wanted the statement to show error. for instance when you run the program it would say "enter a number: " and if you enter lets say "banana" how would you implement program to say "please enter a number" using throw try-catch. – UniQuadrion Nov 03 '13 at 23:27
  • If it still seems a little vague what i posted, this is the specification for the program. Implement the following functions: int getValidInt() Returns a valid integer entered from the keyboard. double getValidDouble() Returns a valid double entered from the keyboard. Each method should do the following: Prompt the user to enter the appropriate value. Store the charaters entered as a string. Convert the string to the appropriate numeric value. If the conversion fails, throw an exception and allow the user to re-enter the value. I did all except for the try-catch which is the hardest part. – UniQuadrion Nov 03 '13 at 23:34

1 Answers1

2

You can use Boost to do a lexical cast. If you have valid input (e.g. 2.54), no exception will be thrown, but with invalid input (e.g. 2???54) the bad_lexical cast is thrown:

#include <boost/lexical_cast.hpp>

try
{
    double x1 = boost::lexical_cast<double> ("2.54");
    double x2 = boost::lexical_cast<double> ("2???54");
    cout << x1 << x2 << endl;
}
catch(boost::bad_lexical_cast& e)
{
    cout << "Exception caught - " << e.what() << endl;
}   
resigned
  • 1,044
  • 1
  • 10
  • 11
  • This seems very advanced which we are far beyond from covering in our course. Isn't there an easier way to do it using throw try-catch? just need it to give the user an error if the user enters a text instead of a number. – UniQuadrion Nov 03 '13 at 23:54
  • 1
    Not really advanced. Its certainly the way we do it in industry. – resigned Nov 06 '13 at 23:30