I am trying to validate the input in a way that the user always has to enter kg or lb, if it doesn't do it will ask the user to reenter. It works until that point but it doesn't read the new input. I know that I need to clear the buffer but iss.clear() is not working.
float readMass()
{
string weight;
getline(cin, weight);
float weightValue;
string massUnit;
istringstream iss(weight);
streampos pos = iss.tellg();
iss >> weightValue >> massUnit;
if (massUnit == "lb")
{
weightValue = weightValue / 2.20462;
}
else if (massUnit == "kg")
{
weightValue = weightValue;
}
else
{
cout << "Please enter a correct weight and mass indicator: \n";
iss.clear();
readMass();
}
return weightValue;
}