C++ std::cin 跳过空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27205251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:45:44  来源:igfitidea点击:

std::cin skips white spaces

c++cinspaces

提问by Pejman Poh

So I am trying to write a function to check whether a word is in a sentence, by looping through a char array and checking for the same string of char's. The program works as long as the Sentence doesn't have any spaces. I googled around and they are all the same suggestions;

所以我试图编写一个函数来检查一个单词是否在一个句子中,方法是循环遍历一个字符数组并检查相同的字符字符串。只要句子没有任何空格,程序就可以工作。我用谷歌搜索,他们的建议都是一样的;

cin.getline

But however I implement it, it either doesn't run or skips the entire input and goes straight towards the output.

但是无论我如何实现它,它要么不运行,要么跳过整个输入并直接进入输出。

How can I account for spaces?

如何计算空间?

#include <iostream>


using namespace std;

bool isPartOf(char *, char *);

int main()
{
char* Word= new char[40];
char* Sentence= new char[200];

cout << "Please enter a word: ";
cin >> Word;
cout << endl << "Please enter a sentence: "; 

//After Word is input, the below input is skipped and a final output is given.
cin.getline(Sentence, 190); 
cout << endl;

if (isPartOf(Word, Sentence)==true)
    {
        cout << endl << "It is part of it.";
    }
else
    {
       cout << endl << "It is not part of it.";
    }
}

bool isPartOf(char* a, char* b) //This is the function that does the comparison. 
{
    int i,j,k;

for(i = 0; b[i] != '
string Word, Sentence;

cout << "Please enter a word: "; cin >> Word;
cout << endl << Word;

cin.ignore();

cout << "\nPlease enter a sentence: "; getline(cin, Sentence); 
cout << endl << Sentence;
'; i++) { j = 0; if (a[j] == b[i]) { k = i; while (a[j] == b[k]) { j++; k++; return 1; if (a[j]=='
string Word, Sentence;

cout << "Please enter a word: "; getline(cin, Word); 
cout << endl << Word;

cout << "\nPlease enter a sentence: "; getline(cin, Sentence); 
cout << endl << Sentence;
') { break; } } } } return 0; }

And I am not allowed to use strstr for the comparison.

而且我不允许使用 strstr 进行比较。

回答by Patryk Krawczyk

Ok, I'll try to explain your problem:

好的,我会试着解释你的问题:

Let's assume this is your input:

让我们假设这是您的输入:

thisisaword
this is a sentence

thisisaword
这是一个句子

When you use cin and give it any input, it stops at the newline character which in my example follows character 'd' in 'thisisaword'.
Now, your getline function will read every character until it stops newline character.
Problem is, the first character getline encounters is already a newline so it stops immediately.

当您使用 cin 并为其提供任何输入时,它会停在换行符处,在我的示例中,该换行符位于 'thisisaword' 中的字符 'd' 之后。
现在,您的 getline 函数将读取每个字符,直到它停止换行符为止。
问题是, getline 遇到的第一个字符已经是换行符,因此它会立即停止。

How is this happening?

这是怎么回事?

I'll try to explain it like this:

我会试着这样解释:

If this is your input given to a program (note \n characters, treat it like a single character):

如果这是您对程序的输入(注意 \n 字符,请将其视为单个字符):

thisisaword\n
this is a sentence\n

thisisaword\n
这是一个句子\n

What your cin function will take and leave:

你的 cin 函数将接受和离开:

\n
this is a sentence\n

\n
这是一个句子\n

Now getline sees this input and is instructed to get every character until it meets a newline character which is "\n"

现在 getline 看到这个输入并被指示获取每个字符,直到遇到一个换行符“\n”

\n <- Uh oh, thats the first character it encounters!
this is a sentence\n

\n <- 哦哦,这是它遇到的第一个字符!
这是一个句子\n

cin reads input and leaves "\n", where getline includes "\n".

cin 读取输入并留下“\n”,其中 getline 包含“\n”。

To overcome this:

为了克服这个:

\n <- we need to get rid of this so getline can work
this is a sentence\n

\n <- 我们需要去掉这个,这样 getline 才能工作,
这是一个句子\n

As said, we cannot use cin again because it will do nothing. We can either use cin.ignore() without any parameters and let it delete first character from input or use 2x getline(first will take remaining \n, second will take the sentence with \n)

如前所述,我们不能再次使用 cin 因为它什么也不做。我们可以使用不带任何参数的 cin.ignore() 并让它从输入中删除第一个字符或使用 2x getline(第一个将使用剩余的 \n,第二个将使用 \n 的句子)

You can also avoid this kind of problem switching your cin >> Word; to a getline function.

你也可以避免这种切换你的cin >> Word的问题;到 getline 函数。

Since this is tagged as C++ I changed Char*[] to Strings for this example:

由于这被标记为 C++,因此我将这个示例的 Char*[] 更改为 Strings:

std::cin >> std::noskipws >> a >> b >> c;

OR

或者

std::cin >> std::skipws >> a >> b >> c;

回答by Patryk Krawczyk

How about using this:

如何使用这个:

std::cin >> std::skipws >> a >> std::noskipws >> b;

cin by default utilizes something like this:

cin 默认情况下使用这样的东西:

is.unsetf(ios_base::skipws)

And you can combine flags:

您可以组合标志:

##代码##

Tell me if it works for you : )

告诉我它是否适合你:)

回答by ravi

By default operator>> skips whitespaces. You can modify that behavior.

默认情况下 operator>> 跳过空格。您可以修改该行为。

##代码##

will cause is's >> operatorto treat whitespace characters as ordinary characters.

将导致is's >> operator将空白字符视为普通字符。