View Single Post
Old 10-30-2005, 05:08 PM   #1 (permalink)
Scape
Insane
 
[C++] Help finishing code..STUCK

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;

}
Scape is offline  
 

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