I cannot figure out why the program continues to prompt me for a vaild number even when the correct number( a gpa) is entered. I'm not sure if the problem lies in the isDouble function or the getDouble?
1 2 3 4 5 6 7 8 9 10 11 12 13
* Determines whether the string holds a valid double. Checks if each
* character is digit and there is no more than 1 decimal point.
*/
bool isDouble (const string &str) {
int decimal = 0;
for (unsignedint i = 0; i < str.size(); ++i){
if ( str[i] == '.'){
decimal++;}
if( !isdigit(str[i]) && (str[i] != '.')) returnfalse;
}
if( (decimal != 1) || (!isInteger(str))) returnfalse;
returntrue;
}
1 2 3 4 5 6 7 8 9 10 11
double getDouble (const string &msg) {
cout << msg;
string num;
cin >> num;
while( !isDouble(num)){
cout << "Enter a valid number ";
cin>> num;
}
double s = atof(num.c_str());
return s; // stubbed method. Replace this return statement.
}