I need help with this program. I cannot get the joke to display. Thanks! Write a
ID: 3621899 • Letter: I
Question
I need help with this program. I cannot get the joke to display. Thanks!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.
This is what I have so far...
//This program reads and prints a joke and its punchline from two different files.
#include <fstream>
#include <iostream>
#include<string>
using namespace std;
// Function prototypes
void displayJoke(ifstream &inputFile);
void displayPunchline(ifstream &inputFile);
int main()
{
string file1, //First File
file2; //Second File
ifstream joke, //Input File Joke
punchline; //Input File Punchline
cout<<"Enter the name of the file with the joke:"<<endl; //Asks user to enter the file name with the joke
cin>>file1;
cout<<"Enter the name of the file with the punchline:"<<endl; //Asks user to enter the file name with the punchline
cin>>file2;
joke.open(file1.data()); //Open joke file
if (!joke)
{
cout<< "The file "<<file1<< " could not be opened."; //Displays if file is not found
exit (0);
}
punchline.open(file2.data()); //Open punchline
if (!punchline)
{
cout<< "The file "<<file2<< " could not be opened."; //Displays if file is not found
exit (0);
}
displayJoke(joke); //Displays joke
displayPunchline(punchline); //Displays punchline
joke.close(); //Close joke file
punchline.close(); //Close punchline file
return 0;
}
void displayJoke(ifstream &inputFile)
{
string line;
inputFile.seekg(ios::beg);
getline(inputFile,line);
inputFile>>line;
cout<<line<<endl;
}
void displayPunchline(ifstream &inputFile)
{
string line;
inputFile.seekg(-35L,ios::end);
getline(inputFile, line);
inputFile>>line;
cout<<line<<endl;
}