cin not reading with the >> operator

The >> operator skips whitespace until it sees non-whitespace, and than reads characters until the next whitespace.

The problem is that if there is white space at the end of a line, cin >> ... will often appear to not work, as it is simply reading until the end of the line, and failing to find anything to return.

The solution is to force the input to the next line, this can be done with the istream ignore method, which read up to a given number of characters until if finds, and consumes, a provided character. In this case we will search for and consume the newline, or '\n' character:


cin.ignore (100, '\n');
cin >> mydata;

Will force the reading into mydata to begin on a new line.

-- JimSkon - 2011-03-17

Topic revision: r1 - 2011-03-17 - JimSkon
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback