Is there a function like getline
or getchar
that will get the next character even if that char
is whitespace? So if I had a bc
, I would have to call that function four times to get the 'a'
, ' '
, 'b'
, and 'c'
.

- 102,968
- 15
- 177
- 252

- 1
- 1
- 5
-
1Maybe http://en.cppreference.com/w/cpp/io/manip/skipws – Neil Kirk Oct 21 '15 at 03:01
-
getline gets a line with 1 call, not 4. getchar is something completely different. – deviantfan Oct 21 '15 at 03:02
4 Answers
The Unformatted Input Functions do not skip whitespace - see here. Sounds like you want get
. From the full list linked above:
The following standard library functions are UnformattedInputFunctions.
std::getline, except that it does not modify gcount.
basic_istream::operator>>(basic_streambuf*)
basic_istream::get
basic_istream::getline
basic_istream::ignore
basic_istream::peek
basic_istream::read
basic_istream::readsome
basic_istream::putback, except that it first clears eofbit
basic_istream::unget, except that it first clears eofbit
basic_istream::sync, except that it does not modify gcount
basic_istream::tellg, except that it does not modify gcount
basic_istream::seekg, except that it first clears eofbit and does not modify gcount
std::ws, except that it does not modify gcount
An alternative is to disable skipping of whitespace inside Formatted Input Functions:
#include <iomanip>
...
char c;
std::cin >> std::noskipws >> c;

- 102,968
- 15
- 177
- 252
-
-
@Cheersandhth.-Alf: I can't claim much credit... saw the link atop the `::get` page rather than thinking to search for it directly. Cheers. – Tony Delroy Oct 21 '15 at 03:18
Maybe try ifstream as there is a manipulator to disable the whitespace skipping behavior:
stream >> std::noskipws;

- 2,697
- 1
- 12
- 19
Reading of single chars with the standard library's named character input functions, doesn't skip whitespace. I.e. the question has an incorrect assumption.
At the C++ level you can use istream::get
; there are various overloads.
At the C level, getchar
and getwchar
, plus family.
It's a good idea to train on finding things in documentation.

- 142,714
- 15
- 209
- 331
Getline takes an entire line so for 'a bc', you would have to parse that string by yourself.

- 700
- 10
- 22