std::cin skips white spaces

匿名 (未验证) 提交于 2019-12-03 08:28:06

问题:

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] != '\0'; i++) { j = 0;  if (a[j] == b[i]) {     k = i;     while (a[j] == b[k])     {          j++;         k++;         return 1;         if (a[j]=='\0')             {                 break;             }         }      }   } return 0; } 

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

回答1:

Ok, I'll try to explain your problem:

Let's assume this is your input:

thisisaword
this is a sentence

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.

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):

thisisaword\n
this is a sentence\n

What your cin function will take and leave:

\n
this is a sentence\n

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

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

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

To overcome this:

\n <- we need to get rid of this so getline can work
this is a sentence\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)

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

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

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; 

OR

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; 


回答2:

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

is.unsetf(ios_base::skipws) 

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



回答3:

How about using this:

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

cin by default utilizes something like this:

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

And you can combine flags:

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

Tell me if it works for you : )



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!