我正在尝试根据用户输入使用不同的 if 语句。但是它似乎只使用最终集。任何帮助都会非常感谢。
char type[20];
double weight;
double feed;
cout<< "Enter horse type: ";
cin>>type;
cout << "Enter the horse weight in whole pounds: ";
cin>>weight;
cout<<"Horse type: "<<type<<endl;
cout<<"Horse weight: "<<weight<<endl;
这是我的 if 语句。
{
if (type=="Light");
if (weight >= 840 && weight <=1200)
feed = (3.0);
else if (weight< 840)
feed = (3.3);
else if (weight > 1200)
feed = (2.5);
}
{
if (type=="Large");
if (weight >= 1100 && weight <=1300)
feed=(3.0);
else if (weight < 1100)
feed=(3.3);
else if (weight > 1300)
feed= (2.5);
}
{
if (type=="Draft");
if (weight >= 1500&& weight <=2200)
feed = (3.0);
else if (weight< 1500)
feed = (3.3);
else if (weight >2200)
feed= (2.5);
}
cout<<"Feed Amount "<<feed<<" pounds"<<endl;
再次感谢您的帮助
最佳答案
您不能使用 ==
比较 C 风格的字符串(字符数组)。比较数组的地址,而不是它们的内容。
改用std::string
。将第一行替换为
std::string type;
您还需要修复 if
语句:
if (type == "Whatever") // no ;
{
// do stuff
}
关于c++ - if 基于用户输入的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26227108/