-
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.
-
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.
-
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.
-
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).