Reading Integers from 2 text files and Merging into another text file

Hi,

I am struggling to get my program working.

The directions are to write a program that reads sorted integers from two separate files and merge the contents of the file to an output file (third file). The only standard output will be any errors to be reported and a
“FINISHED” statement after all items have been processed.

file1.txt
2
4
6
8
10

file2.txt
1
5
11
12
15

Output.txt
1
2
4
5
6
8
10
11
12
15


This is the code I have so far, but it is not working, and I have put the two txt files in the same directory as my .cpp file. It is not working though still. Basically it is outputting 12456810. Then it generates a infinite loop at 10 and never outputs 11,12, and 15. Can you please help me out to resolve this issue? I don't know how to fix this!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <fstream>

using namespace std;

int main()
{

	int num1;
	int num2;

	ifstream inputFile;
	ifstream inputFile2;
	inputFile.open("nums1.txt");
	inputFile2.open("nums2.txt");
	ofstream outputFile;
	outputFile.open("output.txt");

	inputFile >> num1;
	inputFile2 >> num2;

	while (!inputFile.eof() || !inputFile2.eof()) 
        {
                if (num1 <= num2) 
                {
                        outputFile << num1 << endl; 
                        inputFile >> num1; 
                        
                }
                else if (num1 >= num2) 
                {
                      outputFile << num2 << endl; 
                      inputFile2 >> num2; 
                }
                else if (inputFile.eof()) 
                {
                     if(!inputFile2.eof()) 
                     {
                           outputFile << num2 << endl;
                     }
                }
                else if (inputFile2.eof()) 
                {
                     if(!inputFile.eof()) 
                     {
                           outputFile << num1 << endl;
                     }
                }
		}
	
	inputFile.close();
	inputFile2.close();
	outputFile.close();

	return 0;

}
I believe it is because you don't check to see if you've reached the end of either file first. Also, you fail to get the next number from the input files if one has already reached end-of-file. Reorganizing it like this may work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//moved these two conditionals to be check first
if (inputFile.eof()) 
{
     if(!inputFile2.eof()) 
     {
           outputFile << num2 << endl;
           inputFile2 >> num2; //added this code
     }
}
else if (inputFile2.eof()) 
{
     if(!inputFile.eof()) 
     {
           outputFile << num1 << endl;
           intputFile >> num1; //added this code
     }
}
else if (num1 <= num2) 
{
        outputFile << num1 << endl; 
        inputFile >> num1; 
        
}
else if (num1 >= num2) 
{
      outputFile << num2 << endl; 
      inputFile2 >> num2; 
}                
oops... deleted my post while editing.

your while() loop is now technically functional. But when read, doesn't immediately indicate the condition you want to check.
compare these two and see which one more succinctly states what condition you're looking for:
while (!inputFile.eof() || !inputFile2.eof())

while ( !(inputFile.eof() && inputFile2.eof()) )


Topic archived. No new replies allowed.