Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::cin skips white spaces

Tags:

c++

spaces

cin

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.

like image 342
Pejman Poh Avatar asked Nov 29 '14 17:11

Pejman Poh


People also ask

Does CIN skip whitespace?

You can use cin but the cin object will skip any leading white space (spaces, tabs, line breaks), then start reading when it comes to the first non-whitespace character and then stop reading when it comes to the next white space. In other words, it only reads in one word at a time.

How do you skip a white space in C++?

Conclusion. To remove whitespace from a string in C++, we can use the std::remove_if function, the std::regex_replace function, or the Boost Library in C++. The erase_all() function in the boost library can be used to remove any specific type of whitespaces from a string.

Does C++ ignore white space?

The C++ compiler generally ignores whitespace, with a few minor exceptions (when processing text literals). For this reason, we say that C++ is a whitespace-independent language.

Why do we use CIN ignore () in C++?

The cin. ignore() function is used which is used to ignore or clear one or more characters from the input buffer.


2 Answers

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;
like image 101
Patryk Krawczyk Avatar answered Sep 21 '22 06:09

Patryk Krawczyk


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.

like image 43
ravi Avatar answered Sep 19 '22 06:09

ravi