1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'C, C++, C#' started by Nuz25, Nov 3, 2010.

  1. Nuz25

    Nuz25 Junior Member

    Joined:
    Aug 20, 2010
    Messages:
    129
    Likes Received:
    100
    Occupation:
    I'm a student
    #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..)
     
  2. satyawrat

    satyawrat Jr. VIP Jr. VIP

    Joined:
    Jul 8, 2009
    Messages:
    932
    Likes Received:
    1,183
    Occupation:
    Hustler
    Location:
    Gurgaon
    Home Page:
    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.
     
  3. coderjeff

    coderjeff Junior Member

    Joined:
    Nov 20, 2007
    Messages:
    130
    Likes Received:
    26
    Location:
    USA
    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.
     
  4. Nuz25

    Nuz25 Junior Member

    Joined:
    Aug 20, 2010
    Messages:
    129
    Likes Received:
    100
    Occupation:
    I'm a student
    Thanks a lot I just saw the error :)
     
  5. Nuz25

    Nuz25 Junior Member

    Joined:
    Aug 20, 2010
    Messages:
    129
    Likes Received:
    100
    Occupation:
    I'm a student

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