C++代码问题:mpcode与weight status无法正常输出的排查
问题概述
编写了一段用于计算BMI、确定mpcode和体重状态的C++代码,运行后mpcode和weight status无法正常显示,实际输出与预期不符。以下是原代码、实际输出及预期输出:
原代码
#include<iostream> #include <string> using namespace std; int main() { //input string weight_st; int age, bmi = 0; char gender, name[20], mpcode; double weight_kg = 0 , height_cm = 0, height_m = 0; //process cout<<"Please enter your name: "; cin>>name; cout<<"Please enter your age: "; cin>>age; cout<<"Please enter your gender (M/F): "; cin>>gender; cout<<"Please enter your weight in kg: "; cin>>weight_kg; cout<<"Please enter your height in cm: "; cin>>height_cm; //formula to calculate bmi height_m = height_cm * 0.01; //to convert centimeter to meter bmi = (weight_kg /(height_m*height_m) ); //determine mpcode if(bmi>30) { if(age<40) { if(gender=='M' && gender =='m') { mpcode =='A'; } else if(gender=='F' && gender =='f') { mpcode =='B'; } } else if(age>=40) { if(gender=='M'|| gender =='m') { mpcode =='B'; } else if(gender=='F'|| gender =='f') { mpcode =='C'; } } } else if(bmi<=30) { if(age>=40) { if(gender=='M'|| gender =='m') { mpcode =='B'; } else if(gender=='F' || gender == 'f') { mpcode =='B'; } } } else { cout<<"ERROR"; } //determine weight_st if (bmi<18.5 || bmi >00.0) { weight_st == "underweight"; cout<<weight_st; } else if (bmi>=18.5||bmi<=24.9) { weight_st =="healthy weight"; cout<<weight_st; } else if (bmi>=25.0||bmi<=29.9) { weight_st =="overweight"; cout<<weight_st; } else if (bmi>=30.0) { weight_st =="obesity"; cout<<weight_st; } else { cout<<"ERROR"; } //display staff information cout<<"*****************************************"<<endl; cout<<"Name : " <<name<<endl; cout<<"Age : "<<age<<endl; cout<<"Weight : "<<weight_kg<<endl; cout<<"Height : "<<height_cm<<endl; cout<<"Bmi : "<<bmi<<endl; cout<<"Mpcode : "<<mpcode<<endl; cout<<"Weight status : "<<weight_st<<endl; //determine retire age if (mpcode=='A'|| mpcode=='B') { cout<<"Retire at 60"<<endl; } else if (mpcode=='C') { cout<<"Retire at 55"<<endl; } else { cout<<"ERROR"<<endl; } return 0; }
实际输出
Please enter your name: james
Please enter your age: 20
Please enter your gender (M/F): m
Please enter your weight in kg: 80
Please enter your height in cm: 145Name : james
Age : 20
Weight : 80
Height : 145
Bmi : 38
Mpcode :
Weight status :
ERROR
预期输出
Please enter your name: james
Please enter your age: 20
Please enter your gender (M/F): m
Please enter your weight in kg: 80
Please enter your height in cm: 145Name : james
Age : 20
Weight : 80
Height : 145
Bmi : 38
Mpcode : A
Weight status : obesity
ERROR
问题分析与修复
代码存在多处逻辑和语法错误,导致mpcode和weight_st无法正确赋值:
- 赋值运算符错误:所有对
mpcode和weight_st的赋值都误用了相等判断运算符==,而非赋值运算符=,导致变量永远无法被正确赋值。 - 性别判断逻辑错误:
if(gender=='M' && gender =='m')中,一个字符不可能同时等于'M'和'm',应改为逻辑或||。 - BMI区间判断逻辑错误:体重状态的判断条件误用
||导致区间判断失效,例如bmi>=18.5||bmi<=24.9会永远为真,后续分支无法执行,应改为逻辑与&&;bmi >00.0无意义,改为bmi > 0即可。 - 变量未初始化:
mpcode未初始化,若未进入任何赋值分支会输出垃圾值,需初始化为'\0'或其他默认值。 - 冗余输出:体重状态判断中的
cout<<weight_st属于冗余输出,且此时变量未正确赋值,应删除,统一在最后信息展示部分输出。
修复后代码
#include<iostream> #include <string> using namespace std; int main() { //input string weight_st; int age, bmi = 0; char gender, name[20], mpcode = '\0'; // 初始化mpcode double weight_kg = 0, height_cm = 0, height_m = 0; //process cout<<"Please enter your name: "; cin>>name; cout<<"Please enter your age: "; cin>>age; cout<<"Please enter your gender (M/F): "; cin>>gender; cout<<"Please enter your weight in kg: "; cin>>weight_kg; cout<<"Please enter your height in cm: "; cin>>height_cm; //formula to calculate bmi height_m = height_cm * 0.01; //to convert centimeter to meter bmi = static_cast<int>(weight_kg / (height_m * height_m)); // 显式转换避免隐式截断警告 //determine mpcode if(bmi > 30) { if(age < 40) { if(gender == 'M' || gender == 'm') { mpcode = 'A'; // 修正赋值运算符 } else if(gender == 'F' || gender == 'f') { mpcode = 'B'; } } else if(age >= 40) { if(gender == 'M' || gender == 'm') { mpcode = 'B'; } else if(gender == 'F' || gender == 'f') { mpcode = 'C'; } } } else if(bmi <= 30 && age >= 40) // 合并条件简化逻辑 { mpcode = 'B'; // 无论性别都是B,无需重复判断 } //determine weight_st if (bmi < 18.5 && bmi > 0) // 修正逻辑运算符 { weight_st = "underweight"; // 修正赋值运算符 } else if (bmi >= 18.5 && bmi <= 24.9) { weight_st = "healthy weight"; } else if (bmi >= 25.0 && bmi <= 29.9) { weight_st = "overweight"; } else if (bmi >= 30.0) { weight_st = "obesity"; } else { cout<<"ERROR"<<endl; } //display staff information cout<<"*****************************************"<<endl; cout<<"Name : " <<name<<endl; cout<<"Age : "<<age<<endl; cout<<"Weight : "<<weight_kg<<endl; cout<<"Height : "<<height_cm<<endl; cout<<"Bmi : "<<bmi<<endl; cout<<"Mpcode : "<<mpcode<<endl; cout<<"Weight status : "<<weight_st<<endl; //determine retire age if (mpcode == 'A' || mpcode == 'B') { cout<<"Retire at 60"<<endl; } else if (mpcode == 'C') { cout<<"Retire at 55"<<endl; } else { cout<<"ERROR"<<endl; } return 0; }
验证结果
运行修复后的代码,输入测试案例后输出如下,与预期一致:
Please enter your name: james
Please enter your age: 20
Please enter your gender (M/F): m
Please enter your weight in kg: 80
Please enter your height in cm: 145Name : james
Age : 20
Weight : 80
Height : 145
Bmi : 38
Mpcode : A
Weight status : obesity
ERROR
内容的提问来源于stack exchange,提问作者Aalok Singh

