CS 311 Fall 2009  >  Assignment 8

CS 311 Fall 2009
Assignment 8

Assignment 8 is due at 5 p.m. Tuesday, December 8. It is worth 25 points.

Procedures

This is a group assignment. Please work in a group of 2 or 3. Each group only needs to turn in a single copy of the assignment. Under normal circumstances, each will receive the same grade on the assignment.

E-mail answers to the exercises below to ffggc@uaf.edu, using the subjectDA8”.

Exercises (25 pts total)

Exercise A — Word Counting Program

Purpose

In this exercise, you will write a program that uses a C++ STL Table implementation. Your program will also read information from a file. Proper error checking is important part of the assignment.

Instructions

Write a complete C++ program (including function main!) that inputs a filename from the user, reads the named file, and prints a list of the words contained in the file, together with the number of times the word appears. Be sure to follow the coding standards. All standards now apply! Your program should be stored in the file wordcount.cpp.

The program should begin by prompting the user to type a filename. Once the user types the filename and presses ENTER, the program attempts to read the file. If there is any error, other than end-of-file, in the file reading process, then the program should output an error message and start over, returning to the input prompt. If the file is successfully read, then the program should print the word-count statistics for the file, and then exit.

A word is a nonempty sequence of characters containing no whitespace (blank, tab, newline, carriage return, vertical tab, form feed) characters. Words in the file are separated by whitespace. Case is significant (so “A” is different from “a”), and punctuation is considered part of the word.

The word-count statistics consist of a sequence of output lines. Each line should have a word, followed by a single blank, followed by an integer. The word is a word in the file, and the integer is the number of times the word appears in the file. The words should be listed in lexicographic order (the order given by the “<” operator for std::string), and no word should appear more than once in the list.

For example, suppose your program reads the following.

Three THREE four two one three
two two, tw,o three THREE          four
THREE four Three tw,o three four Three two,

Then it should output the following word-count statistics.

THREE 3
Three 3
four 4
one 1
three 3
tw,o 2
two 2
two, 2

Other Requirements

Testing

Since you are writing a complete program this time, there will be no test program. However, I have created a test input file: wordcount_test.txt. If you run your program with this file as input, then it should output the following word-count statistics.

2 1
2009 1
311 1
8, 1
A 1
Assignment 1
CS 1
Can 1
Chappell 1
Dec 1
End 1
Exercise 1
G. 1
Glenn 1
Next 1
Test 1
a 4
correctly 1
count 1234
file 2
for 1
input 1
is 1
line. 2
line? 1
long 3
of 1
with 1
wordcount_test.txt 1
you 1

Do not turn in wordcount_test.txt.

Note that I may test your program using a different input file.

Hints


CS 311 Fall 2009: Assignment 8 / Updated: 2 Dec 2009 / Glenn G. Chappell / ffggc@uaf.edu Valid HTML 4.01!