#include #include #include // for exit #include // for isspace #include using namespace std; #include "prompt.h" // count # of lines and chars in input file int main() { long numChars = 0; long numLines = 0; long numWords = 0; char ch; bool inWord = false; // initially not reading a word string filename = PromptString("enter name of input file: "); ifstream input; input.open(filename.c_str()); if (input.fail() ) { cout << "could not open file " << filename << endl; exit(1); } while (input.get(ch)) // reading char succeeds? { if ('\n' == ch) // read newline character { numLines++; } numChars++; if (isspace(ch)) { if (inWord) // just finished a word { inWord = false; numWords++; } } else // not a space { if (! inWord) // just started a word { inWord = true; } } } if (inWord) numWords++; // ended in a word cout << "lines = " << numLines << "\tchars = " << numChars << "\twords = " << numWords << endl; return 0; }