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

Hi, Please provide the source code for the following program to be written in th

ID: 3667112 • Letter: H

Question

Hi,

Please provide the source code for the following program to be written in the C++ programming language. Also, please provide good code comments that would allow even a beginner to understand what is going on in the program. I am including the requirements below and will give a thumbs up rating if the program compiles error free.

. Assignment 2 Assignment 2 Punch Line. Write a program that reads and prints a joke and its punch line from two different files (please keep it clean). 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 text" (does not matter what it is). NOTE: the last sentence (punch line) will have a newline character before it. The program is to search for the start of the last sentence; it should NOT move backward from the file a fixed number of characters. It should work for any punch line file. 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.text files provided at the textbook web site. o Program must have the following functions void displayAllLines(ifstream &infile;); //Display joke void displayLastLine(ifstream &infile;); //Display punchline - -

Explanation / Answer

// header files
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;

// Function prototypes
void displayAllLines(ifstream &infile);
void displayLastLine(ifstream &infile);

int main()
{
   string fileName1;   // To hold the 1st file name
   string fileName2;   // To hold the 2nd file name

   // File stream objects
   ifstream jokeFile;
   ifstream punchlineFile;
        
   // Explain the program to the user.
   cout << "This program will print a joke "
        << "and its punch line. ";
      
   // Get the joke file name.
   cout << "Enter the name of the file holding "
        << "the joke (joke.txt): ";
   cin >> fileName1;

   // Get the punch line file name.
   cout << "Enter the name of the file holding "
        << "the punch line (punchline.txt): ";
   cin >> fileName2;

   // Open the joke file.
   jokeFile.open(fileName1);

   // Test for errors.
   if (!jokeFile)
   {
      cout << "The file " << fileName1
           << " could not be opened.";
      exit(0);
   }

   // Open the punch line file.
   punchlineFile.open(fileName2);

   // Test for errors.
   if (!punchlineFile)
   {
      cout << "The file " << fileName2
           << " could not be opened.";
      exit(0);
   }

   cout << endl << endl;

   // Call displayAllLines to display the
   // contents of the joke file.
   displayAllLines(jokeFile);

   // Call displayLastLine to display the
   // last line in the punch line file.
   displayLastLine(punchlineFile);
   cout << endl;

   // Close the files.
   jokeFile.close();
   punchlineFile.close();

   return 0;
}

//**************************************************
// The displayAllLines function reads and displays *
// all lines in the text file whose ifstream       *
// variable is passed to the function.             *
//**************************************************
void displayAllLines(ifstream &infile)
{
   string line; // To hold a line of input

   // Just to make sure we are at the beginning
   // of the file, seek to the beginning and clear
   // any error bits.
   infile.seekg(0, ios::beg);
   infile.clear();

   // Read the first line.
   getline(infile, line, ' ' );

   while (!infile.eof())
   {
      // Display the line.
      cout << line << endl;
    
      // Read the next line.
      getline(infile, line, ' ');
   }
}

//***************************************************
// The displayLastLine displays the final line in   *
// a file. It finds 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. *
//***************************************************
void displayLastLine(ifstream &infile)
{
   char ch;                    // To hold a character
   string line;                   // To hold a line of input
   bool foundLastLine = false; // Flag, intitalized to false
     
   // Find the beginning of the last line
   // Clear the eof bit just in case we're
   // already at end of the file.
   infile.clear();

   // 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();

   // Back up until we find a newline.
   while (infile && !foundLastLine)
   {
      // If the char just read is NOT a newline...
      if (ch != ' ')
      {
         // Back up two more characters (one
         // to skip the char just read and one
         // more to move just before the previous
         // char so we can read it next.
         infile.seekg(-2, ios::cur);
         ch = infile.get();
      }
      else
      {
         // We found the newline at the end of the
         // next to last line, so we are where we
         // want to be. The next char begins the
         // final line.
         foundLastLine = true;
      }
   }

   // Read final line and display it
   getline(infile, line, ' ');
   cout << line;
}

joke.txt

Two men who work together in a factory were talking.
"I know how to get some time off," said one.
"How are you going to do that?" asked the other.
"Watch," he said, and climbed a ladder to the ceiling.
The foreman asked what he was doing up there,
and the man replied. "I'm a lightbulb."
"I think you need some time off," the foreman
said, and the first man walked out of the
factory. After a moment, the second man followed
him. "Where do you think you're going?"
the foreman shouted.

punchline.txt
asfasdfasdfasdfsdf
asdfasdfsadfsadfsadf
asdfsadfsdfsdf
"I can't work in the dark," he said.

Sample Output
                                                                                                                                                
This program will print a joke and its punch line.                                                                                                          
                                                                                                                                                            
Enter the name of the file holding the joke (joke.txt): joke.txt                                                                                            
Enter the name of the file holding the punch line (punchline.txt): punchline.txt                                                                            
                                                                                                                                                            
                                                                                                                                                            
Two men who work together in a factory were talking.                                                                                                        
"I know how to get some time off," said one.                                                                                                                
"How are you going to do that?" asked the other.                                                                                                            
"Watch," he said, and climbed a ladder to the ceiling.                                                                                                      
The foreman asked what he was doing up there,                                                                                                               
and the man replied. "I'm a lightbulb."                                                                                                                     
"I think you need some time off," the foreman                                                                                                               
said, and the first man walked out of the                                                                                                                   
factory. After a moment, the second man followed                                                                                                            
him. "Where do you think you're going?"                                                                                                                     
the foreman shouted.                                                                                                                                        
                                                                                                                                                            
"I can't work in the dark," he said.                                                                                                                        

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