Hi everyone, i am doing my new assignment and am having trouble understanding what this program wants me to do.
Q3- Define a structure type to represent a word list. The structure will contain one string
component for the language of the words (e.g., English, Japanese, Spanish), an integer component
that keeps track of how many words are in the list, and an array of MAX_WORDS 20-character
strings to hold the words.
Define the following functions to work with word lists:
a. load_word_list - takes as parameters the name of an input file and a wordlist structure to be
filled.
b. add_word - takes as parameters a word and a wordlist structure to modify. If the wordlist is
already full, it displays the message “List full, word not added.” If the word is already in the
list, it leaves the structure unchanged. Otherwise, it adds the word to the list and updates the list
size. Do not bother keeping the list in order.
c. contains - takes as parameters a word and a wordlist. If the word matches one of the wordlist
entries, the function returns true, otherwise false.
d. equal_lists - takes two wordlists as parameters and returns true if the lists are in the same
language, have the same number of elements, and every element of one list is found in the
other. (Hint: call contains repeatedly.)
e. display_word_list -displays all the words of its wordlist structure parameter in four columns.
Write a program that fills a wordlist from a data file. Then prompt the user to enter a language and
12 words to add to a different list. Then ask the user to enter some words to search for in the first
list using contains, and print a message indicating whether each is found. Use equal_lists to
compare the two lists, printing an appropriate message. Finally, use display_word_list to output
each list.
Write a program that fills a wordlist from a data file. Then prompt the user to enter a language and
12 words to add to a different list. Then ask the user to enter some words to search for in the first
list using contains, and print a message indicating whether each is found. Use equal_lists to
compare the two lists, printing an appropriate message. Finally, use display_word_list to output
each list.
I understand i have to have a list of words, but when it says "prompt the user to enter a language and 12 words" i dont understand what i would do with it
Write a program that fills a wordlist from a data file.
First you create a list (for the program), you use the function void load_word_list(ifstream, WordList); to load a word list from a file into that list. So this part says you need a file with words in it already made and you need to read all those words into a list.
Then prompt the user to enter a language and 12 words to add to a different list.
Create another wordlist (for the user) and prompt the user to enter a language, then they should enter words into that list. This is where you will use the void add_word(string, WordList); function.
Then ask the user to enter some words to search for in the first
list using contains, and print a message indicating whether each is found.
You will then ask the user to enter a word to search for in the program list and use the function bool contains(string, WordList); to do that.
What is the purpose of prompting them to enter a language? The reason im having difficulty is we didn't cover much on lists, so i'm not too sure on how to get the data from a file and put it into a list. How do i go about doing so?
What is the purpose of prompting them to enter a language? The reason im having difficulty is we didn't cover much on lists, so i'm not too sure on how to get the data from a file and put it into a list. How do i go about doing so?
Not sure what the purpose is. About that, ask your prof
First you have to create a new list like I did in the first post then using a for-loop, read the file's contents into the array. Remember that this has to be in your load_word_list() function
thanks for the help! i got most done! one spot i am having trouble with now is in 'add_word - takes as parameters a word and a wordlist structure to modify. If the wordlist is
already full, it displays the message “List full, word not added.”'
I've tried looping, sizeof, and checking when it is not null, but i cannot figure out how to check if the array is full or not. How can i do this?
EDIT:
i tried this and it seems to work, any better ways?
You have a variable in your class called HowManyWords and this should be incremented each time you add a new word so all you have to do when you want to add a new word is to first check that HowManyWords is less than MAX_WORDS. If it is not, add the word and increment HowManyWords. So in your add_word() function, you should have something like this as the first thing in the function:
1 2 3 4 5
if (HowManyWords >= MAX_WORDS)
{
cout <<"List is full\n" << endl;
return;
}
One thing I forgot to do in the initial code is to create a constructor for the struct. So in public, add this constructor:
WordList();
Then somewhere, define this function:
WordList::WordList(){HowManyWords = 0;}
Reason for this is so that HowManyWords is set to a value before using it so that you don't encounter a seg fault.
Now back to your question, what you are doing looks right, but you don't have to use myList.HowManyWords to access an index of the array. I say this because the function you are doing this in is already a member of myList. Also you are using the boolean comparision operator '==' to assign a value to an index of the array so that might actually be where your error is.
1 2 3 4
else
{
myList.Array[HowManyWords++] = Word2add;
}
if HowManyWords was at 7 before this function was called, after this, myList.Array[7] = Word2add; and HowManyWords = 8;
Now the problem I am having is trying to check if the two lists are equal. It says they are equal AS LONG as the one of the words i input is the same as one in the list. I think I should be using recursion?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
bool equal_lists(WordList &myList, NewList &userList)
{
int i=0;
if (contains(myList.Array[i],userList)==1){ //base case
for (i=1; i<=MAX_WORDS;){
if (contains(myList.Array[i],userList)==1){
i++;
}
cout <<"they are the same"<<endl;
returntrue;
}
}
cout <<"The two lists are not the same!"<<endl;
returnfalse;
}
d. equal_lists - takes two wordlists as parameters and returns true if the lists are in the same
language, have the same number of elements, and every element of one list is found in the
other. (Hint: call contains repeatedly.)
No need for recursion here, although could be fun to implement, but remember that equal lists needs to have those bolded requirements in order to pass the 2 lists as being equal. Sort of like this:
bool equal_lists(WordList &myList, NewList &userList)
{
int i;
if (myList.language == userList.language)
{
if (myList.HowManyWords == userList.HowManyWords)
{
for (i=0;i<=myList.HowManyWords; i++)
{
if(contains(myList.Array[i],userList)==1)
{
if (i==myList.HowManyWords)
{
cout <<endl<<"The words are the same! The lists are the same!" << endl;
returntrue;
}
}
else
{
cout <<endl<<"The words are not the same!" << endl;
break;
returnfalse;
}
}
}
else
{
cout<<"Number of elements are not the same."<<endl;
returnfalse;
}
}
else
{
cout <<"Language is not the same."<<endl;
returnfalse;
}
}
You don't want to be printing the same message each time the words match. So you really don't need to be checking for a match in that if-statement, you should be checking for non-match then print the appropriate message and exit when you find one.
line 20-22 should be
1 2 3
cout <<endl<<"The words are not the same!" << endl;
returnfalse;
break;
Finally your code should return something as a default which is what I had the at the very end of mine:
1 2 3
cout << "The two lists are the same!"<<endl;
returntrue;
}
if(contains(myList.Array[i],userList)==1)
{
if (i==myList.HowManyWords)
{
cout <<endl<<"The words are the same! The lists are the same!" << endl;
returntrue;
}
}
else
{
cout <<endl<<"The words are not the same!" << endl;
break;
returnfalse;
}
}
i should use:
1 2 3 4 5 6 7 8 9 10 11 12
if(contains(myList.Array[i],userList)==0)
{
cout <<endl<<"The words are not the same! The lists are not the same!" << endl<<endl;
returnfalse;
break;
}
else
{
cout <<endl<<"The words are the same! The lists are the same!" << endl << endl;
returntrue;
}