Anyone Seeing an Error In This C++ Program??

Nuz25

Junior Member
Joined
Aug 20, 2010
Messages
129
Reaction score
100
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
ifstream readFile1;
ifstream readFile2;
ofstream writeFile;


string temp, temp2;
bool duplicate=false;

readFile1.open("list1.txt");
readFile2.open("list2.txt");
writeFile.open("formated_list.txt");


if(readFile1.fail()||readFile2.fail())

cout<<endl<<"There was a problem opening your file"<<endl;


else
{

while(!(readFile1.eof()))
{


getline(readFile1,temp);

while(!readFile2.eof()&&!duplicate)
{
getline(readFile2,temp2);

if(temp==temp2)
{

duplicate=true;

}

}

if(!duplicate)
writeFile<<temp<<endl;

duplicate=false;

}

}

readFile1.close();
readFile2.close();
writeFile.close();

system("pause");

return 0;

}


This program is supposed to read list1 and list2 and rewrite list1 in formated list without the element that were included in list2 and list1.

I run with no errors but it's not removing the duplicates... (I'm a novice programmer and I'm not even sure you can read 2 files at a time..)
 
Nuz25,
I have one advice for you.. You are usign C++. DO you know why C++ is famous? Because it allows low level functions with OOP features.

Now looking at your cde, its all haphazard as all procedural codes are. SO my advice is to use the class and objects and use OOp to make your programs reusable and comprehend able.
 
It looks like you read the first line from list1, then compare it to every line in list2. When you read the second line from list1 you have already reached the end of list2 and it is at the end of file so those records are never read again.

Simple fix is to close and reopen list2 each time, although that's doing a lot of file reading.

Better fix would be to read the lines from list2 into an array and then compare each line from list1 to see if it is in the array rather than reading list2 over and over again.
 
It looks like you read the first line from list1, then compare it to every line in list2. When you read the second line from list1 you have already reached the end of list2 and it is at the end of file so those records are never read again.

Simple fix is to close and reopen list2 each time, although that's doing a lot of file reading.

Better fix would be to read the lines from list2 into an array and then compare each line from list1 to see if it is in the array rather than reading list2 over and over again.

Thanks a lot I just saw the error :)
 
Nuz25,
I have one advice for you.. You are usign C++. DO you know why C++ is famous? Because it allows low level functions with OOP features.

Now looking at your cde, its all haphazard as all procedural codes are. SO my advice is to use the class and objects and use OOp to make your programs reusable and comprehend able.


You're right for sure, but I'm learning procedural programming right now and my object oriented class is next winter.
 
Back
Top