c++ - if 基于用户输入的语句

标签 c++ visual-c++

我正在尝试根据用户输入使用不同的 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/

相关文章:

c++ - 散列pair <pair <int,int>,pair <int,int >>的unordered_map

c++ - 检测特定虚函数的 vtable 索引/序号(使用 Visual C++)

c++ - mingw 编译错误 : invalid conversion from 'FARPROC ' to 'void*' , 但 msvc 编译正常

c++ - 如何将 RSA 公钥和私钥读入单个 RSA 结构?

c++ - C++中的堆排序实现

c++ - Qt Creator 和 MSVC - 在调试时忽略特定的异常类型?

c++ - 暂时禁用第一次机会异常

c - DirectX 应用程序无法绘制

php - 错误 : php. h 没有这样的文件或目录

c++ - 在 Qt 中关闭程序之前显示一个对话框