10-30-2005, 05:08 PM | #1 (permalink) | |
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:
|
|
10-30-2005, 06:06 PM | #2 (permalink) |
Junkie
Location: India
|
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
__________________
Why did the Comp. Engineer get X-mas and Halloween mixed up? Because Oct(31) == Dec(25) |
10-31-2005, 02:44 PM | #3 (permalink) |
Crazy
|
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. |
10-31-2005, 02:46 PM | #4 (permalink) |
Crazy
|
Here is a link to an introduction to the C++ STL (standard template library)
http://www.topcoder.com/index?t=features&c=feat_082803 |
11-02-2005, 02:04 AM | #6 (permalink) | |
Crazy
Location: Hamilton, NZ
|
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:
while(!inFile.eof() || !inFile2.eof()) { inFile.getline(input1, 101); inFile2.getline(input2, 101); ... } or something.
__________________
"Oh, irony! Oh, no, no, we don't get that here. See, uh, people ski topless here while smoking dope, so irony's not really a high priority. We haven't had any irony here since about, uh, '83 when I was the only practitioner of it, and I stopped because I was tired of being stared at." Omnia mutantu, nos et mutamur in illis. All things change, and we change with them. - Neil Gaiman, Marvel 1602 |
|
11-02-2005, 02:58 AM | #7 (permalink) |
Junkie
Location: San Francisco
|
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; |
11-02-2005, 03:43 AM | #8 (permalink) |
Lover - Protector - Teacher
Location: Seattle, WA
|
Call me a java fanboi, but thats ^^^ why I HATE C++.
__________________
"I'm typing on a computer of science, which is being sent by science wires to a little science server where you can access it. I'm not typing on a computer of philosophy or religion or whatever other thing you think can be used to understand the universe because they're a poor substitute in the role of understanding the universe which exists independent from ourselves." - Willravel |
Tags |
codestuck, finishing |
|
|