Question

c++ program that prints joke and its punchline

nothing happens when I replace the skeleton code. the program runs but it does not give me the desired results. All the question marks must be removed and filled in with the appropriate code.

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

int main()
{
// Arrays for file names

char fileName1[SIZE];
char fileName2[SIZE];

// File stream objects
ifstream jokeFile;
ifstream punchlineFile;

// Explain the program to the user.
cout << "This program will print a joke "
<< "and its punch line.\n\n";

// Get the joke file name.
cout << "Enter the name of the file holding "
<< "the joke (joke.txt): ";
"????";

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

// Open the joke file.
"????";

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

// Open the punch line file.
"????";

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

cout << endl << endl;

// Call displayAllLines to display the
// contents of the joke file.
"????";

// Call displayLastLine to display the
// last line in the punch line file.
"????";
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)
{
char "????"; // To hold a line

// 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.
"????"(line, SIZE, '\n' );

while (!infile.eof())
{
// Display the line.
cout << line << endl;

// Read the next 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 "????")
{
char ch; // To hold a character
char line[SIZE]; // To hold a line
bool foundLastLine = false; // Flag

// 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
"????"(-1, ios::cur);
"????" = infile.get();

// Back up until we find a newline.
while (infile && !foundLastLine)
{
// If the char just read is NOT a newline...
if (ch != '\n')
{
// 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("????");
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
infile.getline(line, SIZE, '\n');
cout << line;
}

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
c++ program that prints joke and its punchline
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • The following program is in c++ ; I am trying to read in games from a...

    The following program is in c++ ; I am trying to read in games from a file and when doing so I am only reading in parts of the file. It is giving me a segmentation fault error I am going to post my code below if anyone is able to help me debug the program I would greatly appreciate it. The program is too large to be submitted as a single question on here :( --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void Business::addGamesFromFile() {...

  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

  • Code in C++: Please help me fix the error in function: void write_account(); //function to write...

    Code in C++: Please help me fix the error in function: void write_account(); //function to write record in binary file (NOTE: I able to store data to a txt file but however the data get error when I try to add on data the second time) void display_all(); //function to display all account details (NOTE: This function is to display all the info that save account.txt which is ID, Name, and Type) void modify_account(int); //function to modify record of file...

  • I need to write a C++ program using DEV C++ that reads and prints a joke and its punch line from...

    I need to write a C++ program using DEV C++ that reads and prints a joke and its punch line from two different files. The first file contains a joke, but not its punchline. The second file has the punch line as its last line, preceded by “ garbage.” The main function of the program should open the two files and then call twofunctions, passing each one the file it needs. The first function should read and display each line...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • I need to add something to this C++ program.Additionally I want it to remove 10 words...

    I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words);    int main() { // Declaring variables std::map<std::string,int> words;       //defines an input stream for the data file ifstream dataIn;    string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...

  • Write a simple telephone directory program in C++ that looks up phone numbers in a file...

    Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the...

  • Write a C++ program that uses a structure to store the following inventory information in a...

    Write a C++ program that uses a structure to store the following inventory information in a file: ⦁   Item description, Quantity on hand, Wholesale cost, Retail cost, and Date added to inventory. ⦁   Use a char array for item description and date. ⦁   The program should have a menu that allows the user to perform the following tasks: i.   Add a new record at the end of the file. ii.   Display any record in the file. iii.   Change any record...

  • I need help to fix this program it is written in c++ and needs to run...

    I need help to fix this program it is written in c++ and needs to run in visual studio. Program uses character strings from a file containing on the first line and answer key and every other line after is the students ID number followed by a space then their test answers.Program currently reads only test answer key and student ID and their answers of second line.  Sample data can be found below. TFTTFTFFFTTTFFTTFFFT HUB00123 TFTFFFTFTFTFTTTFTTTT SMU12456 FFTFTTFFTTTTTTFFTTFT #include #include #include...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT