double radius;
string input = "";
for(;;)
{
cout << "Enter the radius of a circle: ";
getline(cin, input);
if(!(atoi(input.c_str()) == 0 && input != "0"))
break;
cout << "Please enter number values only!\n" << endl;
}
radius = atoi(input.c_str());
The program calculates geometric formulas. This block of code is used to check if they try to enter a character instead of a number. I use it every time when they have to input a number, which makes my code look messy.
I want it so when they input something into 'radius' the function checks radius, loops "Please enter a number value" every time they enter a 'non' number value until the user enters a number. Then it returns the number value.
All I have gotten it to do is put it in an infinite loop repeating "Please enter a number values only!" or it returning a non-number value the user enters producing a garbage value.
ANY HELP would be golden! Thank you!
instead of getline(cin, input) to input a string number and then convert from string to numeral use cin>> radius.If you want you can use assert() from #include<cassert>or a loop to ensure that radius is a double between some values like 1.0 and 100.0