TDT4102, Spring 2009

Exercise 7

Deadline: 13.03.2009


The objective of this exercise:

General requirements:

Tools:

Recommended reading:


Part 1: File output and input (20 pt.)

a) Write a program that reads words from the user input (cin), and stores each word as a separate line in a text file. 

Hints: Store the  input from the extraction  operator in a string and
use a specific word to terminate the  input process such as "quit".
Use the compare method  of the string class to test if the temination word is found (this function will return 0 if  the strings are equal).

b) Write a program that reads the same text file, and creates a new file (with another name) with the same text but with line numbers. Implement the code that is needed to test for common errors such as file not found.

File paths are a source of confusion when reading and writing to files. The programs you create will typically store files in the working directory of the project if you use only the file name (not including the full path).

The best way to find out where the file ends up is to simply create a file and look for the file in the project directory.

 

Text files that you use in your Visual Studio project can be added to the project (or created) as items in the projects “Resource folder”. If you're not using Visual Studio, you can manually create a text file in the same folder as the executable program (the folder in which you run the program) so that you don't have to worry about file paths.


Part 2: File Input: character statistics (20 pt.)

In this exercise you will read from a text file and create statistics about characters and words. To get started you will need a text file containing a reasonable amount of ordinary text (at least a few lines). Use any text file you like, or create a new text file with the program you wrote in part 1.

 

a) Write a program that reads a text file and displays character statistics (using cout); the number of characters in the file and the occurrence of each character (how many times a character is used in the text). You can limit the number of different characters you count to normal letters (a-z) and you can futher simplify by converting uppercase to lowercase using tolower(char).

Hint: This can be achieved by using an array with the length equal the number of different characters. You can limit the number of different characters you count to normal letters. For instance, if you encounter the character 'e', you should increment the element of the array in position 'e'-'a' (characterCount['e'-'a']). Make sure to stay within the range of array.

Example output:

Character statistics:
Total number of chararcters: 555
a: 38   b: 1    c: 45   d: 13
e: 46   f: 26   g: 4    h: 15
i: 64   j: 0    k: 0    l: 33
m: 14   n: 48   o: 40   p: 8
q: 0    r: 36   s: 29   t: 60
u: 24   v: 3    w: 3    x: 1
y: 0    z: 4

Part 3: File Input: word statistics (30 pt.)

Write a program that creates statistics about the words in a text file. You should at least count the words in the file, how many times each word is used and find the longest word. Display the statistics by sending it to cout.

Example output:

 
Text statistics:
Longest word: alphabet
Number of words: 49
Number of lines: 5
Average word length: 7
this: 1
is: 1
a: 1
very: 1
exercise: 1
....
....
....


 
Hints: Words can be extracted by using the extraction operator in combination with a string. It is probably a good idea to create a class or struct with member variables for word and occurrence. Remember that strings have to be compared by using the compare function in the string class.

Part 4: Hangman (30 pt.)

Hangman is a game usually played with paper and pencil. One player thinks of a word and the other tries to guess it by suggesting letters. The word to guess is represented by a row of dashes, giving the number of letters. If the suggested letter doesn't occur in the word, the other player draws one element of the hang man diagram. The game is over when either the diagram is completed, or the word is guessed. The diagram is designed to look like a hanged man, but in this exercise it can be represented by a decreasing integer value for each wrong letter guessed.

Start by writing a program which reads words from a text file (hangman.txt) and puts each word in a vector ( that is, you should put all of the words into the same vector ). The text file should contain one word on each line.
Example of hangman.txt content:

 
swordfish
programming
blue
freebird

Modify the program so that the user is asked to guess a letter in a randomly chosen word from the vector. Give the user some hint on how many letters the word has, and which letters he has left to guess. This should continue to loop until the game is over. To make the game easier to code, you can limit the characters in the game to lowercase only.

When the user has guessed all the correct letters, the program should congratulate the user. If the player used more than a given number (choose a reasonable number) of tries, the game will be over and the player lost.
How you choose to implement the game rules are up to you, but a playable hangman game is required.

Hint: Use the string member function find(char c) to check if the string contains the character:

 
if (myString.find(currentLetter) != string::npos) {
        // currentLetter is in the string
}


Optional: Don't let the user guess the same letter twice and display which letters he has already guessed.
Optional: Print a list after each input with all the letters which the player has guessed, but wasn't in the word.
Optional: Modify the program so that the player can submit his own words to the list (use code from part 2). Also, let the player choose the length of the word which the program is going to pick.

Example of output:

 
Welcome to Hangman!
A random word has been chosen, try to guess it!
Current word: _______
Tries: 5
> e
Current word: __ee____
Tries: 5
> i
Current word: __ee_i__
> x
Current word: __ee_i__
Tries: 4
(... etc ...)
>b
Current word: freebird
YOU WIN!