Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. Punch Line Write a program that reads and prints a joke and its punch line fr

ID: 3669924 • Letter: 3

Question

3. Punch Line

Write a 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 line in the file it is passed (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 beginning of the last

line. Data to test your program can be found in the joke.txt and punchline.txt files.

c++

Explanation / Answer

#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#include<stdlib.h>

using namespace std;

// Function prototypes
void functionJoke(fstream &);
void functionPunchLine(fstream &);

int main()
{
   fstream jokeFile, punchLineFile;  

   jokeFile.open("joke.txt", ios::in );

   if (jokeFile.fail())
   { cout << " Error openeing file!! "; }

   functionJoke(jokeFile);
   jokeFile.close();

   system("PAUSE");

   cout << " ";

   punchLineFile.open("punchline.txt", ios::in);

   if (punchLineFile.fail())
   { cout << " Error Opening file!! "; }

   functionPunchLine(punchLineFile);

   punchLineFile.close();  

   return 0;
}

void functionJoke(fstream &file)
{
   char ch;
  
   file.get(ch);
   while (!file.fail())
   { cout << ch; file.get(ch); }
   cout << " ";
  
}

void functionPunchLine(fstream &file)
{
   const int LENGTH = 81;
   char input[LENGTH];

   file.getline(input, LENGTH, ' ');
   while(!file.eof())
   {
       file.getline(input, LENGTH, ' ');
   }

   cout << input << " ";  
}

*Try this any help Please comment****

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote