简体   繁体   English

带空格的 C++ 输入字符串

[英]C++ Input String with Spaces

I am writing a program that takes input from the user.我正在编写一个接受用户输入的程序。 I need the input to include spaces between the words.我需要输入在单词之间包含空格。 I am having trouble finding a solution to do that.我很难找到解决方案来做到这一点。

Before you ask, I have tried multiple other questions on StackOverflow with the same question.在你问之前,我已经在 StackOverflow 上尝试了多个其他问题,同样的问题。 These are some of the ones I have tried:这些是我尝试过的一些:

How to cin Space in c++? 如何在 C++ 中输入空间?

std::cin input with spaces? std::cin 输入带空格?

Demonstration of noskipws in C++ 用 C++ 演示 noskipws

Effect of noskipws on cin>> noskipws对cin的影响>>

The problem with my code is that as soon as my setBusinessName() method is called, it just completes itself.我的代码的问题是,只要我的setBusinessName()方法被调用,它就会自行完成。 It outputs and then returns itself without waiting for me to input my data.它输出然后返回自身,而无需等待我输入我的数据。

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

I can't comment yet, don't have enough points, but did you try adding cin.ignore();我还不能评论,没有足够的积分,但你有没有尝试添加cin.ignore(); before the getline(cin, name, '\n');getline(cin, name, '\n'); ? ?

Like this:像这样:

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

Just adding some more explanation to the comments, when you do:当您这样做时,只需在评论中添加更多解释:

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

The cin instruction is executed when the user presses Enter , so the input buffer has the value the user inserted and an extra '\n' char. cin 指令在用户按下Enter时执行,因此输入缓冲区具有用户插入的值和一个额外'\n'字符。 If you continue doing cin that is ok, but if you want to use getline (like in your case to include spaces in a string) you must be aware that getline will stop at the first occurence of '\n' in the buffer, so the result from getline will be empty.如果你继续做cin问题,但是如果你想使用getline (比如在你的情况下在字符串中包含空格),你必须知道getline将在缓冲区中第一次出现'\n'时停止,所以getline的结果将为空。

To avoid this, and if you really must use both cin and getline, you need to remove that '\n' from the buffer by using cin.ignore(streamsize n = 1, int delim = EOF) , this function clears streamsize chars from the buffer or until the first char that matches delim (including), here's an example:为避免这种情况,并且如果您确实必须同时使用 cin 和 getline,则需要使用streamsize (streamsize n = 1, int delim = EOF)从缓冲区中删除该'\n' ,此函数会从缓冲区直到匹配delim (包括)的第一个字符,这是一个示例:

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

Note it is advisable to use:请注意,建议使用:

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

if you don't want to guess how many chars are in the buffer.如果您不想猜测缓冲区中有多少个字符。

#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;
}

It's possible that there is already something in the stream and getline() just reads it.流中可能已经存在某些内容,而getline()只是读取它。

Make sure you didn't use cin>> before this function.确保在此函数之前没有使用cin>> And you can use cin.ignore() before getline() to avoid something already existed in the stream.你可以在getline() cin.ignore() ) 来避免流中已经存在的东西。

It is working fine.它工作正常。 I just tried this.我刚试过这个。

#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");
}
#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM