cin.get(char) to the Rescue
Usually, programs that read input character-by-character need to examine every character, including spaces, tabs, and newlines. The istream class (defined in iostream), to which cin belongs, includes member functions that meet this need. In particular, the member function cin.get(ch) reads the next character, even if it is a space, from the input and assigns it to the variable ch. By replacing cin>>ch with this function call, you can fix Listing 5.16. Listing 5.17 shows the result.
Listing 5.17. textin2.cpp
// textin2.cpp -- using cin.get(char)#include <iostream>int main(){ using namespace std; char ch; int count = 0; cout << "Enter characters; enter # to quit:\n"; cin.get(ch); // use the cin.get(ch) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access