Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C++ program with the following characteristics: Main program in main.cpp Accepts a single command line argument, the name of a text file. A

Write a C++ program with the following characteristics:

  • Main program in main.cpp
    • Accepts a single command line argument, the name of a text file.
  • A class named CountWords (files CountWords.h and CountWords.cpp)
    • Constructor accepts the name of a text file as above.
    • Implements these public methods (each has a return value of type uint32_t):
      • getWordCount: returns the number of "words" in the file. A "word" is any string of contiguous non-whitespace characters as determined by the >> operator input for a string variable.
      • getLineCount: returns the number of lines in the file.
      • getMaxWordsInLine: returns the maximum number of words in any line in the file.
    • Your class must read the file only once, in the constructor, and should discard the original file contents after reading and just keep the appropriate counts. The constructor should close the file.

Your main program should create a single instance of the CountWords class using the file specified as the program argument, and should then print out the number of words, lines, and maximum words in a line in the file in the following format, substituting the actual numbers for the highlighted numbers in the example:

3482 words, 291 lines, 11 max words in line

All printing must be done in main; the CountWords class should not do any printing.

Any errors encountered in the constructor (such as input file not found) should throw an exception of type std::runtime_error (Links to an external site.)Links to an external site.. A string describing the error should be supplied as the exeception argument. The string should also include the file name.

Your program should conform to the course Programming Standards.

You must define your class such that copy and move constructors and assignments are not allowed. You must define a destructor, even though the destructor body will be empty.

Implementation Notes for WordCount

  • Use the C++ type ifstream (Links to an external site.)Links to an external site. for the input file.
  • Use the function getline (Links to an external site.)Links to an external site. to read each line of the file into a string.
  • For each line, create a variable of type istringstream (Links to an external site.)Links to an external site. to hold the line contents for parsing words.
  • Parse the words by using the >> operator with the istringstream (Links to an external site.)Links to an external site. variable and a string destination.
  • Both the getline (Links to an external site.)Links to an external site. function and the >> operator return a value which can be tested as true or false in an if or while statement. A false value indicates end of file or error.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Filing And Computer Database Projects

Authors: Jeffrey Stewart

2nd Edition

007822781X, 9780078227813

More Books

Students also viewed these Databases questions

Question

2. Are my sources up to date?

Answered: 1 week ago