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.