+ 4

Homework problem regarding files in C++

Write a program that merges the numbers in two files and writes all the numbers into a third file. Each input file contains a list of numbers of type int in sorted order from the smallest to the largest. After the program is run, the output file will contain all the numbers in the two input files in one longer list in sorted order from smallest to largest. Your program should define a function that is called with the two input file streams and the output file stream as three arguments. Here's what I have so far: https://code.sololearn.com/cqoCfIMDtmCu/#cpp My problem is that when it outputs into the third file, it it only reads about 4/5 both of the input files then breaks out of the do-while loop. How can I get it to read all the way through both input files?

9th Nov 2017, 8:41 PM
Dylan
15 Respostas
+ 3
hi, lets track down your problem. are the files contain the same amount of numbers? what happens if not? hint: why not simply print each "line" so you can look into your files where it stops exactly?
9th Nov 2017, 9:12 PM
Gunther Strauss
Gunther Strauss - avatar
+ 3
There are multiple problems with your design. Line 31 you are reading from fin into num2 and you meant fin2. Line 37 your loop stops once either file hits eof. At that point, one of the files could have tons more numbers. Line 39 & 42 reads after eof on one of the files. The last number read from the files within the loop never get written.
9th Nov 2017, 9:45 PM
John Wells
John Wells - avatar
+ 2
What Gunther suggests is writing to cout the numbers you get from fin or fin2 so you know what is read. I suggest expanding on that and writing which numbers are written also so looking at the console output you can see the entire processing.
9th Nov 2017, 9:50 PM
John Wells
John Wells - avatar
+ 2
look through the posts of John! they tell you which lines wrong ^^
10th Nov 2017, 2:33 AM
Gunther Strauss
Gunther Strauss - avatar
+ 2
Ahh I see now. Thanks again people itā€™s much appreciated!
10th Nov 2017, 9:09 PM
Dylan
+ 1
You can leave the do while as is. Afterwards, you still have last two numbers read to compare plus use the while kureius sugested to process the rest of the file, but they need to test for eof. Your main loop reads last number from a file and loop exits because you're at eof so it wasn't compared or written. Like I said use cout to watch what happens and adjust code to fix issues.
10th Nov 2017, 2:38 AM
John Wells
John Wells - avatar
+ 1
yes :) but we suggested to do it TOO not instead :) so you can see what's going on in each step try this: print the number you read from the file print the decision (==, <,>) print what will put into the files this should help you to debug your programme, alternatively, just debug and step through your programme line per line
10th Nov 2017, 3:33 PM
Gunther Strauss
Gunther Strauss - avatar
+ 1
I shall, thank you.
10th Nov 2017, 10:37 PM
Dylan
0
I want the program to be universal, working for any two files as long as they have integers, the integers being separated by either a space or a new line, and having the integers in both files be in order from smallest to largest. The program should simply output the rest of the file that still has input in it if the other one runs out of input. I so sorry I don't understand your hint. To read to the end of the file I use !fin.eof(), but it doesn't seem to be working as intended.
9th Nov 2017, 9:21 PM
Dylan
0
John, How should the do-while loop be changed? What I was going for was for the do-while loop to read through both files, and if one finishes exit the loop. Then I just dump the rest of the numbers out of whatever file has any data left in it. This only works because the numbers in both files are in order from smallest to largest, so if you do get through one file, the other file doesn't need to be compared to the one that is finished so you can simply output the rest of the numbers.
10th Nov 2017, 2:22 AM
Dylan
0
Gunther, I understand your hint now. The output is quite strange at the moment. Here are the files I'm testing. input1.txt: 1 3 4 6 7 12 88 89 100 input2.txt: 0 5 8 9 10 19 55 95 101 output.txt: 0 1 3 4 5 6 7 8 9 10 12 19 55 88 89 101 I don't understand how it simply skips to 101 even though neither file has fully been read. Why is it exiting the do-while loop so early?
10th Nov 2017, 2:27 AM
Dylan
0
kurwius, I changed this in my program and it didn't change my output. Thanks though! Do you have any other suggestions?
10th Nov 2017, 2:31 AM
Dylan
0
John, If I use cout instead of fout won't that simply output the same thing but to the screen? I apologize if this is simply going over my head, I'm trying to make sense of this.
10th Nov 2017, 3:29 PM
Dylan
- 1
As Gunther stated, yes and output to both. Your program also fails if one of the files is empty or fails to open.
10th Nov 2017, 5:01 PM
John Wells
John Wells - avatar
- 1
If you get stuck, reply here.
10th Nov 2017, 9:15 PM
John Wells
John Wells - avatar