SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

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 Hide all
Teaching Since: Jul 2017
Last Sign in: 304 Weeks Ago, 3 Days Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 02 Jan 2018 My Price 10.00

prints a joke and its punch line from two different files

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

(5)
Status NEW Posted 02 Jan 2018 12:01 PM My Price 10.00

-----------  ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly

Not Rated(0)