Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [C++] Help finishing code..STUCK (https://thetfp.com/tfp/tilted-technology/96837-c-help-finishing-code-stuck.html)

Scape 10-30-2005 05:08 PM

[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;

}

nukeu666 10-30-2005 06:06 PM

your problem looks to be here


inFile.getline(input1, 101);
inFile2.getline(input2, 101);
}

itll try getting a newline even tho eof has been reached
replace it like this
if infile.eof=false then getline
else print all of infile2 and quit
similarly for infile2

BAMF 10-31-2005 02:44 PM

The output wont be alphabetical. You may want to read everything into a container class, sort it, and then write the output file.

C++ container classes should have sort functions built in, so that step becomes 1 line.

For example,
infile1=
aa
ab
ac
infile2=
bb
bc
bd

Your output, I believe, would be
aa
bb
ab
bc
ac
bd
Since you only compare adjacent lines.

BAMF 10-31-2005 02:46 PM

Here is a link to an introduction to the C++ STL (standard template library)
http://www.topcoder.com/index?t=features&c=feat_082803

Pragma 10-31-2005 03:24 PM

What you'll probably want to use is merge-sort to organize the data. I believe the STL has containers to help with merge-sort.

Zyr 11-02-2005 02:04 AM

Yeah, you'll have to read all of the data in, then sort it. At the moment, you're just comparing adjacent lines like BAMF said.

As a general note, you'll want to be checking EOF before you start reading lines, to stop it going off the end.

Quote:

inFile.getline(input1, 101);
inFile2.getline(input2, 101);
while(!inFile.eof() || !inFile2.eof())
{
...
inFile.getline(input1, 101);
inFile2.getline(input2, 101);
}
should be

while(!inFile.eof() || !inFile2.eof())
{
inFile.getline(input1, 101);
inFile2.getline(input2, 101);
...
}
or something.

n0nsensical 11-02-2005 02:58 AM

You also have a potential buffer overflow by passing 101 as the buffer size parameter to istream::getline, which should be 100, the size of your buffers. Also, if you use code tags instead of quote tags you can include the whitespace so reading the code isn't painful.

This is almost ridiculously easy if you use STL strings and vectors as you can sort everything with one statement eg:
Code:

string s;
vector<string> v;
while (getline(inFile, s))
        v.push_back(s);
while (getline(inFile2, s))
        v.push_back(s);
sort(v.begin(), v.end());
for (unsigned i = 0; i < v.size(); ++i)
        outFile << v[i] << endl;

#include <algorithm>, <string>, and <vector> in addition.

Jinn 11-02-2005 03:43 AM

Call me a java fanboi, but thats ^^^ why I HATE C++.

Pragma 11-02-2005 05:29 AM

You Java fanboy! C++ forever! (Though to be honest, I'm not a huge fan of C++ - I'm a C/Perl man myself).


All times are GMT -8. The time now is 07:34 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


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