GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Mar-2005, 17:00
Chris In VA Chris In VA is offline
New Member
 
Join Date: Mar 2005
Posts: 2
Chris In VA is on a distinguished road

ok i got my i/o stream program running and almost done...


after lots of reading i realized i was going about the problem wrong so i started again from scratch.. i have the 2 lists of numbers merging into 1 output file just fine. i just cant seem to grasp how to sort the SOBs out. again i will c&p my code here but this time its actually worth looking at :-D

CPP / C++ / C Code:
#include<fstream>
#include<cstdlib>
#include<iostream>
#include <iomanip>

using namespace std; 

void sortnum(ifstream& fin, ifstream& fin2, ofstream& fout); 

int main() { 

  ifstream fin;
  ifstream fin2;
  ofstream fout;

   fin.open("in1.txt"); 
   if (fin.fail()) { 
      cout <<" opening failed. " << endl; 
      exit (1); 
   } 

   fin2.open("in2.txt"); 
   if (fin2.fail()) { 
      cout <<" opening failed. " << endl; 
      exit(1); 
   } 

   fout.open("out.txt"); 
   if (fout.fail()) { 
     cout <<" opening failed. " << endl; 
     exit (1); 
  } 

sortnum(fin, fin2, fout); 

fin.close(); 
fin2.close(); 
fout.close(); 

cout << "End of program.\n"; 
return 0; 
} 

void sortnum(ifstream& fin, ifstream& fin2, ofstream& fout) { 
  int data1, data2; 
  fin2 >> data2; 
  fin >> data1; 

  while (!fin.eof() && !fin2.eof()) { 
     if (data1>data2) { 
         fout << data2 << endl; 
         fin2 >> data2; 
      } 
   else 
   { 
         fout << data1 << endl; 
         fin >> data1; 
      } 
   } 
 while (!fin.eof()) { 
        fout << data1 << endl; 
        fin >> data1; 
   } 
   while (!fin2.eof()) { 
        fout << data2 << endl; 
        fin2 >> data2; 
   } 
}
Last edited by LuciWiz : 09-Mar-2005 at 23:38. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 10-Mar-2005, 01:22
twigboy twigboy is offline
New Member
 
Join Date: Feb 2005
Posts: 7
twigboy is on a distinguished road
why dont you try reading fin and fin2 into an array or vector then sort the vector and merge that into the output file. well thats what i would try and do
  #3  
Old 10-Mar-2005, 09:00
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
First, check out this information. feof() and fin.eof() work the same

Second, use code tags

Third, don't start new threads on the same topic.

Forth, it helps to explain the problem. What is the program not doing? I think I see the problem but without a description, I can't be sure. It looks like the comparison in
CPP / C++ / C Code:
while (!fin.eof() && !fin2.eof())
should be
CPP / C++ / C Code:
while (!fin.eof() || !fin2.eof())
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 4) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:35.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.