ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

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

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 2 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 26 Apr 2017 My Price 11.00

When debugging in Visual Studio

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. &quot;I know how to get some time off,&quot; said one. &quot;How are you going to do that?&quot; asked the other. &quot;Watch,&quot; he said, and climbed a ladder to the ceiling. The foreman asked what he was doing up there, and the man replied. &quot;I'm a lightbulb.&quot; &quot;I think you need some time off,&quot; the foreman said, and the first man walked out of the factory. After a moment, the second man followed him. &quot;Where do you think you're going?&quot; the foreman shouted.

Attachments:

Answers

(11)
Status NEW Posted 26 Apr 2017 03:04 AM My Price 11.00

-----------

Attachments

file 1493181221-Solutions file 2.docx preview (51 words )
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 -----------onl-----------ine----------- an-----------d g-----------ive----------- yo-----------u e-----------xac-----------t f-----------ile----------- an-----------d t-----------he -----------sam-----------e f-----------ile----------- is----------- al-----------so -----------sen-----------t t-----------o y-----------our----------- em-----------ail----------- th-----------at -----------is -----------reg-----------ist-----------ere-----------d o-----------n -----------THI-----------S W-----------EBS-----------ITE-----------. ----------- Th-----------ank----------- yo-----------u -----------
Not Rated(0)