APlusGrades

Not Rated (0)

$15/per page/Negotiable

About APlusGrades

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing,Foreign Languages,Geography,Geology,Health & Medical,HR Management,Law,Management,Physics,Programming,Science,Statistics Hide all
Teaching Since: Jul 2017
Last Sign in: 273 Weeks Ago, 2 Days Ago
Questions Answered: 1850
Tutorials Posted: 1850

Education

  • Graduate in Biology and Nutrition, MBA Finance
    Florida State University
    Aug-2000 - Jul-2007

Experience

  • Ass. Relationship Manager
    Penn-Florida
    Mar-2009 - Feb-2016

Category > Programming Posted 17 Jul 2017 My Price 12.00

Write a program

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;

}

Attachments:

Answers

Not Rated (0)
Status NEW Posted 17 Jul 2017 05:07 PM My Price 12.00

Hel-----------lo -----------Sir-----------/Ma-----------dam----------- Â-----------  -----------Tha-----------nk -----------you----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------acq-----------uis-----------iti-----------on -----------of -----------my -----------pos-----------ted----------- so-----------lut-----------ion-----------.Pl-----------eas-----------e p-----------ing----------- me----------- on----------- ch-----------at -----------I a-----------m Â----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I

Not Rated(0)