I have made a function for controlling user input so that the user can input anything and including a long string of letters and the function gives out "Incorrect input" and repeats until a number is input. (These are then used for a switch statement or initialising values.)
This works fine for everything, except when I enter "0"- here it gives out incorrect input rather than 0, as though 0 is not a number. Do strings treat zero as different from a normal number? And does anyone know how to fix this problem? Thank you.
float user_input(string input_name){
string line;
float variable;
bool x = true;
while (x == true)
{
cout<<"\nPlease enter the "<<input_name<<": ";
getline(cin, line);
istringstream Str_variable(line);
Str_variable >> variable;
if (variable){
//cout<<"\nIn function"<<input_name<<"= "<<variable<<endl;
x = false;
}
else{
cout<<"Incorrect input. Please try again"<<endl;
}
}
return(variable);
}