std::cin skips white spaces

Pejman Poh picture Pejman Poh · Nov 29, 2014 · Viewed 30k times · Source

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.

Answer

Patryk Krawczyk picture Patryk Krawczyk · Nov 29, 2014

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;