我是初學者,所以對我來說很簡單。我的問題在我的輸出中。如果你輸入一個全名,它會崩潰。我應該使用字符串以外的東西嗎?它正確地顯示輸出最初,但然後它也跑下來,並添加你是超重行,和一個額外的計算。如果計算加起來就是超重的用戶,它會很好地工作。另外,設置精度未被應用。我很難過。C++ BMI計算器問題
int main()
{
double height, weight, bmi; // height in inches, weight in pounds, bmi is total bmi of user
string name; // name of the user
int num; // the number 703 used for bmi calculation
num = 703; // constant used for bmi calculation
cout << "Please enter your full Name:"; // Asking the user to input their name
cin >> name; // Users name
cout << "Please enter your height(in inches):"; // User height in inches
cin >> height; // Users height
cout << "Please enter your weight(in lbs):"; //Users weight in lbs
cin >> weight; // Users weight
bmi = (weight/pow(height, 2)) * num; // the actual calculation of the bmi of the user
if (bmi >= 18.5 && bmi < 25) {
cout << name << " your BMI is " << setprecision(1) << bmi; // outputting to the user their actaul BMI
cout << endl;
cout << "You are in the optimal weight category!"; // outputting their category
}
else if (bmi < 18.5) {
cout << name << " your BMI is " << setprecision(1) << bmi;
cout << endl;
cout << "You are underweight.";
}
else (bmi >= 25); {
cout << name << " your BMI is " << setprecision(1) << bmi;
cout << endl;
cout << "You are overweight.";
}
return 0;
}
也看看使用cin和[我們爲什麼會叫CIN清晰讀取輸入後CIN忽略](http://stackoverflow.com/問題/ 5131647 /爲什麼 - 會 - 我們叩CIN-明確和CIN-忽略,後讀輸入) – 2014-09-27 00:10:36