【问题标题】:How to if user input matches a specific value?如果用户输入与特定值匹配怎么办?
【发布时间】:2014-02-26 18:38:08
【问题描述】:

我正在编写一个简单的 c++ 测量程序,它要求用户输入以选择他们想从哪个单位测量单位和单位?但我不知道结构是否正确(我的 C++ 真的很烂)。这是我的代码:

#include <iostream>
using namespace std;

int main()
{
    int storeFROM, storeTO;
    char mm, cm, m, kk;

    cout << "Enter the initial unit (mm, cm, m, or km): ";
    cin >> storeFROM;

    cout << endl;

    if ((storeFROM != 'mm') || storeFROM != 'cm' || storeFROM != 'm' || storeFROM != 'km') {
        cout << "--> Sorry unit to convert FROM is invalid" << endl;
    }
    else if ((storeFROM == 'mm') || storeFROM == 'cm' || storeFROM == 'm' || storeFROM == 'km') 
    {
        cout << "Enter the initial unit(mm, cm, m, or kk) :";
        cin >> storeTO;
    }

    // Calculate the selected units
    system("pause");
}

我希望有人可以帮助我询问用户输入以询问他们想要哪个单元,这正是它应该显示的方式:

【问题讨论】:

  • 问题中缺少最后一句的某些部分。
  • 是的,我没有添加度量计算,因为我可能知道该怎么做,它只是设置了如何提问的结构,是我真正不擅长的部分:(
  • 首先将用户输入存储在正确的数据类型中。现在您正尝试将chars 和stringss 存储在ints 中,然后再将它们与chars 和strings 进行比较。你也不能使用==比较字符串

标签: c++


【解决方案1】:

这是正确的代码:

bool isValidUnit(const std::string &unit);
int convertValue(double value, const std::string &unitFrom, const std::string &unitTo);

int main() {

    ...

    std::string storeFROM, storeTO;

    cout << "Enter the initial unit (mm, cm, m, or km): ";
    cin >> storeFROM;

    if (!isValidUnit(storeFROM)) {
        cout << "--> Sorry unit to convert FROM is invalid" << endl;
        ...
    }

    cout << "Enter the initial unit (mm, cm, m, or km): ";
    cin >> storeTO;

    if (!isValidUnit(storeTO)) {
        cout << "--> Sorry unit to convert TO is invalid" << endl;
        ...
    }

    double value;

    cout << "Enter the value in (" << storeFROM << "): ";
    cin >> value;

    double valueConverted = convertValue(value, storeFROM, storeTO);

    cout << "Value in (" << storeTO << "): " << valueConverted << endl;

    return 0;
}

bool isValidUnit(const std::string &unit) {
    return unit == "mm" || unit == "cm" || unit == "m" || unit == "km";
}

double unitMultiplier(const std::string &unit) {
    if (unit == "mm") return 0.001;
    if (unit == "cm") return 0.01;
    if (unit == "m") return 1;
    if (unit == "km") return 1000;
    return 0.;
}

double convertValue(double value, const std::string &unitFrom, const std::string &unitTo) {
    if (unitFrom == unitTo) return value;
    if (value <= 0.) return 0.; // or value :)

    // Get it in meters
    int valueInMetes = value * unitMultiplier(unitFrom);
    return valuesInMeter / unitMultiplier(unitTo);
}

【讨论】:

  • if 语句中的 !isValidUnit 让我出错,我是否在 if 语句中使用 isValidUnit 之前将此 bool 放在开头?
  • @PatrickGamboa,您应该在引用它之前放置这个函数(整个函数或其声明)。放在main函数之前
  • 我让它完全正常工作,只是编辑了一些代码,但过程成功了!好的,很抱歉打扰你,但是使用我的代码,我如何使用用户输入的单位并进行特定的单位转换?希望你能帮忙:)
  • 我已经更新了答案。不确定它是否工作正常,因为没有检查它。
  • @PatrickGamboa, 5.612e+007 = 56120000 这是正确的结果。
【解决方案2】:

在你的程序中,storeFROMstoreTO 都是strings,所以你应该将它们声明为strings:

string storeFROM, storeTO;

当将它们与 "cm""mm" 等值进行比较时,您应该在 "" 中引用它们,而不是在 '' 中引用它们,因为您绝对不希望它们是 multicharacter literals。它应该是这样的:

if ((storeFROM != "mm") || storeFROM != "cm" 
                        || storeFROM != "m" || storeFROM != "km") 
{
    cout << "--> Sorry unit to convert FROM is invalid" << endl;
}
else if ((storeFROM == "mm") || storeFROM == "cm" 
                             || storeFROM == "m" || storeFROM == "km") 
{
    cout << "Enter the initial unit(mm, cm, m, or kk) :";
    cin >> storeTO;
}

【讨论】:

    【解决方案3】:

    对于 int 类型的对象,您不得输入不可接受的符号,例如字母“m”。

    您可以将此代码用作程序的模板。

    #include <iostream>
    #include <string>
    #include <initializer_list>
    #include <algorithm>
    #include <cstdlib>
    
    std::ostream & display_meazures( const std::initializer_list<const char *> &l,
                                     std::ostream &os = std::cout )
    {
        auto it = l.begin();
        for ( size_t i = 0; i < l.size(); i++ )
        { 
            if ( i == l.size() - 1 ) os << "or ";
            os << *it;
            if ( i != l.size() - 1 ) os << ", ";
        }
    
        return os;
    }
    
    int main()
    {
       std::string storeFROM, storeTO;
    
       std::initializer_list<const char *> meazure = { "mm", "cm", "m", "km" };
    
       while ( true )
       {
          std::cout << "Enter the initial unit (" << display_meazures( meazure ) 
                    << "): ";
          std::cin >> storeFROM;
    
          if ( std::find( meazure.begin(), meazure.end(), storeFROM ) != 
               meazure.end() ) 
          {
            break;
          }
    
          std::cout << "--> Sorry unit to convert FROM is invalid" << std::endl;
          std::cout << std::endl;
       }
    
    // Calculate the selected units
    
       std::system( "pause" );
    }
    

    【讨论】:

    【解决方案4】:
    string storeFROM, storeTO; // note string thingie
    float value1, value2;
    
    cin>>storeFROM;
    
    if ((storeFROM != "mm") || storeFROM != "cm" || storeFROM != "m" || storeFROM != "km") )
    {
        cout << "--> Sorry unit to convert FROM is invalid" << endl;
    }
    else  
    {
        cin>>value1;
        cout << "Enter the initial unit(mm, cm, m, or kk) :";
        cin >> storeTO;
        if((storeTO == "mm") || storeTO == "cm" || storeTO == "m" || storeTO == "km"))
        {
           cin>>value2;
              //convert here...
              //hint: use switch-case
        }
    }
    //end programme
    

    这里...

    【讨论】:

    • 字符串和单引号的用法呢?
    • 'mm' 将是您的问题。
    猜你喜欢
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 2018-02-21
    • 2014-09-28
    • 1970-01-01
    相关资源
    最近更新 更多