【问题标题】:Need help with getline() [duplicate]在 getline() 方面需要帮助 [重复]
【发布时间】:2010-12-17 05:03:19
【问题描述】:

如果在我的程序中我要求用户输入,我这样做是否有原因:

int number;
string str;
int accountNumber;

cout << "Enter number:";
cin >> number;
cout << "Enter name:";
getline(cin, str);
cout << "Enter account number:";
cin >> accountNumber;

为什么在输入第一个数字后,它会输出“输入姓名”,然后在我输入 getline(cin, str) 行的“str”之前立即输出“输入帐号”?谢谢!

【问题讨论】:

    标签: c++ getline


    【解决方案1】:

    getline(cin, str); 读取先前读取的数字之后的换行符,并立即返回此“行”。为避免这种情况,您可以在阅读名称之前使用 std::ws 跳过空格:

    cout << "Enter number:";
    cin >> number;
    cout << "Enter name:";
    ws(cin);
    getline(cin, str);
    ...
    

    请注意,这也会跳过换行符后的前导空格,因此str 不会以空格开头,即使用户确实输入了空格。但在这种情况下,这可能是一个特性,而不是一个错误......

    【讨论】:

    • 请注意,此方法不允许用户输入空行。
    【解决方案2】:

    试试

    cout << "Enter name:";
    cin.ignore();
    getline(cin, str);
    

    【讨论】:

      【解决方案3】:

      看起来你想要基于行的阅读。为此,您可能希望始终使用getline,然后在需要解析读取行中的数字时解析每一行。它使输入读数更加一致。

      这样您就不必手动扫描每一行的结尾来保证下一个读取操作从新的一行开始。

      它还简化了为重复输入请求添加错误处理。

      例如

      #include <string>
      #include <iostream>
      #include <istream>
      #include <ostream>
      #include <sstream>
      
      int parse_integer(const std::string& input)
      {
          std::istringstream iss(input);
          int result;
          if (!(iss >> result))
          {
              // error - throw something?
          }
          return result;
      }
      
      int main()
      {
          int number;
          std::string str;
          int accountNumber;
      
          std::string inputline;
      
          std::cout << "Enter number: ";
      
          if (!std::getline(std::cin, inputline))
          {
              // error - throw something?
          }
      
          number = parse_integer(inputline);
      
          std::cout << "Enter name:";
      
          if (!std::getline(std::cin, inputline))
          {
              // error - throw something?
          }
      
          str = inputline;
      
          std::cout << "Enter account number:";
      
          if (!std::getline(std::cin, inputline))
          {
              // error - throw something?
          }
      
          accountNumber = parse_integer(inputline);
      
          return 0;
      }
      

      【讨论】:

        【解决方案4】:
        cin >> number
        

        只从缓冲区中抓取数字,它将“输入”留在缓冲区中,然后立即被 getline 抓取并解释为空字符串(或仅包含新行的字符串,我忘记了)。

        【讨论】:

        • 是的,他打错了,仅此而已。
        • @Secko 是的,这正是我想要交流的。如果我显得粗鲁,我深表歉意。
        • 哎呀。好久没用stream了,忘了那些算子的方向了。
        【解决方案5】:
        cin >> number // eat the numeric characters
        getline(cin, str) // eat the remaining newline
        

        【讨论】:

          【解决方案6】:

          我认为问题在于cin &gt;&gt; 传递了换行符(\n)。 getline() 假定换行符是空格并将其传递。有人发布了您可以使用的解决方案。

          您可以使用虚拟的getline(cin, dummy); 或真实的cin.ignore(100,'\n');

          【讨论】:

            【解决方案7】:

            不要使用getline():这对内存分配不利。使用fgets()。见fgets reference

            【讨论】:

              猜你喜欢
              • 2021-09-10
              • 1970-01-01
              • 1970-01-01
              • 2014-08-31
              • 1970-01-01
              • 2014-08-02
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多