in c++
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 (including spaces and punctuation, not including the ending newline). If the user enters an empty string they should be given an error and prompted to enter another question.
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. Opening and closing the file should be handled outside writeAnswers(). The function writes into the file connected to the ofstream exactly 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! You should google it.
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. Opening and closing the file should be handled outside displayAnswer().
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 28 characters, 28 % 9 +1 = 2. So the answer displayed would be line 2 in the file, "That's a good question! You should google it."
If line 1 is selected for printing, replace the # with 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.
add. notes
//displayAnswer() which receives an ifstream parameter and an int parameter.- function should use get to read char by char till line end char
"ch =char"
should look something like
for (inti=1,i
do
{
infile.get (ch);
while (ch != '\n')
}
reset ch
while(ch != '\n')
{infile.get(ch)
if ((position==1)&&(ch=='#')
make random #
cout<
else
cout <
//function called writeAnswers() should create a .txt file that is populated with the answers the first time it runs
the random number should be seeded with time
Screenshot
-------------------------------------------------------------------------
Program
//Header files
#include <iostream>
#include<fstream>
#include<string>
#include<algorithm>
using namespace std;
//Function prototype
void writeAnswers(ofstream &);
int getQuestion();
void displayAnswer(ifstream&,int);
int main()
{
//Variable for loop continuation
string ch;
//Write file object
ofstream
out("C:/Users/deept/Desktop/answers.txt");
//File open error check
if (!out) {
cout << "File not found!!!"
<< endl;
exit(0);
}
//Write into file function call
writeAnswers(out);
//Close the file
out.close();
//Loop until no
do {
//Input file object
ifstream
in("C:/Users/deept/Desktop/answers.txt");
//File open error check
if (!in) {
cout <<
"File not found!!!" << endl;
exit(0);
}
//Call method to get question and
display answer
displayAnswer(in,
getQuestion());
//Close the file
in.close();
//Repeatation question
cout << "Do you want to
continue(yes/no)? ";
cin >> ch;
//Convert into uppercase
transform(ch.begin(), ch.end(),
ch.begin(), ::toupper);
//Error check
while (ch != "YES"&& ch !=
"NO") {
cout <<
"\nIncorrect option!!! Please enter agsin!!!" << endl;
cout <<
"Do you want to continue(yes/no)? ";
cin >>
ch;
transform(ch.begin(), ch.end(), ch.begin(), ::toupper);
}
cin.ignore();
} while (ch == "YES");
//End
cout <<
" Thank you!!!"
<< endl;
}
//Function to write answers into file
void writeAnswers(ofstream &out) {
out << "I'm not sure, but I think you will find
the answer in Chapter #.\n";
out << "That's a good question! You should
google it.\n";
out << "If I were you, I would not worry about
such things.\n";
out << "That question has puzzled philosophers
for centuries.\n";
out << "I don't know. I'm just a
machine.\n";
out << "The answer is 42.\n";
out << "Think about it and the answer will come
to you.\n";
out << "I used to know the answer to that
question, but I've forgotten it.\n";
out << "The answer can be found in a secret
place in the woods.\n";
}
//Function to prompt for question and return length of
question
int getQuestion() {
string question;
cout << "Enter your question? ";
getline(cin, question);
return question.length();
}
//Function to read answers from file and display proper
answer
void displayAnswer(ifstream& in, int numChars) {
int lineCount = numChars % 9 + 1;
string line;
int count = 0;
while (!in.eof()) {
getline(in,line);
count++;
if (count == lineCount) {
cout <<
line << endl;
break;
}
}
}
------------------------------------------------------
Output
Enter your question? What is the meaning of life?
That's a good question! You should google it.
Do you want to continue(yes/no)? yes
Enter your question? Are you looking for something?
That question has puzzled philosophers for centuries.
Do you want to continue(yes/no)? no
Thank you!!!
In c++ Write a program that allows the user to type in any one-line question and then answers tha...
nothing happens when I replace the skeleton code. the program runs but it does not give me the desired results. All the question marks must be removed and filled in with the appropriate code.// Function prototypesvoid displayAllLines(ifstream &infile);void displayLastLine(ifstream &infile);int main(){// Arrays for file nameschar fileName1[SIZE];char fileName2[SIZE];// File stream objectsifstream jokeFile;ifstream punchlineFile;// Explain the program to the user.cout << "This program will print a joke "<< "and its punch line.\n\n";// Get the joke file name.cout << "Enter the name of...
Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...
IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...
Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line is read, the program strips out all characters that are not upper or lower case letters or spaces, and then outputs the line. Thus, the program acts as a filter and issues no prompt. There are many ways this program could be written, but to receive full credit, you must observe the following: Place your code in a file called filterChars.cpp. The program should...
1. Suppose you wrote a program that reads data from cin. You are now required to reimplement it so that you can read data from a file. You are considering the following changes. I. Declare an ifstream variable in_file II. Replace all occurrences of cin with in_file III. Replace all occurrences of > > and get_line with the appropriate operations for ifstream objects What changes do you need to make? I, II, and III II and III I and III...
Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page. #include <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13 +...
Instructions: Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page. #include <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13...
Q.1. Write a C program that determines whether a line entered by a user is a palindrome or not. You must demonstrate it as an application of stack (you may use linked list implementation demonstrated in the class). Hint! Think of the basic property of a stack i.e. LIFO (list-in-first-out). Q.2. Write a charQueue header file containing all the function headers of following functions: 1- initiateQueue—to initialize a queue 2- enqueue—to add a node at the rear end of the...
Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...
// Write a program that determines how many of each type of vowel are in an entered string of 50 characters or less. // The program should prompt the user for a string. // The program should then sequence through the string character by character (till it gets to the NULL character) and count how many of each type of vowel are in the string. // Vowels: a, e, i, o, u. // Output the entered string, how many of...