| CS 311 Fall 2009 > Assignment 8 |
Assignment 8 is due at 5 p.m. Tuesday, December 8. It is worth 25 points.
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 subject
“DA8”.
wordcount.cpp, from Exercise A.
This file (or an archive file containing it)
should be attached to your e-mail message.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.
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
std::map.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.
!” operator
and member function eof.
For example, if you have an ifstream called infile,
then after every operation on infile,
you should do
if (!infile)
{
...
This is a check for an error on the last operation.
If this test succeeds (that is, if there was an error),
then check for EOF:
if (infile.eof())
{
...
If this is true, then the error was due to the end of the file, and so there is nothing wrong with the file; just stop reading. On the other hand, if you get an error that is not due to EOF, then there is something wrong with the file (or the device, or the connection with the device, or ...).
Using “cin >> ...”
is usually not appropriate when reading from the user,
because the user thinks in terms of lines, not words.
Instead, use std::getline to grab a whole line at a time.
Test the strange cases. For example, what if the user, when prompted for a filename, just hits ENTER? What if they type a bunch of blanks and then hit ENTER? What about a filename with a blank in the middle? What about the name of a file that does not exist? A file that exists but cannot be read? The name of an empty file? The name of a directory? Etc.
CS 311 Fall 2009: Assignment 8 /
Updated: 2 Dec 2009 /
Glenn G. Chappell /
ffggc@uaf.edu
|
|