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!
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:
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())