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

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 not its punch line. The second file has the punch line as its last line, preceded by "garbage." The main function of your program should open the two files and then call two functions, passing each one the file it needs. The first function should read and display each lile in the file it is passes(the joke file). The second function should display only the last line of the file it is passed(the punch line file). It should find this line by seeking to the end of the file and then backing up to the begging of the last line. Data to test your program can be found in the joke.txt and punchline.txt files.

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.

Write a C++ program that reads and prints a joke and its

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!