【问题标题】:C++ Input String with Spaces带空格的 C++ 输入字符串
【发布时间】:2016-12-05 22:46:27
【问题描述】:

我正在编写一个接受用户输入的程序。我需要输入在单词之间包含空格。我很难找到解决方案来做到这一点。 在你问之前,我已经用相同的问题尝试了有关 stackoverflow 的多个其他问题。这些是我尝试过的一些。 How to cin Space in c++?

std::cin input with spaces?

Demonstration of noskipws in C++

Effect of noskipws on cin>>

我的代码的问题在于,只要我的 setBusinessName 方法被调用,它就会自行完成。它输出然后返回自身,而无需等待我输入我的数据。需要帮助...

string setBusinessName()
{
    string name = "";
    cout << "The name you desire for your business:";
    getline(cin, name, '\n');
    cout << name;
    return name;
}

【问题讨论】:

  • 听起来你可以混合使用&gt;&gt;getline&gt;&gt; 在流中留下未解析的空白,通常会留下一个行尾标记让 getline 绊倒。不过,需要查看minimal reproducible example 以确保这是您遇到的问题。
  • 试试这个编辑:link

标签: c++


【解决方案1】:

我还不能评论,没有足够的积分,但你有没有尝试在getline(cin, name, '\n'); 之前添加cin.ignore();

像这样:

string setBusinessName()
{
    string name = "";
    cout << "The name you desire for your business:";
    cin.ignore();
    getline(cin, name, '\n');
    cout << name;
    return name;
}

【讨论】:

  • 虽然我很确定这可以解决 OP 的问题,但在一般情况下这是一个坏主意。如果缓冲区中没有悬挂空白,则您只是吃了有效的输入。最好将ignore 放在&gt;&gt; 之后,您知道应该有EOL 并将ignore() 升级到cin.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n');,以确保除了EOL 之外没有空间或其他东西.
【解决方案2】:

当你这样做时,只需为 cmets 添加更多解释:

cout << "Enter value:";
cin >> x;

当用户按下 Enter 时执行 cin 指令,因此输入缓冲区具有用户插入的值和一个额外的 '\n' 字符。如果您继续执行cin 没关系,但如果您想使用getline(例如在您的情况下在字符串中包含空格),您必须知道getline 将在'\n' 第一次出现时停止在缓冲区中,所以来自getline 的结果将为空。

为了避免这种情况,如果你真的必须同时使用cin和getline,你需要使用cin.ignore(streamsize n = 1, int delim = EOF)从缓冲区中删除'\n',这个函数从缓冲区中清除streamsize字符或 直到第一个字符匹配delim(包括),这里是一个例子:

cin << x;
cin.ignore(256, '\n');
getline(cin, name, '\n');

注意建议使用:

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

如果您不想猜测缓冲区中有多少个字符。

【讨论】:

    【解决方案3】:

    流中可能已经存在某些内容,而getline() 只是读取了它。

    确保您在此函数之前没有使用cin&gt;&gt;。 您可以在getline() 之前使用cin.ignore() 来避免流中已经存在的内容。

    【讨论】:

      【解决方案4】:
      #include <iostream>
      #include <string>
      using namespace std;
      
      int main() {
          string name1, name2, name3, name4, name5;
          int a,b; //or float ...
      
          cout << "Input name 1: ";
          getline(cin, name1); //input: abc def
          cout << "=> Name 1: "<< name1 << endl;      //output: abc def
          cout << "Input name 2: ";
          getline(cin, name2); //input: abc def
          cout << "=> Name 2: "<< name2 << endl;      //output: abc def
      
          cout<<"a: ";
          cin>>a;
          cout<<"a: "<<a<<endl;
          cout << "Input name 3: ";
          getline(cin, name3); //can not input
          cout << "=> Name 3: "<< name3 << endl; //output:
      
          cout<<"b: ";
          cin>>b;
          cout<<"b: "<<b<<endl;
          cout << "Input name 4: ";
          cin.ignore();
          getline(cin, name4); //input: abc def
          cout << "=> Name 4: "<< name4 << endl; //output: abc def
      
          
          cout << "Input name 5: ";
          cin.ignore();
          getline(cin, name5); //input: abc def
          cout << "=> Name 5: "<< name5 << endl; //output: bc def  !!!!!!!!!!
          
          //=> cin>>number; cin.ignore(); getline(cin, str); => OK
          //else: !!!!!!!!
          return 0;
      }
      

      【讨论】:

        【解决方案5】:

        它工作正常。我刚试过这个。

        #include <iostream>
        #include <string>
        using namespace std;
        
        string setBusinessName(){
            string name = "";
            cout << "The name you desire for your business:";
            getline(cin, name);
            cout << name;
            return name;
        }
        
        int main() {
            setBusinessName();
            system("PAUSE");
        }
        

        【讨论】:

        • 系统(“PAUSE”)是做什么的?
        • 它暂停输出控制台。每当您的程序返回结果时,它都会帮助输出控制台暂停,直到您关闭窗口。
        【解决方案6】:
        #include<bits/stdc++.h>
        using namespace std;
        
        string setBusinessName(){
         string name;
         cout << "The name you desire for your business: ";
         getline(cin, name);
         cout << name;
         return name;
         }
        
        int main() {
          setBusinessName();
          return 0;
        }
        

        【讨论】:

        • 虽然这段代码可能会解决问题,但一个好的答案应该解释代码的什么以及如何它有帮助
        猜你喜欢
        • 2017-09-06
        • 2018-10-15
        • 2016-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多