Std :: cin skips spaces

So I'm trying to write a function to check if a word is in a sentence by looping through a char array and checking for the same char string. The program works as long as there are no spaces in Sentence. I googled around and they are all the same suggestions;

cin.getline

      

But, however, I implement it, it either does not run or it skips all input and goes straight to the output.

How can I identify 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'm not allowed to use strstr for comparison.

+1


source to share


3 answers


Ok, I'll try to explain your problem:

Let's assume this is your input:

thisisaword
this suggestion

When you use cin and give it any input, it stops at the newline character, which in my example follows the 'd' character in 'thisisaword'.
Now your getline function will read each character until it stops the newline character. The problem is that the first character getline encounters are already newlines, so they stop immediately.

How does this happen?

I will try to explain it this way:

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

thisisaword \ n
this sentence \ n

What your cin function will take and leave:

\ n
is a sentence \ n

Getline now sees this input and is instructed to fetch each character until it encounters a newline character, which is "\ n"



\ n <- Oh, oh, this is the first character it encounters!
this sentence \ n

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

To overcome this:

\ n <- we have to get rid of this so that getline can work
this sentence \ n

As said, we can't use cin again because it won't do anything. We can either use cin.ignore () without any parameters and have it remove the first character from the input, or use 2x getline (takes the remaining \ n first, second takes the sentence with \ n)

You can also avoid this problem by switching your cin -> Word; to the getline function.

Since this is marked 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;

      

+9


source


By default, operator -> skips spaces. You can change this behavior.

is.unsetf(ios_base::skipws)

      



will cause it is >> operator

to treat whitespace characters as normal characters.

+2


source


How about this:

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

      

cin uses something like this by default:

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

+2


source







All Articles