Zookal
Zookal

We’d love to hear any feedback or comment from you!

© 2011-2021 Zookal Pty Ltd

View question and answer

From our collection of questions and answers
Engineering · Computer Science
Question details

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.
Answer
Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.

Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.