Question

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 in the file it is passed (the joke file). The second functionshould 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 thebeginning of the last line. Data to test program can be found in the joke.txt and punchline.txt files.


+++++++++++++++++ joke.txt++++++++++++++++++++++++++++++++++++++=
Two men who work together in a facory 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.

*****************************************************************************************
1 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void readJoke()
{
string line;
ifstream joke ("joke.txt");
if (joke.is_open())
{
while (joke.good())
{
getline (joke,line);
cout << line << endl;
}
joke.close();
}
}

void punchline()
{
string lastline,line;
ifstream punch ("punchline.txt");
if (punch.is_open())
{
while (punch.good()){
lastline=line;
getline (punch,line);}

cout << lastline << endl;
punch.close();
}
}


int main ()
{
readJoke();
punchline();

return 0;
}
Add a comment
Answer #2

Dear,


/**
*This program takes two files jokes.txt and punch.txt and uses two funtions
*and read the jokes.txt and prints and read punch.txt to end of the file
*and trace back and display only the last line
*/
//Header files
#include<iostream>
#include<fstream>
using namespace std;
//funtion Protypes
void jokeFile(fstream &);
void punchFile(fstream &);
//Main funtion starts
int main()
{
//create a two file streams
fstream fin1,fin2;
//open jokes.txt in read mode
fin1.open("jokes.txt",ios::in);
if(!fin1)
{
cout<<"File not found "<
system("pause");
exit(1);
}
while(!fin1.eof())
{
//Passing the file stream to funtion
jokeFile(fin1);
}
//close the file
fin1.close();
fin2.open("punch.txt",ios::in);
while(!fin2.eof())
{
//Passing the file stream to funtion
punchFile(fin2);
}
//close the file
fin2.close();
//To pause the output
system("pause");
return 0;
}
//End of main
//jokeFile futnion definition with file stream address
void jokeFile(fstream &file)
{
cout<<"Joke File Data"<
cout<<"-------------------------------------------------"<<
while(!file.eof())
{
char line[20];
file.getline(line,21,'n');
cout<<
}
}
//punchFile futnion definition with file stream address
void punchFile(fstream &file)
{
//Variable declaration
int i=3;
char ch;
char line[30];
//Move to end
file.seekg(0L,ios::end);
//back 3 positions left
file.seekg(-3L,ios::end);
//Get character
ch=file.get();
cout<<"Punch File Data"<
cout<<"---------Last Line of Punch File ----------"<<

//Until until new line arrived
while(ch!='n')
{
file.seekg(-i,ios::end);
ch=file.get();
i++;
}
file.getline(line,i);
cout<<<
}




input files

jokes.txt file

uploaded image
punch.txt file

uploaded image


output

uploaded image

Note:I have taken my own joke file and punch files.

Hope this would helpful to you..

answered by: sydnee
Add a comment
Know the answer?
Add Answer to:
I need to write a C++ program using DEV C++ that reads and prints a joke and its punch line from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 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 prototypesvoid displayAllLines(ifstream &infile);void displayLastLine(ifstream &infile);int main(){// Arrays for file nameschar fileName1[SIZE];char fileName2[SIZE];// File stream objectsifstream 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...

  • 1. Please provide code based on Chapter 12 only from the text. Also include output to...

    1. Please provide code based on Chapter 12 only from the text. Also include output to console picture after the program is compiled. 2. Text is Tony Gaddis 9th edition C++. Chapter 12, page 714, Problem 5 'Line Numbers' 3. Please use 'forChapt12.txt' file for the problem. The text is pasted below after description of the problem. 5. LINE NUMBERS (page 714) Write a program that asks the user for the name of the file. The program should display the...

  • Write a C++ program that reads text from a file and encrypts the file by adding...

    Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...

  • C++ please Write a program that reads the following sentences from a file: I am Sam...

    C++ please Write a program that reads the following sentences from a file: I am Sam Sam I am That Sam I am That Sam I am I do not like that Sam I am Do you like green eggs and ham I do not like them But I do like spam! You will first have to create the text file containing the input, separate from your program. Your program should produce two output files: (i) (ii) one with the...

  • Lab #10 C++ Write a C++ program that reads text from a file and encrypts the...

    Lab #10 C++ Write a C++ program that reads text from a file and encrypts the file by adding an encryption factor (EF) to the ASCII value of each character. The encryption factor is 1 for the first line and increases by 1 for each line up to 4 and then starts over at 1. So, for the 4 th line the EF is 4, for the 5th line it is 1, for the 10th line it is 2. In...

  • I/O program for C Write a program in direct1.c which reads from standard input a line...

    I/O program for C Write a program in direct1.c which reads from standard input a line and then outputs that line immediately to standard output. it should read the first line only ! Then, write another program called direct2.c which reads from standard input every line until end of input and outputs them to standard output. Everything that goes in should come out exactly as it was. Compile both programs and run them on the input files given below The...

  • In c programming, I need to write a program that reverses each of the lines in...

    In c programming, I need to write a program that reverses each of the lines in a file. The program accepts two command line arguments: The first one is the name of the input file The second is the name of the output file If the user didn't give two filenames, display an error message and exit. The program reads in each line of the input file and writes each line in reverse to the output file. Note, the lines...

  • I need to write a C++ program that reads two integers from a data filed called...

    I need to write a C++ program that reads two integers from a data filed called "integers.dat" The first integer should be low and the other one high. The program should loop through all integers between the low and high values and include the low and high integers and then display Display the integers in the file Displays the number of integers divisible by 5 OR 6, but not both. Displays the sum of the integers from condition 2 as...

  • Q4) Write a C program named "hw3q4.c” that takes three string inputs from the command line...

    Q4) Write a C program named "hw3q4.c” that takes three string inputs from the command line arguments that represent file names. The first and second files contain four sorted integer numbers that represent set elements. The main process creates a child process that shares the three files. The child process determines the intersection set of two sets and saves the line: "Child process PID: xxxx Intersection of (x, x, x, x) and (y, y, y, y) (z, z, z, z)"...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

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