Question
Write a program that allows the user to type in any one-line question and then answers that question. Your program won't really respond to the
Write a program that allows the user to type in any one-line question and then answers that question. Your program won't really respond to the question, rather it will read a random response from a file.
Requirements
Your program should have a function called getQuestion() which prompts the user for a question and returns the number of characters in the question (not counting the newline). So, if the user types What is the meaning of life? getQuestion() will return 28.
Your program should have a function called writeAnswers() which accepts an ofstream parameter and writes into the file connected to the ofstream the following text (which are your answers):
I'm not sure, but I think you will find the answer in Chapter #. That's a good question. If I were you, I would not worry about such things. That question has puzzled philosophers for centuries. I don't know. I'm just a machine. The answer is 42. Think about it and the answer will come to you. I used to know the answer to that question, but I've forgotten it. The answer can be found in a secret place in the woods.
Your program should have a function called displayAnswer() which receives an ifstream parameter and an int parameter. The ifstream parameter should be connected to the file containing the answers. The int parameter should be the number of characters in the question asked. displayAnswer() then displays the answer from the ifstream file on the line number that is the result of: (number of characters in the question) % 9 + 1. We use 9 because that is the number of answers in the file. For instance, What is the meaning of life? has 25 characters, 25 % 9 +1 = 8. So the answer displayed would be "I used to know the answer to that question, but I've forgotten it." If line 1 is selected for printing, change the # to a random number from 1 to 18 before displaying. So if the random number selected was 5 it would display "I'm not sure, but I think you will find the answer in Chapter 5."
After displaying the answer your program should ask the user if they'd like to ask another question. If they input Yes it should prompt for another question and answer it. The program should repeat until the user enters No.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started