Thread: Comparing two lists

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    8

    Comparing two lists

    Hi,

    I've got a serious problem. I'm supposed to compare two lists with eachother and it should output wich words can be found in both lists.

    I've come up with abit how to do this, but im stuck! I'm thinking that you first count the number of letters in the first word, then compare it to the other word in the other list, here is a problem; how do i compare the first word in list #1 to the second word in list #2.

    The lists;
    List1;
    Apple
    Saussage
    Dingle

    List2;
    Claw
    Banana
    Saussage

    How can i make this simple and nice? Im really stuck.

    Here is what i got now;

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    int counterone[250], countertwo[250], count, counttwo;
    char list_a[250], list_b[250];
    int main(int argc, char *argv[])
    {
        ifstream list1 ("lista1.txt");
        ifstream list2 ("lista2.txt");
                 list1>>list_a;
                 list2>>list_b;
        
        if (list1.is_open())
            {
            cout<<"File 'list1' is open.\n";
            cin.get();
            
            }
        else
            {
            cout<<"File 'list1' could not be opened.\n";
            cin.get();
            return 0;
            }
        
        
        if (list2.is_open())
           {
           cout<<"File 'list2' is open.\n";
           cin.get();
           }
        else
            {
            cout<<"File 'list2' could not be opened.\n";
            cin.get();
            return 0;
            }
        
        cout<<list_a;
        
        
        
        cin.get();
        return 0;
    }
    Wich really is NOTHING with what i need to do. I dont want anyone to do this for me, but i need a push in the right direction, how to get started and maybe a hint to how-to.

    Thanks alot.

  2. #2
    Registered User SpaceCadet's Avatar
    Join Date
    Oct 2006
    Posts
    23
    I suggest you tokenise the char buffer that you populate from parsing the file into strings that represent words.

    Insert these words into a std::vector container or std::list container (Check which is most applicable for your needs).

    You can then use the container API to make comparison between the contents of the two containers.

    I suggest you investigate the use of iterators to traverse your two containers.
    All questions are easy. The answers cause the problem...

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Hi, sorry i havent replyed yet SpaceCadet.
    Uhm, about what you said doesnt really tell me alot... Seems to be abit high level for me, only read C-Programming for a few months. Could you explain it abit more deep or maybe help me in another direction.

    I chose to wait with this assignment becouse i felt it was abit high-level. I still want to get it completed tho, for personal gain and development.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    char list_a[250], list_b[250];
    
    ...
    
    list1>>list_a;
    list2>>list_b;
    That doesn't do what you want it to. It only will read the first "word" from the files and store it into the respective arrays. What you need are two arrays of strings or a couple vector<string> containers to store the words read from both files. If you go the array route, you'll need a counter to track how many values you read into each array.



    Code:
    ifstream list1 ("lista1.txt");
    ifstream list2 ("lista2.txt");
    list1>>list_a;
    list2>>list_b;
        
    if (list1.is_open())
    
    ...
    
    if (list2.is_open())
    
    ...
    You should test if your files are open before you try to read from them, not after.


    Regardless of whether you choose an array or vector implementation, after you've read the words from both files into two seperate containers you now need to figure out which words are in both. This can easily be done by using the STL set_intersection function declared in the <algorithm> header. To use that function you must first sort the two containers which can be accomplished by a quick call to the STL sort function (also conveniently declared in the <algorithm> header).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding comparing two linked lists
    By cyberfish in forum C++ Programming
    Replies: 2
    Last Post: 08-23-2007, 04:28 AM
  2. Help creating lists of pointers
    By The_Kingpin in forum C Programming
    Replies: 2
    Last Post: 12-11-2004, 08:10 PM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  4. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM