Date: Mon, 11 Nov 1996 17:00:47 GMT Server: NCSA/1.5 Content-type: text/html Last-modified: Wed, 02 Oct 1996 20:28:54 GMT Content-length: 12560
For program 3, you will write a simple version of the game Hangman. In this game, you try to guess a "secret" word or phrase one letter at a time. If you guess all the letters to the word before making a certain number of mistakes, you win.
In order to use these provided files, you will link them with your source code. This means that several C++ source code files are combined into a single executable. Even in programs 1 and 2, your source code was linked with the standard library, enabling you to, for example, print to the screen.
The exact steps involved in setting up this linking process depend on the compiler and environment you are using:
#include <iostream.h> #include "r:\public\mbirk\string.h" string random_secret_phrase (); bool char_in_string (char ch, string string); int main () { cout << random_secret_phrase() << endl; return 0; }
When you run this program, it should print a single, "random" phrase and quit. You can now use this program as a "shell" or "template" for the hangman program.
Note: The next time you run Borland C++ in order to work on Program 3, you will need
to a "Project/Open", not a "File/Open." Then select "prog3.ide" from the file
selection window.
Borland C++ Outside of the Vectra Lab
If you are using Borland C++ at home or some other site than the Vectra Lab,
then read the instructions above. They are correct,
except that you cannot access the files on the r: drive. Instead,
you will have to download the ".cpp" and ".h" files
(note that the "Makefile" and "CVS" directory won't be used) and save them
in your working directory. (Note that these files are source files
ending in ".cpp" and ".h" instead of object files ending in ".obj" -
that's ok.) Then, when creating the project, eliminate the prefix
"r:\public\mbirk" and replace all of the ".obj" suffixes with ".cpp".
Note: If you are using Borland C++ 5.0, there is a minor incompatibilty.
Edit the "defs.h" file, and remove (or "comment out") all lines with the
word "bool". This is because 5.0 has the bool type built-in, but
4.5 does not.
Using GNU g++
The process for using g++ is very different. First, you need to download the source files. Download all six files
(ignore the "CVS" directory). Then, test these files by typing "make" in
the directory which contains them. It should compile the program to an
executable "prog3". Test it by typing "./prog3" - it will print a single
"secret phrase" and exit. Now you can use "prog3.cpp" as a basis for the
assignment; each time you want to compile it, type "make".
Any Other Environment
If you are using any other environment, such as Microsoft Visual C++ or
Turbo C++, you may need to do some extra work. This is because I don't have
access to these environments, so I can't test them out. You can try the
following: download the source files. Consult
your instructions on how to set up a projectt, and add the three ".cpp"
files to this project. You may need to modify the "defs.h" file. If you
can't get it to work, ask me. If you still can't get it to work, you will
have to do it in the Vectra Lab.
string first_name = "John"; string last_name = "Doe"; cout << "Hello, " << first_name << " " << last_name << "!\n";
In addition, you can "add" two strings together with the + operator. This is called concatenation:
string name = first_name + " " + last_name; cout << name << endl; // prints "John Doe" name += ", Jr."; // same as name = name + ", Jr." cout << name << endl; // prints "John Doe, Jr."
To find out how long a string is, use the length member function. For example:
string name; cout << "What is your name? "; cin >> name; cout << "Your name is " << name.length() << " characters long!\n";
You can also access individual characters of the string using the square brackets. Inside the brackets, put an integer expression that evaluates to the index of the character you are interested in. These indices start at 0, not 1. E.g.:
string test = "Test"; cout << test [0] << // prints "T" char ch = test [2]; // ch = 's'
To access all of the characters in a string, use the square brackets inside of a loop. For this you need to know how long the string is; use the length member function for this. Also, note that you can modifiy the characters of a string using the brackets. For example, to convert a string to all uppercase, you could do the following:
for (int i = 0; i < some_string.length(); i++) some_string [i] = toupper( some_string[i] );
For example, char_in_string ('x', "xyz") returns true, which char_in_string ('h', "Hello!") returns false.
Welcome to John Q. Doe's Hangman program! You have five chances to guess the secret phrase. Secret phrase: "____" 5 chances left! Enter a letter: a Good guess! Secret phrase: "_a_a" 5 chances left! Enter a letter: t Sorry, that letter is not in the secret phrase. Secret phrase: "_a_a" 4 chances left! Enter a letter: R Sorry, that letter is not in the secret phrase. Secret phrase: "_a_a" 3 chances left! Enter a letter: s Sorry, that letter is not in the secret phrase. Secret phrase: "_a_a" 2 chances left! Enter a letter: p Sorry, that letter is not in the secret phrase. Secret phrase: "_a_a" 1 chances left! Enter a letter: l Sorry, that letter is not in the secret phrase. Secret phrase: "_a_a" Sorry, you ran out of chances. The secret phrase was: "java" Welcome to John Q. Doe's Hangman program! You have five chances to guess the secret phrase. Secret phrase: "_______ ________" 5 chances left! Enter a letter: a Good guess! Secret phrase: "_a__a__ _a__a___" 5 chances left! Enter a letter: e Sorry, that letter is not in the secret phrase. Secret phrase: "_a__a__ _a__a___" 4 chances left! Enter a letter: # That is not a letter. Try again. 4 chances left! Enter a letter: l Sorry, that letter is not in the secret phrase. Secret phrase: "_a__a__ _a__a___" 3 chances left! Enter a letter: t Good guess! Secret phrase: "_a_ta__ _a__a___" 3 chances left! Enter a letter: i Good guess! Secret phrase: "_a_tai_ _a__a___" 3 chances left! Enter a letter: n Good guess! Secret phrase: "_a_tain _an_a___" 3 chances left! Enter a letter: c Good guess! Secret phrase: "Ca_tain _an_a___" 3 chances left! Enter a letter: P Good guess! Secret phrase: "Captain _an_a___" 3 chances left! Enter a letter: l You already guessed that letter. Try again. 3 chances left! Enter a letter: k Good guess! Secret phrase: "Captain Kan_a___" 3 chances left! Enter a letter: g Good guess! Secret phrase: "Captain Kanga___" 3 chances left! Enter a letter: r Good guess! Secret phrase: "Captain Kangar__" 3 chances left! Enter a letter: O Good guess! Secret phrase: "Captain Kangaroo" Congratulations! You guessed the secret phrase!