<<< Week 9. Ch.11, Unformatted I/O and character classification | Index | Character classification functions >>> |
We can read individual characters:
Using formatted input:
char ch; while ( cin >> ch ) { // read into ch, skipping whitespace characters if ( isalpha( ch ) ) { // do something } }
Using unformatted input:
#include <iostream> #include <cctype> char ch; while ( cin.get( ch ) ) { if ( isspace( ch ) ) { // if ch is whitespace // do nothing (i.e. skip whitespace) } if ( isdigit( ch ) ) { // read a number } else if ( isalpha( ch ) ) { // read an identifier } else { // deal with operators } }
A complete example: chapter_11_6.cpp (download)
<<< Week 9. Ch.11, Unformatted I/O and character classification | Index | Character classification functions >>> |