I'm trying to search with regex to find words like "long tonne" by ignoring any white spaces between "long" and "tonne", which I thought would be best with regex. with the code below, it will print out every converted output but I'm only trying to print one result. ex. if I enter 12 kg, I would only want my result to print the converted to lbs string.
So far, Ive tried :
- removing the semi colon after the regex
- adding { } brackets before if and after regex
-
running the code section by section by copying and pasting in another project (which i feel its the regex that I'm having issues with)
#include <iostream> #include <string> #include <regex> using namespace std; int main() { cout << "Enter your mass with unit: "; double earthMass; string unit; string tolower(unit); cin >> earthMass >> unit; regex kg("kg|kgs"); if (regex_match("kg", kg)) { double dKgToLb; dKgToLb = (earthMass * 2.20462); cout << "your converted mass is : " << dKgToLb << " lb" << endl; } regex pound("lb|lbs"); if (regex_match("lb", pound)) { double dLbToKg; dLbToKg = (earthMass * 0.453592); cout << "your converted mass is : " << dLbToKg << " kg" << endl; } regex longTonne("long\\s*tonne|lg\\s*tn"); if (regex_match("long tonne", longTonne)) { double dLongToShort; dLongToShort = (earthMass * 1.12); cout << "your converted mass is : " << dLongToShort << " sh tn" << endl; } regex shortTonne("short\\s*tonne|sh\\s*tn"); if (regex_match("short tonne", shortTonne)) { double dShortToLong; dShortToLong = (earthMass * 0.892857); cout << "your converted mass is : " << dShortToLong << " lg tn" << endl;} } else if (!cin.good()) { cerr << "your input is invalid\n"; return EXIT_FAILURE; } }
Aucun commentaire:
Enregistrer un commentaire