-1

I am getting some errors in this below problem. The problem statement is as follows:

Write a program that reads two input files whose lines are ordered by a key data field. Your program should merge these two files, writing an output file that contains all lines from both files ordered by the key field. As an example, if two input files contain student names and grades for a particular class ordered by name, merge the information as shown below.

File 1:

Adams   C
Jones   D
King   B

File 2:

Barnes   A
Johnson   C

Output file:

Adams   C
Barnes   A
Johnson   C
Jones   D
King   B

You must read one line of a file at a time and either write it or the last line read from the other data file to the output file. A common merge algorithm is the following:

Read a line from each data file
While the end of both files has not been reached
    If the line from file 1 is smaller than the line from file 2
            Write the line from file 2 to the output file and read a new line from file 1
    Else
            Write the line from file 2 to the output file and read a new line from file 2.
Write the remaining lines (if any) from file 1 to the output file.
Write the remaining lines (if any) from file 2 to the output file.

 

#include <fstream.h>
#include <iostream.h>

using namespace std;

void merge() {
  ifstream ifile1("input1.txt");
  ifstream ifile2("input2.txt");
  ofstream ofile("output.txt");
  std::string temp1;
  std::string temp2;

  ifile1.getline(temp1, 100);
  ifile2.getline(temp2, 100);

  while (ifile1 != EOF AND ifile2 != eof) {
    while (temp1[i++] != "\n")
      ;
    while (temp2[j++] != "\n")
      ;
    if (i < j) {
      ofile << temp2;
      ifile1.getline(temp1, 100);
    } else {
      ofile << temp1;
      ifile2.getline(temp2, 100);
    }
  }
  if (ifile1 != eof) {
    ifile1.getline(temp1, 100);
    ofile << temp1;
  }
  if (ifile2 != eof) {
    ifile2.getline(temp1, 100);
    ofile << temp1;
  }
}

int main() {
  merge();
}

These are the errors I am getting

C:\Users\Ndekwu\Downloads\merge.cpp||In function 'void merge()':|
C:\Users\Ndekwu\Downloads\merge.cpp|5|error: 'ifstream' was not declared in this scope|
C:\Users\Ndekwu\Downloads\merge.cpp|5|note: suggested alternative:|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\iosfwd|159|note: 'std::ifstream'|
C:\Users\Ndekwu\Downloads\merge.cpp|5|error: expected ';' before 'ifile1'|
C:\Users\Ndekwu\Downloads\merge.cpp|6|error: expected ';' before 'ifile2'|
C:\Users\Ndekwu\Downloads\merge.cpp|7|error: 'ofstream' was not declared in this scope|
C:\Users\Ndekwu\Downloads\merge.cpp|7|note: suggested alternative:|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\iosfwd|162|note: 'std::ofstream'|
C:\Users\Ndekwu\Downloads\merge.cpp|7|error: expected ';' before 'ofile'|
C:\Users\Ndekwu\Downloads\merge.cpp|11|error: 'ifile1' was not declared in this scope|
C:\Users\Ndekwu\Downloads\merge.cpp|12|error: 'ifile2' was not declared in this scope|
C:\Users\Ndekwu\Downloads\merge.cpp|14|error: 'eof' was not declared in this scope|
C:\Users\Ndekwu\Downloads\merge.cpp|14|error: expected ')' before 'AND'|

Am I using the wrong header file here?

Benjamin Bannier
  • 55,163
  • 11
  • 60
  • 80
user3499617
  • 3
  • 1
  • 4
  • What have you tried? The errors are you giving you suggestions, why not correct them? Additionally you're programming in C++, but using C-style headers. You're also alternating in explicitly using and not using "std". The generally accepted best practice is to always use "std::" and not use "using namespace std;". – Eugene K Apr 18 '14 at 19:23

1 Answers1

2

Here are some issues I found:

Semicolon after while
The line:

while(temp1[i++]!="\n");  
                       ^This could be a problem.

Has no function. The ';' terminates the while statement; there is no loop content. As it stands, it looks like a variable delay loop, which depends on the first character that is not a line ending.

Reading until EOF
Using the eof function will cause your last read to be processed twice because the EOF is not detected until after a read operation.

Change to something like: while (getline(input_file, text))

Consistency with std
You have using namespace std; for ifstream and ofstream, yet you use std::string. You should be consistent, and use std::ifstream or remove std from std::string.

Also, don't use using namespace std; as it is too broad. Specifically list the std namespace items you are using:

using std::ifstream;
using std::string;
using std::cout;

The main function
Regardless of how the compiler accepts other variations, the void main() is wrong because main() returns an int to the operating system.

Using standard include files correctly
The header files should be <fstream> and <iostream>, without the .h extension.
Also review that you are using a C++ compiler and not a C language compiler. Verify that the compiler is using the C++ language. For example, the C language does not have fstream header files.

Don't use magic numbers
A magic number is a numeric constant in an expression or parameter to a function, that has no comment or description. Use either #define or const unsigned int with a descriptive identifier name.

By naming the constant, you will only have to make one change when the constant changes. Look at your program. If you change the constant 100 to 150, how many changes will you need to perform?

Use records
Use a structure to describe a record of your input file. Each column would be a data member in the structure.

The structure will allow you to easily compare two records by a key field.

Use std::vector
Don't use arrays. Use a std::vector<Record> instead. The vector automatically adjusts during run-time. With an array, you have to monitor size and reallocate if the capacity is exceeded.

There is no comparing of records
The requirements say to read in the data and to compare by the key field. You are not comparing any key fields.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154