Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effect of noskipws on cin>>

As I understand, the extraction operator skips the whitespace in the beginning and stops upon encountering a whitespace or end of stream. noskipws can be used to stop ignoring the leading whitespaces.

I have the following program where I have used noskipws.

#include <iostream>
using namespace std;

int main()
{
    char name[128];

    cout<<"Enter a name ";
    cin>>noskipws>>name;
    cout<<"You entered "<<name<<"\n";

    cout<<"Enter another name ";
    cin>>name;
    cout<<"You entered "<<(int)name[0]<<"\n";

    return 0;
}

My queries are:

  1. If I enter "John" as the first input, then the second cin>> operation does not wait for input and does not copy anything to the destination i.e. the name array. I expected second cin>> to transfer at-least a newline or end of stream, instead of just setting the destination string to empty. Why is this happening ?

  2. The same thing is observed when I enter "John Smith" as the input for first cin>> statement. Why doesn't the second cin>> statement copy the space or "Smith" to the destination variable ?

Following is the output of the program:

Enter a name John
You entered John
Enter another name You entered 0


Enter a name John Smith
You entered John
Enter another name You entered 0

Thanks!!!

like image 482
Achint Mehta Avatar asked Jun 08 '12 10:06

Achint Mehta


People also ask

What does STD Noskipws do?

std::skipws, std::noskipwsEnables or disables skipping of leading whitespace by the formatted input functions (enabled by default).

What does Noskipws mean in C++?

The noskipws() method of stream manipulators in C++ is used to clear the showbase format flag for the specified str stream. This flag reads the whitespaces in the input stream before the first non-whitespace character.

What does cin >> n mean in C++?

cin is the standard input stream. Usually the stuff someone types in with a keyboard. We can extract values of this stream, using the >> operator. So cin >> n; reads an integer. However, the result of (cin >> variable) is a reference to cin .

Does CIN get ignore whitespace?

Using cin. 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.


1 Answers

The basic algorithm for >> of a string is:

skip whitespace
read and extract until next whitespace

If you use noskipws, then the first step is skipped. After the first read, you are positionned on a whitespace, so the next (and all following) reads will stop immediatly, extracting nothing.

>> to a string will never put whitespace into the string. More generally, using >> with noskipws is problematic, since whitespace is always a separator for >>; it may make sense to use it punctually, but it should generally be reset immediately after it has been used. (The once case where it might make sense is when using >> to a char. In this case, the stream always extracts one character.)

like image 95
James Kanze Avatar answered Sep 27 '22 19:09

James Kanze