Download this source file


// LineNum2.cpp - a program that prepends line numbers
// to each line read from the standard input, and
// outputs the result.  Note that the use of setw manipulator
// requires the additional header file <iomanip.h>.
// This version demonstrates the use of the getline member function.
//
// Written by Wayne Pollock, Tampa FL 1999

#include <iostream>
#include <iomanip>

const int LINE_LENGTH = 256;

int main ()
{
   int line_num = 1;
   char line[LINE_LENGTH];   // Note the use of const int as array bounds.

   while ( cin.getline(line, LINE_LENGTH) )
   {
      cout << setw(4) << line_num << ": " << line << endl;
      ++line_num;
   }
   return 0;
}