Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 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:
#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  
Old 10-30-2005, 06:06 PM   #2 (permalink)
Junkie
 
nukeu666's Avatar
 
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)
nukeu666 is offline  
Old 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.
BAMF is offline  
Old 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
BAMF is offline  
Old 10-31-2005, 03:24 PM   #5 (permalink)
I am Winter Born
 
Pragma's Avatar
 
Location: Alexandria, VA
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.
__________________
Eat antimatter, Posleen-boy!
Pragma is offline  
Old 11-02-2005, 02:04 AM   #6 (permalink)
Zyr
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:
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.
__________________
"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
Zyr is offline  
Old 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;
#include <algorithm>, <string>, and <vector> in addition.
n0nsensical is offline  
Old 11-02-2005, 03:43 AM   #8 (permalink)
Lover - Protector - Teacher
 
Jinn's Avatar
 
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
Jinn is offline  
Old 11-02-2005, 05:29 AM   #9 (permalink)
I am Winter Born
 
Pragma's Avatar
 
Location: Alexandria, VA
You Java fanboy! C++ forever! (Though to be honest, I'm not a huge fan of C++ - I'm a C/Perl man myself).
__________________
Eat antimatter, Posleen-boy!
Pragma is offline  
 

Tags
codestuck, finishing


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 06:43 PM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360