CSE 333 Exercise 12

Out:   Wednesday, February 11
Due:   Friday, February 13 by 11 AM
Rating:   2 (note)
CSE 333 Exercise Rating Scale

Each exercise this quarter is rated on a integer scale of 1 – 5, inclusive, with 1 being the "least time-consuming" and 5 being the "most time-consuming".

This difficulty scale is meant as a rough guide for you in predicting the amount of time to set aside for each exercise as you balance the work required for 333 with your other obligations. However, it is necessarily imperfect as everyone's set of circumstances and experiences with the exercises differ. If your experience with an exercise does not align with its rating, that is not a reflection of you or your abilities.

Goals

  • Use C++ streams to read files.
  • Use C++ STL containers (e.g., map).
  • Write templatized C++ code.

Problem Description

Write a C++ program that reads a file whose filename is given as a single command-line argument. The program should:

  1. Read and count the individual strings in the file.
    • Strings must match exactly to be considered the same. Thus, word, Word, and woRD are counted separately because they contain different capitalizations.
  2. Print the final list of the strings, sorted using the ordinary ordering relation (i.e., <), and the number of times each word appears in the file.
    • There should be exactly one space character between the string and its count, with no whitespace after the count or before the string.

Example transcript:

$ cat quotes.txt
to be or not to be
to do is to be
to be is to do
do be do be do
$ g++ -Wall -g -std=c++17 -o ex12 ex12.cc
$ ./ex12 quotes.txt
be 6
do 5
is 2
not 1
or 1
to 6

Implementation Notes

C++ STL Library

Take advantage of the C++ STL library; one of the map containers should be particularly useful. For reading files, take a look at the ifstream class.

File Reading

  • You can assume the input file contains ASCII text; you do not need to verify this. You may NOT assume that the file ends with a newline character ('\n').
  • In this problem, we define a string as a series of non-whitespace characters. Use the C++ >> operator to read the input file one string/"word" at a time; you do not need to do any additional processing (e.g., whitespace or punctuation).
  • For checking for errors, we recommend you investigate the good(), bad(), and eof() methods for streams. More information on how to read files in C++ can be found here.

Template Function

You MUST factor out a helper template function with the following prototype:

template <typename T> bool ReadValue(ifstream& in, T* const output);
  • Each time the client calls ReadValue, it should read one token from the input stream in, attempt to parse the token into type T, and store the value in the location pointed to by output.
  • Returns false if either reading or parsing fails (e.g., conversion errors, EOF conditions), and true otherwise.

Templates

When you write template code, you should make as few assumptions about the template type's functionality as possible (i.e., what it has implemented). For this exercise, you may assume there is a default constructor and operator>> with input streams defined. Note that this means that if you were to change main and your input file to only contain integers instead of strings, your ReadValue function should still work! However, make sure that the code you submit reads strings.

Style Focus

File Reading Errors

This exercise reads in a file, which can raise errors that need to be checked accordingly. If unrecoverable errors are detected, be sure that you print a useful message to standard error and return EXIT_FAILURE from main. For this exercise, you can consider a parsing error to be unrecoverable.

Submission

Submit the following file(s) by creating an ex12-submit tag in your exercise repo before the assignment deadline. The file(s) should be located in the exact directory listed below, including capitalization:

  • ex12/ex12.cc

Your code must:

  • Compile without errors or warnings on CSE Linux machines (lab workstations, attu, or CSE home VM).
  • Have no runtime errors, memory leaks, or memory errors (g++ and valgrind).
  • Be contained in the file listed above that compiles with the command:
    $ g++ -Wall -g -std=c++17 -o ex12 ex12.cc
  • Have a comment at the top of your .cc file with your name(s) and CSE or UW email address(es).
  • Be pretty: the formatting, modularization, variable and function names, commenting, and so on should be consistent with class style guidelines. Additionally, the linter shouldn't have any complaints about your code (cpplint.py).
  • Be robust: your code should deal with hard-to-handle/edge cases and bogus user input (if there are any) gracefully.