How to read in newline without using .get()

Just wondering if there is a way to get something similar to this working without using .get():

1
2
3
4
5
6
7
8
//...
while(cin){
     char ch;
     std::cin >> ch;
     if(ch == '\n')
          std::cout << "newline\n";
}
//... 


Does anyone have an idea?
Last edited on
you could make your code work as-is, by executing std::cin >> std::noskipws; before the loop
Sorry, I malformed my question. See, this isn't for practical purposes as you can imagine. This is actually a question posed by someone learning the language through a textbook. The exercise in the textbook asks for something somewhat similar to the problem shown above, however, istreams have not been covered aside from their most basic usage. The exercise does hint that isspace() may be useful, however I don't see a difference in using isspace() and directly testing for '\n' other than that testing for '\n' is more direct.
Last edited on
That is very non-specific information.

The >> istream operators (formatted input) all skip whitespace by default, so if you want to cin >> ch without skipping whitespace (spaces, tabs, newlines, etc) you must first turn off the ‘skip whitespace’ flag as Cubbi indicated.

Otherwise, you must use one of the non-formatted input functions, like cin.get().

Hope this helps.
That is very interesting information, and I appreciate the insight. I'll have to look deeper into this book to see what the deal is with that question then. Thanks!
Topic archived. No new replies allowed.