【问题标题】:Assure correct string input in C++确保在 C++ 中正确输入字符串
【发布时间】:2017-01-05 18:37:35
【问题描述】:

我试图确保我的输入字符串格式正确(格式正确 XXX-X-XXX ),但我不想使用 char temp[255] 和 cin.getline 组合。

到目前为止,我已经设法“捕获”了除此之外的所有异常:

输入车牌:shjkf 22h 23jfh3kfh jkfhsdj h2j h2k 123-A-456

假设 regPlate 将从输入中获取所有字符串,包括最后格式化的正确字符串,并将打印字符串。这是不正确的。读取第一个字符串后,它应该打印 Bad input 以及需要删除之后的所有内容。

我尝试过使用cin.clear()cin.ignore()等。在我的 if 函数中,但没有结果。

void main()
{
    std::string regPlate;
    do
    {
        cout << "Enter registration plate: ";
        cin >> regPlate;
        if (regPlate.size() < 9 || regPlate.at(3) != '-' || regPlate.at(5) != '-' || regPlate.size() > 9)
        {
            cout << "Bad entry" << endl;

        }
    } while (regPlate.size() < 9 || regPlate.at(3) != '-' || regPlate.at(5) != '-' || regPlate.size() > 9);
    cout << endl << regPlate << endl;

    system("pause");
}

【问题讨论】:

  • 您不需要检查大小是否小于9大于9。只需检查一次大小不等于9
  • 至于你的问题,你是不是在找std::getline
  • @Someprogrammerdude "但我不想使用 char temp[255] 和 cin.getline 组合。"
  • @BrunoFerreira std::getlinestd::istream::getline 是两个不同的东西。
  • @Someprogrammerdude 我想你可以假设 OP 根本不想要任何 getlines。

标签: c++ string format stdstring istream


【解决方案1】:

while 循环是否有效,可能类似于:

int main(){
    string regPlate;
    while(regPlate.size() != 9 || regPlate.at(3) != '-' || regPlate.at(5) != '-'){
        cout << "Enter registration plate: " << endl;
        cin >> regPlate;
    }
    cout << regPlate;
    return 0;
}

【讨论】:

    【解决方案2】:

    使用您提供的示例(有一些假设),我运行了您的代码,它似乎按预期工作。这是我运行的内容,包括标题。

    #include <string>
    #include <iostream>
    
    using namespace std;
    
    void main()
    {
       string regPlate;
       do
       {
           cout << "Enter registration plate: ";
           cin >> regPlate;
           if (regPlate.size() != 9 || regPlate.at(3) != '-' || regPlate.at(5) != '-')
           {
              cout << "Bad entry" << endl;
    
           }
       } while (regPlate.size() != 9 || regPlate.at(3) != '-' || regPlate.at(5) != '-');
       cout << endl << regPlate << endl;
    
       system("pause");
    }
    

    我收到的输出是:

    Enter registration plate: shjkf 22h 23jfh3kfh jkfhsdj h2j h2k 123-A-456
    Bad Entry
    Enter registration plate: Bad Entry
    Enter registration plate: Bad Entry
    Enter registration plate: Bad Entry
    Enter registration plate: Bad Entry
    Enter registration plate: Bad Entry
    Enter registration plate: Bad Entry
    Enter registration plate: 
    123-A-456
    

    我还手动输入了您列出的所有值,而且它似乎也可以这样工作。

    【讨论】:

    • 该输入无效。我已经解决了这个问题,但还是谢谢你。
    【解决方案3】:

    感谢@Someprogrammerdude,我设法解决了这个问题。

    我唯一需要做的就是使用std::cin&gt;&gt;regPlate 而不是std::getline(cin,regPlate)

    std::string regPlate;
        do
        {
            cout << "Enter registration plate: ";
            std::getline(cin, regPlate);
            if (regPlate.size() != 9 || regPlate.at(3) != '-' || regPlate.at(5) != '-')
            {
                cout << "Bad entry" << endl;
    
            }
        } while (regPlate.size() != 9 || regPlate.at(3) != '-' || regPlate.at(5) != '-');
        cout << regPlate << endl;
    

    【讨论】:

      猜你喜欢
      • 2014-03-08
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多