The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 2 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
I need help with this problem:
Â
"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."
Â
When debugging in Visual Studio, I get the following error:
Â
"Run-Time Check Failure #2 - Stack around the variable 'line' was corrupted."
Â
PLEASE HELP ME!
Â
I have included the joke.txt and punchline.txt for your use, as well as my code as follows:
Â
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstring>
Â
using namespace std;
Â
void functionJoke(fstream & infile);
void functionPunchLine(fstream & infile);
Â
Â
int main()
{
fstream jokeFile, punchLineFile;
Â
//open joke
jokeFile.open("joke.txt", ios::in);
//if joke file does not exist
if (jokeFile.fail())
{
cout << "Error opening joke filenn";
}
Â
//print joke
functionJoke(jokeFile);
jokeFile.close();
cout << "nnn";
Â
//open punch line
punchLineFile.open("punchline.txt", ios::in);
//if punch line does not exist
if (punchLineFile.fail())
{
cout << "Error opening punch line filenn";
}
Â
//print last line of file
functionPunchLine(punchLineFile);
//close
punchLineFile.close();
Â
system("pause");
Â
  return 0;
}
Â
//functionJoke
void functionJoke(fstream & infile)
{
//declare char
char ch;
//read char
infile.get(ch);
while (!infile.fail())
{
//if file is opened then print every char in file
cout << ch;
infile.get(ch);
}
Â
cout << "n";
}
Â
//implement punchLine function
void functionPunchLine(fstream & infile)
{
//declare variable
int i = 3;
char ch;
char line[30];
Â
//move to end
infile.seekg(0L, ios::end);
Â
//back three positions left
infile.seekg(-3L, ios::end);
Â
//get character
ch = infile.get();
Â
//until new line arrived
while (ch != 'n')
{
infile.seekg(-i, ios::end);
ch = infile.get();
i++;
}
Â
infile.getline(line, i);
cout << line;
cout << endl;
}
Â
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.
Attachments:
-----------