Hello, I'm supposed to write a programming project that reads 2 files and then organizes the numbers into an output file in order from smallest to largest.
The input files are as they appear:
1
4
5
and also
2
3
6
Right now I'm having trouble with the output and my monitor only shows the numbers 1-4.
My code is:
If anyone can help that'd be great, thank!
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
void combine_file(ifstream& infile1, ifstream& infile2, ofstream& outfile);
//Combines intergers from both instream files and sorts them
//infile1 is the first list of integers
//infile2 is the second list of integers
//outfile lists all integers from both instream files in ascending order
int main()
{
ifstream fin, fin2;
ofstream fout;
fin.open("input1.txt");
if (fin.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
fin2.open("input2.txt");
if (fin.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
fout.open("output.txt");
if (fout.fail())
{
cout << "Output file opening failed.\n";
exit(1);
}
combine_file(fin, fin2, fout);
fin.close();
fin2.close();
fout.close();
system("PAUSE");
return 0;
}
void combine_file(ifstream& infile1, ifstream& infile2, ofstream& outfile)
{
int c, d, store;
infile1 >> c;
infile2 >> d;
while((!infile1.eof()) && (!infile2.eof()))
{
if (c < d)
store = 1;
else if (d < c)
store = 2;
else if (d == c)
store = 3;
switch (store)
{
case (1):
{
cout << c << " ";
infile1 >> c;
outfile << c << " ";
break;
}
case (2):
{
cout << d << " ";
infile2 >> d;
outfile << d << " ";
break;
}
case (3):
{
cout << c << " " << d << " ";
outfile << c << " " << d << " ";
infile1 >> c;
infile2 >> d;
break;
}
default:
cout << "Error";
}
}
while(!infile1.eof())
{
cout << c << " ";
outfile << c << " ";
infile1 >> c;
break;
}
while(!infile2.eof())
{
cout << d << " ";
outfile << d << " ";
infile2 >> d;
break;
}
cout << endl;
}