Wizard Laboratory

November 1997

Introduction

In this laboratory you will work with strings of characters. You will read the question typed in by the user, extract the first word to determine the type of question asked, and then answer the user with an answer from the appropriate category.

Goals

The Tasks of this Laboratory

In this program you will work with character strings . You will read user's question into a variable of the type string. You will then isolate the first word of the question by looking for the first character that is not a letter. Once you know the first word, you will convert it to all lower case letters by using the ChangeCase( ) function. In the rest of the program, you will have a series of if statements - if the first word is 'how', select one of three answers to 'how' type of question, else if the first word is 'when', select... To select the answer, pick a random number between 1 and 3 and use a switch statement to output the answer.

Before you begin programming, you should run the solution program WizardSolution. As you can see, the computer is capable of answering many silly questions. You may use the same answers or write your own.

Working with Character Strings
The main loop is written for you.

string question;

	while (ReadingString("Enter a question:\n", question)) {
 		cout << Answer(question) << endl;
  }
The function ReadingString() is defined in IOtools. It writes out the prompt "Enter a question:". The characters '\n' together form the new line character and cause the cursor to move down a line to wait for the user's response. The string typed in is put into the variable question. The function ReadingString() returns true as long as something is typed and false if only return is pressed. In the later case, the loop will end.

Writing the Code
You must implement three functions:

bool IsLetter(char ch);
Return true if 'a' <= ch <= 'z' or 'A' <= ch <= 'Z'.

string FirstWord(string s);
Locate the position of the first character in question that is not a letter.
Copy the part of question before that position into a new string WordOne.

string Answer(const string question);
Convert WordOne to all lower case letters.
Compare WordOne to one of your question words.
Tips and Tools

From IOtools :
	ChangeCase(string-variable, LowerCase)

Changes all the letters in string-variable to lower case.
	SquashString(string-variable)

Removes leading and trailing blanks from the string-variable

From the string class:

If s if of type string:

Generating apparently random responses :
Select a random number between 1 and 3 and use it as a selector for a switch statement that prints one of three possible answers.

You should be able to answer questions that start with the words: who, where, when, how, why, and what .

Extra Credit: (1 point max)
Make your program 'smarter' - for example differentiating between how long, how often, how many, etc.

Hand in diskette and hard copy of your program and also a sample one page output of your questions and answers. (Save the console window in your folder and print it.) The diskette should also include the project and application.

Sample Run

Professor Fell Answers.

Enter a question:
Where should I go tomorrow?
On the roof
Enter a question:
What will I find there?
I have no idea.
Enter a question:
Why should I go there?
You'll understand when you're older.
Enter a question:
When is the best time to go?
This time next year
Enter a question:
OK, I'll wait.
I didn't understand that. Try again.
Enter a question:
Who taught you to talk like that?
Mickey Mouse
Enter a question:
How long have you been thinking?
Eons.
Enter a question:
Why should I?
Because I said so.
Enter a question:

No Question Terminating Program
Select Quit from the File Menu


Last Updated: October 23, 1997 8:39 am by
Harriet Fell
College of Computer Science, Northeastern University
360 Huntington Avenue #161CN,
Boston, MA 02115
Internet: fell@ccs.neu.edu
Phone: (617) 373-2198 / Fax: (617) 373-5121
The URL for this document is: http://www.ccs.neu.edu/home/fell/COM1100/PROG/Wizard.html