Unit Converter Validation help

Hey, im new to c++. I had to make a unit converter to class. Im trying to add validation as a little extra challenge, but im having an issue. If i type in a correct unit validated with regex, my code will go on and run like normal. However, if it doesnt pass, it will ask for input again, however it does not complete the calculations properly. My feeling is that it has something to do with my stringstream, input getting caught up? i worked with .clear and .ignore and had no luck.

int main(){
// Output a title, ask the user to enter the mass that will be calculated
cout << "Mass converter, (c)2017: " << endl;
cout << "Enter the mass: ";
getline(cin, input);

istringstream ss(input);
ss >> mass;
getline(ss >> skipws >> ws, initialUnit);

regex reg_icase(initialUnit, regex::icase);
regex reg_unit("g|oz|kg|lb|sh tn|long tn|tonne|gr|dwt", regex::icase);

while (!regex_match(initialUnit, reg_unit))
{
cout << mass << " " << initialUnit << " is an unknown unit and cannot be converted." << endl;
cout << "Enter a valid mass: (ex. lb, g, dwt, etc.)";
getline(cin, input);

istringstream ss(input);
ss >> mass;
getline(ss >> skipws >> ws, initialUnit);
}

if (regex_match("g", reg_icase))
{
resultMass = mass * toOunce;
unit = "g";
resultUnit = "oz";
}
else if (regex_match("oz", reg_icase))
{
resultMass = mass * toGram;
unit = "oz";
resultUnit = "g";
}
else if (regex_match("kg", reg_icase))
{
resultMass = mass * toLb;
unit = "kg";
resultUnit = "Lb";
}
else if (regex_match("lb", reg_icase))
{
resultMass = mass * toKg;
unit = "lb";
resultUnit = "Kg";
}
else if (regex_match("sh tn", reg_icase))
{
resultMass = mass * toLong;
unit = "sh tn";
resultUnit = "tonne";
}
else if (regex_match("long tn", reg_icase))
{
resultMass = mass * longTnToSh;
unit = "long tn";
resultUnit = "sh tn";
}
else if (regex_match("tonne", reg_icase))
{
resultMass = mass * tonneToSh;
unit = "tonne";
resultUnit = "sh tn";
}
else if (regex_match("gr", reg_icase))
{
resultMass = mass * toDwt;
unit = "gr";
resultUnit = "dwt";
}
else if (regex_match("dwt", reg_icase))
{
resultMass = mass * toGr;
unit = "dwt";
resultUnit = "gr";
}


cout << mass << " " << unit << " is " << resultMass << " " << resultUnit << "\n" << endl; // Print out the final conversion
}
My variables are declared at the top, forgot to copy them in!
Topic archived. No new replies allowed.