Source of reverse_line.cpp


  1: /** @file reverse_line.cpp
  2: Reads in a line of input and then writes out
  3: the line with all characters in reverse order.
  4: */

  6: #include <iostream>
  7: using namespace std;

  9: #include "utilities.h"
 10: using Scobey::Pause;
 11: using Scobey::userSaysYes;

 13: void ReadRestOfCharsAndWriteInReverse()
 14: /**<
 15: Read a string of characters from an input line and then write
 16: those characters out in reverse.
 17: @return Nothing.
 18: @pre The standard input stream is empty.
 19: @post All characters entered by the user, up to the first newline
 20: character, have been read in and written out in reverse order.
 21: */
 22: {
 23:     char ch;

 25:     cin.get(ch);
 26:     if (ch == '\n')
 27:         ; //Stop. That is, do nothing, since you're finished.
 28:     else
 29:     {
 30:         ReadRestOfCharsAndWriteInReverse();
 31:         cout << ch;
 32:     }
 33: }

 35: int main()
 36: {
 37:     cout << "\nThis program reads a sentence and "
 38:         "then writes it out in reverse order.\n";
 39:     Pause();

 41:     do
 42:     {
 43:         cout << "Enter a sentence on the following line "
 44:             "and press Enter:\n";
 45:         ReadRestOfCharsAndWriteInReverse();
 46:         cout << "\n\n";
 47:     }
 48:     while (userSaysYes("Do it again?"));
 49: }