1

Please excuse any amateur mistakes, I'm still quite new to this. For my class we need to convert a double input to a string to use in a different part of the project. The verification for the integers worked just fine and I attempted using some of the code from this previous question Validating a double in c++ though, much to my chagrin it did not work.

Here is my code:

string input;
bool valid;
double num;

//Verification of valid inputs
do
{
    valid = true;
    cout << "What is the " << name << " rate of the population? (% per year): ";
    getline(cin, input);
    num = input.length();

    if (num == 0)
    {
        cout << "\nNo data was entered, please enter a number.\n";
        valid = false;
    }
    else
    {
        for (double i = 0; i < num; i++)
        {
            if (input.at(i) < '0' || input.at(i) > '9')
            {
                cout << "\nPlease enter a valid, positive number.\n";
                valid = false;
                break;
            }
        }
    }
} while (valid == false);
return stod(input);

Thanks.

Edit: I just found this How do i verify a string is valid double (even if it has a point in it)? and I can safely say I have no idea what is going on.

Community
  • 1
  • 1
Bowski
  • 11
  • 3
  • 1
    The answer in the last reference, (http://stackoverflow.com/a/29169409/464581), just using `std::stod`, is good. Simple. You can *use* that function even if you don't understand it. ;-) – Cheers and hth. - Alf Sep 28 '15 at 06:25
  • 2
    How about using [`std::stod`](http://en.cppreference.com/w/cpp/string/basic_string/stof) or [`std::strtod`](http://en.cppreference.com/w/cpp/string/byte/strtof)? [Example here](http://stackoverflow.com/a/32792825/440558) (with integers, but still the same principle). – Some programmer dude Sep 28 '15 at 06:26
  • I tried using the example from http://stackoverflow.com/questions/32792790/read-whether-int-or-string-using-the-file-extraction-operator/32792825#32792825 and it broke my program =( – Bowski Sep 28 '15 at 06:38

3 Answers3

0

If you are really keen to do it manually, take a look at the following methods:

How to manually parse a floating point number from a string

https://www.daniweb.com/programming/software-development/code/217315/c-function-stod-string-to-double-convert-a-string-to-a-double

and read the comments. It's not quite straightforward, but it should work. Or even better, check the code of stod from C++.

Community
  • 1
  • 1
asalic
  • 664
  • 3
  • 6
0

Why are you using a double as an index for the string? Also, I'd skip using a ++ for increment a double.

rockworm
  • 11
  • 2
  • It's how the instructor was showing us how to do it and how he wants us to do it. Most of what I've found around the web shows completely different ways of doing it but I'm stuck doing it this way =/ – Bowski Sep 28 '15 at 22:39
  • You can do it the same but switch to an `int` instead. – rockworm Sep 29 '15 at 06:38
  • Right now you are validating that you get only numbers in your input string, how are you gonna validate the decimal point? – rockworm Sep 29 '15 at 07:32
0

Figured it out.

Just had to add something to an if statement.

 if ((input.at(i) < '0' || input.at(i) > '9') && input.at(i) != '.')
            {
                cout << "\nError! illegal character was entered.\n";
                valid = false;
                break; // 12w345

                if (input.at(i) == '.')
                    ct++;

                if (ct > 1)
                {
                    cout << "Error! Only one dot allowed.";
                    valid = false;

With ct being an integer with value 0 to count the dots and ensure only one was entered.

Bowski
  • 11
  • 3