Help Understanding this!

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.

you need to create a structure that will hold the words and the data like the word count. your words will be stored in an array within the struct.

a, b, c, d, and e are just functions.

what are you not understanding about it?
You can start from here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <fstream>
#define MAX_WORDS 20

using std::cin;
using std::string;
using std::cout;
using std::endl;
using std::ifstream;

struct WordList
{
    string language;
    int HowManyWords;
    string Array[MAX_WORDS];
    
public:
    
    void load_word_list(ifstream, WordList);
    void add_word(string, WordList);
    bool contains(string, WordList);
    bool equal_lists(WordList, WordList);
    void display_word_list();
    
};


int main()
{
    WordList myWords;
    return 0;
}

void WordList::load_word_list(ifstream readStream, WordList myList)
{
    
}

void WordList::add_word(string Word2add, WordList myList)
{
    
}

bool WordList::contains(string Word2Search, WordList myList)
{
    return true;
}

bool WordList::equal_lists(WordList myList1, WordList myList2)
{
    return true;
}

void WordList::display_word_list()
{
    for (int q = 0; q < HowManyWords; q++)
    {
        cout << Array[q] << " ";
        if ((q + 1) % 4 == 0)
            cout<<endl;
    }
    cout << endl;
}
Thankyou for the starting code!

I am not understanding this:

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.


I think the rest are self explained
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

As for reading the file contents into a list, a simple search on google revealed this:
http://www.cplusplus.com/doc/tutorial/files/

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

Gl
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?
1
2
3
4
			if (myList.Array[18]=="\0"){
				cout << "not full" << endl;
			}
			else cout <<"full"<<endl;
Last edited on
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;
}
OHH right! super obvious, but totally forgot about that!

Now, for adding the string to the new list i tried:

1
2
3
4
5
else
	{
		myList.Array[myList.HowManyWords+1]==Word2add;	 
			 myList.HowManyWords++;
	}


which is not working. Im not sure how to use strcpy in this case, what can I do?
Last edited on
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;
Thanks again! adding the constructor fixed things! Also, it won't let me use

myList.Array[HowManyWords++]

but it works with myList.Array[myList.HowManyWords++]
Ok so the first if should be

1
2
3
4
5
if (myList.HowManyWords >= MAX_WORDS)
{
  cout <<"List is full\n" << endl;
  return;
}


Gl
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;
			return true;
			}
		}
	cout <<"The two lists are not the same!"<<endl;
return false;
}
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
bool equal_lists(WordList &myList, WordList &userList)
{
    if (myList.language == userList.language)
    {
        if (myList.HowManyWords == userList.HowManyWords)
        {
            //
            //
            //
            //
        }
        else
        {
            //
            //
        }
    }
    else
    {
        //
        //
    }
    
    
    cout << "The two lists are the same!"<<endl;
    return true;
}
Last edited on
Well, I think you had a MUCH more simple idea in mind... any suggestions to improve this lol? seems rather much


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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;
						return true;
					}
				}
				else
				{
					cout <<endl<<"The words are not the same!" << endl;
						break;
						return false;
				}
			}
        }

        else
        {
            cout<<"Number of elements are not the same."<<endl;
			return false;
        }
    }
    else
    {
        cout <<"Language is not the same."<<endl;
            return false;
    }
}
line 8 should be

for (i=0; i < myList.HowManyWords; i++)

line 14

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;
						return false;
						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;
    return true;
}
So instead of:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
				if(contains(myList.Array[i],userList)==1)
				{
					if (i==myList.HowManyWords)
					{
						cout <<endl<<"The words are the same! The lists are the same!" << endl;
						return true;
					}
				}
				else
				{
					cout <<endl<<"The words are not the same!" << endl;
						break;
						return false;
				}
			}
        



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;
					return false;
					break;
					
				}
				else
				{
					cout <<endl<<"The words are the same! The lists are the same!" << endl << endl;
					return true;		
				}
Last edited on
Remove the else. The lists are not the same until all the values have been compared
ok! Thanks so much for the help!
Last edited on
Topic archived. No new replies allowed.