MC logo
Remove Indentation
[^] Code Examples
<<Convert To Upper Case in C++ nodent.cc Input Statistics>>
/* This program removes any indentation and blank lines from its input. */

#include <iostream>
#include <cctype>

using namespace std;

int main() 
        char inch;                      // Input character.
        bool suppressing = true;        // Currently suppressing leading space.

        while(cin.get(inch)) {
                /* At the end of a line, if we have suppressed up to this
                   point, squash the newline, too, to eliminate the whole
                   line.  Otherwise, begin suppression for the next line. */
                if(inch == '\n')
                                suppressing = true;
                        /* Not end-of-line.  See if we are worrying about
                           suppressing spaces right now. */
                                /* We are suppressing spaces.  If it's a space,
                                   squash it; if not, we aren't suppressing
                                   anymore. */
                                        suppressing = false;

                /* If we got here, this character should not be suppressed. */
                cout << inch;

It's also worth noting the way this program uses the continue directive. The loop test reads the next character, entering the loop if there is one. The character is then printed at the end of the loop. Since the continue statement transfers control directly to the loop test, it has the effect of skipping the output and proceeding to the next character.
<<Convert To Upper Case in C++ Input Statistics>>