Hi, I have this program it goes through 2 input files then outputs their contents into another file and the outputs are saposta be sorted in alphabetical order. The probelm is one file is shorter than the other and as my program is now it sorts just fine untill it hits the eof of one of the files before the other then its messed up. Heres my code so far
Quote:
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
char input1[100];
char input2[100];
char inputFile1[50];
char inputFile2[50];
char outputFile[50];
ifstream inFile;
ifstream inFile2;
ofstream outFile;
cout << "Please enter the name of the first input file: ";
cin >> inputFile1;
inFile.open(inputFile1);
if(inFile.fail())
{
while(inFile.fail())
{
inFile.clear();
cout << "File not found try again: ";
cin >> inputFile1;
inFile.open(inputFile1);
}
}
cout << "Please enter the name of the second input file: ";
cin >> inputFile2;
inFile2.open(inputFile2);
if(inFile2.fail())
{
while(inFile2.fail())
{
inFile2.clear();
cout << "File not found try again: ";
cin >> inputFile2;
inFile2.open(inputFile2);
}
}
cout << "Please enter the name of the output file: ";
cin >> outputFile;
outFile.open(outputFile);
inFile.getline(input1, 101);
inFile2.getline(input2, 101);
while(!inFile.eof() || !inFile2.eof())
{
if(strcmp(input1,input2) > 0)
{
outFile << input2 << endl;
outFile << input1 << endl;
}
else if(strcmp(input1,input2) < 0)
{
outFile << input1 << endl;
outFile << input2 << endl;
}
inFile.getline(input1, 101);
inFile2.getline(input2, 101);
}
inFile.close();
inFile2.close();
cout << "Done\n";
return 0;
}
|