-3

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'.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
Matt
  • 1
  • 1
  • 5

4 Answers4

3

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;
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
1

Maybe try ifstream as there is a manipulator to disable the whitespace skipping behavior:

stream >> std::noskipws;
dcieslak
  • 2,697
  • 1
  • 12
  • 19
1

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.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

getline(cin,variable)

Getline takes an entire line so for 'a bc', you would have to parse that string by yourself.

Stefan
  • 700
  • 10
  • 22