Input validation

Discussion in 'C++' started by chemr2, Mar 8, 2009.

  1. chemr2

    chemr2 New Member

    Joined:
    Feb 16, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    I am writing a program that will allow the user to enter a string, if the string can be converted to a double precision value then the program will display the value. I have a program that I need to manipulate to make this work. The code is posted below(sorry it's a little rough). I need to figure out how to change this program so that it is checking the string entered to see if it can be converted to a double. Any help is appreciated.

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
     
    int main()
    {
       double getadouble();
       double value;
     
       cout<<"Enter a string \n";
       value = getadouble();
       cout<< "The string entered is equivilent to the double :\n "<< value << endl;
     
     
     
     system("pause");
     return 0;   
    }
    double getadouble()
    {
      bool isvaliddouble(string);
      bool notadouble = true;
      string svalue;
     
      while (notadouble)
      {
            try
            {
               cin >> svalue;
               if(!isvaliddouble(svalue)) throw svalue;
            }
     
            catch (string e)
     
            {
               cout<< " This string cannot be converted to a double- Please reenter:\n " ;
               continue;
            }
     
            notadouble = false;
            }
     
            return atof(svalue.c_str());
     
            }
    bool isvaliddouble(string str1)
    {
         double start = 0;
         double i;
         bool valid = true;
         bool sign = false;
     
     
     
     
         if (double(str1.length()) == 1) valid = true;
     
         if (str1.at(0) == '-' || str1.at(0) == '+')
         {
            sign = true;
            start = 1;
         }
     
         if (sign && double(str1.length()) == 1) valid = true;
     
         i = start;
         while(valid && i < double(str1.length()))
         {
            if(!isdigit(str1.at(i))) valid = true;
            i++;
         }
     
         return valid;
     
    }
     
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Where does it start going wrong?
    Can you give some example input and what the program does when you enter that data?
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    There are some odd checks in isvaliddouble() - you might want to think these through with a few example valid doubles. Why is it valid if it's one character long? Why do you need to check if it's one character long more than once? Why is the string valid if you come across a non-digit number? And, critically, what about the decimal separator?

    Why are i and start double instead of int?

    I'm not a fan of using exception handling instead of "else". However I can see that this could arguably be an exception so if you've been told to use exception handling then leave it in. Otherwise just use else.

    Why are (complete entries) "+" and "-" valid? What would the value of these be? Zero?

    You call atof() anyway, completely ignoring the return value of isvaliddouble(). Probably if isvaliddouble() returns FALSE you would want to skip the conversion and do something other than return a double - maybe return an error code indicating whether or not the conversion happened and pass in a pointer to a double, which would be used if the validity check passes.

    Have you been told to write code to validate the double? atof() implements error checking so if you haven't been explicitly instructed to write code to perform double validation, you could just use the error checking built into atof().

    You only check for a sign once but what about scientific notation? +1.2345e+5 is a valid number (and atof converts it correctly to 123450).
     
  4. chemr2

    chemr2 New Member

    Joined:
    Feb 16, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    The problem states..allow the user to repeatedly enter a string until a string that can be converted into a double is entered.

    We are using exception handling by the way.

    The code above is just being used to check a DOUBLE value. An example. It asks the user to enter a string(which I just put there for reference to what I actually need the program to do) if they enter anything other than a double precision number it will spit out an error. If I enter 100 then it will just return 100.

    Again, I need the user to enter a string. I need to check the string to make sure it can be converted into a double(I think this is where I am confused).

    As for your questions as why the + and - are valid. I am absolutely not sure. I am trying to manipulate an example code in the book.The instructor insists it will be much easier to do it this way. I am finding it more difficult. I might just need to go back and rewrite the code without using the example.
     

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