Question

I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program...

I need help debugging this C++ prgram. What Am i doing wrong?

//********************************************************

// This program reads two input files whose lines are //ordered by a key data field. This program should merge //these two files, writing an output file that contains //all lines from both files ordered by the same key field.

//

//*********************************************************

#include <iostream>

#include<string>

#include<fstream>

//prototype

void mergeTwoFiles (ifstream&,ifstream&, ofstream&);

using namespace std;

int main()

{string inFile1,inFile2,outFile;

// input and output files

ifstream in1;

ifstream in2;

ofstream out;

// get two input files from user

cout<<"enter input file 1:";

cin>>inFile1;

cout<<"enter second input file";

cin>>inFile2;

cout<<"enter output file";

cin>>outFile;

//prepare files

in1.open(inFile1);

if (in2.fail())

{

cerr<< "can't open" << inFile2 << "for input.";

system("pause");

return EXIT_FAILURE;

}

out.open(outFile);

if(out.fail())

{

cerr <<" Can't open"<< outFile << "for output."<<;

system("pause");

return EXIT_FAILURE;

}

//Merge 2 files

mergeTwoFiles(in1,in2,out);

// close files

in1.close();

in2. close();

out.close();

//Pause

system("pause");

return 0;

}

void mergeTwoFiles(ifstream &in1, ifstream &in2, ofstream &out)

{

string str1,str2;

//read data

getline(in1, str1);

getline(in2, str2);

//repeat loop

while((!in1.eof()) && (!in2.eof()))

{

//if the line from file 1 is smaller

if(str1<=str2)

out<< str1 << endl;

//read a new line

getline(in1,str1);

}

//if the line from file 2 is smaller than file 1

else

{

//write file 2's line to the output

out<< str2 << endl;

getline(in2, str2);

}

}

// write remaining lines from file 2

while(!in1.eof())

{

out<< str2 <<endl;

getline(in2, str2);

}

if(str2 !="")

out<< str2 <<endl;

//end of merge function

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me commment...

//********************************************************

// This program reads two input files whose lines are //ordered by a key data

// field. This program should merge //these two files, writing an output file

// that contains //all lines from both files ordered by the same key field.

//

//*********************************************************

#include <iostream>

#include <string>

#include <fstream>

#include <cstdlib>

// prototype

using namespace std;

void mergeTwoFiles(ifstream &, ifstream &, ofstream &);

int main() {

string inFile1, inFile2, outFile;

// input and output files

ifstream in1;

ifstream in2;

ofstream out;

// get two input files from user

cout << "enter input file 1: ";

cin >> inFile1;

cout << "enter second input file: ";

cin >> inFile2;

cout << "enter output file: ";

cin >> outFile;

// prepare files

in1.open(inFile1.c_str());

in2.open(inFile2.c_str());

if (in1.fail()) {

cerr << "can't open" << inFile1 << "for input.";

system("pause");

return EXIT_FAILURE;

}

if (in2.fail()) {

cerr << "can't open" << inFile2 << "for input.";

system("pause");

return EXIT_FAILURE;

}

out.open(outFile.c_str());

if (out.fail()) {

cerr << " Can't open" << outFile << "for output." << endl;

system("pause");

return EXIT_FAILURE;

}

// Merge 2 files

mergeTwoFiles(in1, in2, out);

// close files

in1.close();

in2.close();

out.close();

// Pause

system("pause");

return 0;

}

void mergeTwoFiles(ifstream &in1, ifstream &in2, ofstream &out) {

string str1, str2;

// read data

getline(in1, str1);

getline(in2, str2);

// repeat loop

while ((!in1.eof()) && (!in2.eof())) {

// if the line from file 1 is smaller

if (str1 <= str2) {

out << str1 << endl;

// read a new line

getline(in1, str1);

}

// if the line from file 2 is smaller than file 1

else {

// write file 2's line to the output

out << str2 << endl;

getline(in2, str2);

}

}

// write remaining lines from file 2

while (!in1.eof()) {

out << str1 << endl;

getline(in1, str1);

}

// write remaining lines from file 2

while (!in2.eof()) {

out << str2 << endl;

getline(in2, str2);

}

// end of merge function

}

Add a comment
Know the answer?
Add Answer to:
I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program...
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
  • Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for...

    Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for writing and reading from console #include <fstream> //this one to deal with files, read and write to text files using namespace std; int main() { //first thing we need to read the data in the text file //let's assume we have the reading in a text file called data.txt //so, we need a stream input variable to hold this data ifstream infile; //now we...

  • Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and...

    Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and write the output file; the output file should be the same as the input file, except that it has no e’s. For example, if the input file had the line. Streets meet in the deep. Then the output file would have...

  • C++ Programming question For this exercise, you will receive two string inputs, which are the names...

    C++ Programming question For this exercise, you will receive two string inputs, which are the names of the text files. You need to check if the input file exists. If input file doesn't exist, print out "Input file does not exist." You are required to create 4 functions: void removeSpace (ifstream &inStream, ofstream &outStream): removes white space from a text file and write to a new file. If write is successful, print out "Text with no white space is successfully...

  • // Retire.cpp : Defines the entry point for the console application. // program to convery years...

    // Retire.cpp : Defines the entry point for the console application. // program to convery years to retire // sumin Kang // 9/26/17 #include "stdafx.h" #include <iostream> #include <iomanip> #include <string> #include <fstream> using namespace std; int main() { //declare files ifstream inFile; ofstream outFile; // declare constants and variables string name; int age; int ageInRetire; // open files inFile.open("D:\\retire"); outFile.open("D:\\retire"); // get input from user cout << "What is your name?"; getline(inFile, name); cout << "How old are you?";...

  • In c++ please How do I get my printVector function to actually print the vector out?...

    In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){    ifile.open(filename.c_str(), ios::in);    if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); }    } void...

  • i have two issues that i need help. 1- that in case three and two only...

    i have two issues that i need help. 1- that in case three and two only one line is being read from the file 2- my case 4 is not working correctly as it should as the output is only " Enter 1 for Trump, 2 for Warren:" #include <iostream> #include <cctype> // For the letter checking functions #include <fstream> // For file input #include <iomanip> // For setw #include <ctime> #include <cstdlib> // For exit and abs #include <errno.h>...

  • I need help with using the infile and outfile operations in C++... Here's my assignment requirements:...

    I need help with using the infile and outfile operations in C++... Here's my assignment requirements: Assignment: Cindy uses the services of a brokerage firm to buy and sell stocks. The firm charges 1.5% service charges on the total amount for each transaction, buy or sell. When Cindy sells stocks, she would like to know if she gained or lost on a particular investment. Write a program that allows Cindy to read input from a file called Stock.txt: 1. The...

  • C++: Need help debugging my code I am writing this and using some other examples as reference code while rewriting it with my own understanding of the material but am having trouble making it finally...

    C++: Need help debugging my code I am writing this and using some other examples as reference code while rewriting it with my own understanding of the material but am having trouble making it finally compile. Below is a picture of the error messages. //main.cpp //Semester Project //Created by J---on 5/6/2019 #include <iostream> #include <fstream> #include <string> #include <sstream> #include <bits/stdc++.h> using namespace std; void instructions(); //displays program details and instructions void openFile(); void takeInput(int*); void switchBoard(int*); struct price {...

  • C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one...

    C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one file, and output the result to another file. But i am confused about how to remove whitespace. i need to make a function to do it. It should use string, anyone can help me ? here is my code. #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> using namespace std; void IsFileName(string filename); int main(int argc, char const *argv[]) { string inputfileName = argv[1];...

  • I am having trouble trying to output my file Lab13.txt. It will say that everything is...

    I am having trouble trying to output my file Lab13.txt. It will say that everything is correct but won't output what is in the file. Please Help Write a program that will input data from the file Lab13.txt(downloadable file); a name, telephone number, and email address. Store the data in a simple local array to the main module, then sort the array by the names. You should have several functions that pass data by reference. Hint: make your array large...

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