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:
ChangeCase(string-variable, LowerCase)
SquashString(string-variable)
From the string class:
If s if of type string:
s.substr(pos, num) // substring of s consisting of num characters starting at position pos
s[pos] // the character at position pos in s
s == "abcde" // true if and only if the two strings are equal
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