Question: Write a C++ program that reads and prints a joke and its punch line from two different files. The first file contains a joke, but
To display the final line in a file, you need to find it by going to the end of the file and then
backing up a character at a time until it reaches the beginning of the last line. For example,
// Go to the end of the file. infile.seekg(0, ios::end); // Move backwards to beginning of the // final char in the file and read it in infile.seekg(-1, ios::cur); ch = infile.get();
Then use while loop to back up until we find a newline. It is where the last sentence starts.
output should be as below.
This progran will print a joke and its punch line. Enter the name of the file holding the joke (joke - txt): joke - txt Enter the nane of the file holding the punch line (punchline.txt): punchline.txt Iwo men who work together in a factory were talking . "I know how to get some tine off," said one. "How are you going to do that?" asked the other. "Watch," he said, and clinbed a ladder to the ceiling. The forenan asked what he was doing up there. and the nan replied. "I'm a lightbulb." "I think you need some time off," the forenan said, and the first man walked out of the factory. After a monent, the second man followed hin. "Where do you think you' re going?" the foreman shouted. "I work in the dark," he said. Press any key to continue
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
