#include <iostream>
#include <string>
#include <regex>
using namespace std;
double weightConv(double w , string weightUnit)
{
if (weightUnit == "g" || "G")
cout << w * 0.035274 << "oz";
else if (weightUnit == "oz" || "OZ" || "oZ" || "Oz")
cout << w / 28.3495 << "g";
else if (weightUnit == "kg" || "KG" || "Kg" || "kG")
cout << w * 2.20462 << "lb";
else if (weightUnit == "lb" || "LB" || "Lb" || "lB")
cout << w / 0.453592 << "kg";
else if (weightUnit == "ton" || "tn" || "TON" || "TN")
cout << w * 1.10231 << "sh tn";
else if (weightUnit == "sh tn" || "SH TN")
cout << w / 0.90718 << "tn";
else
cout << "Invalid unit of measurement";
return weightConv(w, weightUnit);
}// end of weightCov function
int main()
{
string user;
double mass;
string unitType;
cout << "Enter a mass and its unit type indicator(g,kg,lb,oz,ton,or sh tn)" << endl;
cin >> mass >> unitType;
// case sensitive characters
//regex reg_icase("g", regex::icase);
//if (regex_match("G", reg_icase))
double answer = weightConv(mass, unitType);
// Output Results
cout << " Mass = " << answer << endl;
if (user == "exit")
{
return 0;
}
}// end of main
По какой-то нечетной причине, когда я вводил число и строку через cin
и пытаюсь распечатать ответ, вызывая функцию weightConv
он дает бесконечный цикл. Может кто-то, пожалуйста, помогите мне исправить мой код?
Это бесконечная рекурсия: weightConv()
вызывает себя.
Это бесконечная рекурсия, так как weightConv()
вызывает себя снова и снова, вызывая Segmentation Fault
. Кроме того, вы никогда не вводите user
строки, поэтому его следует удалить или ввести после функции weightConv()
.