Help this is due soon!

I am not sure how to get the numbers to print in ascending order when I input them into numbers1.txt and numbers2.txt. I am having numbers repeat themselves among other things!

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

int main()
{
	ifstream fin1, fin2;
	ofstream fout;

	fin1.open("numbers1.txt");
	if(fin1.fail())
	{
		cout << "Input file opening failed." << endl;
		exit(1);
	}

	fin2.open("numbers2.txt");
	if(fin2.fail())
	{
		cout << "Input file opening failed." << endl;
		exit(1);
	}

	fout.open("output.txt");
	if(fout.fail())
	{
		cout << "Output file opening failed." << endl;
		exit(1);
	}

	int number1, number2;

	fin1 >> number1;
	fin2 >> number2;

	do
	{
		if(number1 == number2)
		{
			fout << number1 << endl;
			fout << number2 << endl;
			fin1 >> number1;
			fin2 >> number2;
		}
		if(number1 > number2)
		{
			fout << number2 << endl;
			fin2 >> number2;
		}
		if(number2 > number1)
		{
			fout << number1 << endl;
			fin1 >> number1;
		}
	}while(!fin1.eof() && !fin2.eof());
	
	if(fin1.eof() || fin2.eof())
	{
		if(number2 > number1)
		{
			fout << number1 << endl;
		}
		if(number1 > number2)
		{
			fout << number2 << endl;
		}
		if(number1 == number2)
		{
			fout << number2 << endl;
		}
	}
	while(!fin2.eof())
	{
		fout << number2 << endl;
		fin2 >> number2;
	}
	while(!fin1.eof())
	{
		fout << number1 << endl;
		fin1 >> number1;
	}
	
	if(fin1.eof() || fin2.eof())
	{
		if(number2 > number1)
		{
			fout << number1 << endl;
		}
		if(number1 > number2)
		{
			fout << number1 << endl;
		}
		if(number1 == number2)
		{
			fout << number1 << endl;
		}
	}	
	
	fin1.close();
	fin2.close();
	fout.close();
	
	return 0;
}
please help.....idk how to fix it....
I suggest, read file1 and file two inside a vector - std::vector<int>.

you will have text in the input files, so convert the string to numbers using atoi/atol and then push inside the vectors.

sort the vector using std::sort. Output them to the output file. You will again have to convert them to string. You can use a stringstream or sprintf function to convert number to a string.

Edit: I know this is totally different from what you are doing but that would be easy for you.
Last edited on
atoi/sprintf are totally unnecessary; the stream overloads are just fine.

While sorting vector or using multiset would produce expected output, is that the goal? Or the beef in the logic?

You seem to start well. Then you reach the point, where one or both streams are at end and the latest number from each has not been output. If they are equal, both should be written. If not, one stream still may have values and the challenge is to insert the value of the exhausted stream at correct position.

How about a function that reads from one stream and inserts "pivot value" when suitable. Then call that function with appropriate input.
Topic archived. No new replies allowed.