Result not being displayed

I am writing a program that generates 8 random letters (vowel or consonant to the users choice). It then opens a file of words and checks to see if the letters match any of the words in it, then it should display them. My problem is that after I choose the letters, nothing happens, it doesn't even display " Yours words are ".
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <iostream>
#include <fstream>
#include <cstdlib> 
#include <ctime>
#include <algorithm>
#include <string>
using namespace std;


string *pointer;

char response;
int letter;
int letter2;
int n;
char  result[8];

string words;

string temp[70549] [25];

char array1[70549] [25];
char array2[70549] [25];
char array3[70549] [25];
char array4[70549] [25];
char array5[70549] [25];
char array6[70549] [25];
char array7[70549] [25];
char array8[70549] [25];
char array9[70549] [25];
char array10[70549] [25];

	int i = 0;
	char line[256];

	int j = 0;
	int k = 0;

int main()
{
pointer:
cout<< "Consonant (c) or vowel(v)?" << endl;
cin >> response; 

n ++;


{

	if ( 'c' == response ) 
	{
		srand (time(0));
		letter =  (rand() %21);
	

			switch (letter)						//selects random const
			{
			case 0: 
				cout << "b" << endl;
				result[n] = 'b';
				break;

			case 1: 
				cout << "c" << endl;
				result[n] = 'c';
				break;

			case 2:  
				cout << "d" << endl;
				result[n] = 'd';
				break;

			case 3: 
				cout << "f" << endl;
				result[n] = 'f';
				break;

			case 4:
				cout << "g" << endl;
				result[n] = 'g';
				break;

			case 5: 
				cout << "h"<< endl;
				result[n] = 'h';
				break;

			case 6: 
				cout << "j" << endl;
				result[n] = 'j';
				break;

			case 7: 
				cout << "k" << endl;
				result[n] = 'k';
				break;

			case 8: 
				cout << "l" << endl;
				result[n] = 'l';
				break;

			case 9: 
				cout << "m" << endl;
				result[n] = 'm';
				break;

			case 10: 
				cout << "n" << endl;
				result[n] = 'n';
				break;

			case 11: 
				cout << "p" << endl;
				result[n] = 'p';
				break;

			case 12: 
				cout << "q" << endl;
				result[n] = 'q';
				break;

			case 13: 
				cout << "r" << endl;
				result[n] = 'r';
				break;

			case 14: 
				cout << "s"<< endl;
				result[n] = 's';
				break;

			case 15: 
				cout << "t" << endl;
				result[n] = 't';
				break;

			case 16: 
				cout << "v"<< endl;
				result[n] = 'v';
				break;

			case 17: 
				cout << "w"<< endl;
				result[n] = 'w';
				break;
	
			case 18: 
				cout << "x" <<endl;
				result[n] = 'x';
				break;
	
			case 19: 
				cout << "y"<< endl;
				result[n] = 'y';
				break;
	
			case 20: 
				cout << "z"<< endl;
				result[n] = 'z';
				break;
			}
	}

else if ('v' == response)
	{
		srand (time(0));
		letter2 = ( rand() %4);
	
		switch (letter2)								//selects random vowel
			{
			case 0: 
				cout <<  "a"<< endl;
				result[n] = 'a';
				break;

			case 1: 
				cout << "e" <<endl;
				result[n] = 'e';
				break;

			case 2: 
				cout << "i"<< endl;
				result[n] = 'i';
				break;

			case 3: 
				cout << "o"<< endl;
				result[n] = 'o';
				break;

			case 4: 
				cout << "u" << endl;
				result[n] = 'u';

			}	
		}

	else 
		{
		cout << "Please enter 'c' or 'v' " << endl;
		goto pointer;
		}
	}

if (n <8)
	{
		goto pointer;
	}

cout<<endl<< "Your letters are";

std::sort(result, result+sizeof(result));			//sorts the letters


for(int i=0; i<9; i++)
	{
		cout << result[i];
		cout << " ";
	}




	
	 //opens the file and puts it in array
	FILE* file = fopen("C:\\Users\\Chris\\Documents\\words.txt", "r"); 
	
	while (fgets(line, sizeof(line), file))
	{
		strcpy(array1[i], line);
		std::sort(array1[i], array1[i]+sizeof(array1[i]));			//sorts into alphabetical order
		i++;
	
	}
	

	for( j = 0; j <sizeof(array1); j++)
	{
		for ( k = 0; k < sizeof(array1[k]); k++)
			{

				cin >> array1[j][k];
			}
	}			

	
	

	// comparing the stuff
for (int n=0; n< 8; n++)
{
	int matches =0;
	int maxmatch = 0;
	string matchWord;
		
	for (int i=0; i<70549; i++)
	{
		for (int j=0; j< sizeof(array1[i]); j++)
		{
			if (result[n] == array1[i][j])
			{ matches ++; 
			array1[i][j] = array2[i][j];
			}		

		}
		
		if (matches >maxmatch)
			{
				maxmatch = matches;
				matchWord = array1[i];
			}
	}



}
cout << array2[i];

for (int i=0; i< sizeof(array2[i]); i++)
	{
		if (result[1] == array2[i][j])
			array2 ==array3;

	}
for (int i=0; i< sizeof(array3[i]); i++)
	{
		if (result[2] == array3[i][j])
			array3 == array4;

	}
for (int i=0; i< sizeof(array4[i]); i++)
	{
		if (result[3] == array4[i][j])
			array4 == array5;

	}
for (int i=0; i< sizeof(array5[i]); i++)
	{
		if (result[4] == array5[i][j])
			array5 == array6;

	}
for (int i=0; i< sizeof(array6[i]); i++)
	{
		if (result[5] == array6[i][j])
			array6 == array7;

	}
for (int i=0; i< sizeof(array7[i]); i++)
	{
		if (result[6] == array7[i][j])
			array7 == array8;

	}
for (int i=0; i< sizeof(array8[i]); i++)
	{
		if (result[7] == array8[i][j])
			array8 == array9;

	}
for (int i=0; i< sizeof(array9[i]); i++)
	{
		if (result[8] == array9[i][j])
			array9 == array10;

	}
cout << "Your words are "<<array10;
return 0;
}
Last edited on
Shouldn't you use loop to display every character from array?
I tried that but i don't think the program is even getting that far as it is not displaying " Your words are ".
You can really easily generate letters like this:
1
2
3
4
5
6
7
8
9
10
11
char letters[8];

    //Generate and output random letters.
    for(i = 0; i < 8; ++i)
    {
        letters[i] = rand() % 26 + 65; 
    }
    for(i = 0; i < 8; ++i)
    {
        std::cout << letters[i] << " ";
    }


If you need separate generation of vowels or consonants for some reason then disregard results which are a,e,i,o,u to get consonants and only include results a,e,i,o,u for vowels using if statements and a while loop, which only increments if the correct letter is found. Technically, it will make the loop have to do more work and faster ways are around, but it's really not going to be a problem here. This compresses about 150 lines of your code into just 6 lines!

Secondly, you need to find a way to structure this program that is not using goto. There is absolutely no reason at all to use goto here and it's a very bad habit to get into!

I'm not really sure what line 270 - 327 is supposed to be doing?



After tinkering with it for a while i found what was stopping it from working and now i have a different question. How would i check to see if the letters in result match the words in array1?
So array1 consists of ~70 000 words with up to 25 characters? I really don't understand the size of that array. :S Surely you would want up to 8 chars... Erm anyway... You have to check if each letter in turn is there and remember to mark the letter as used when you have found it (otherwise a word with multiple instances of the same letter might be found when only one of that letter exists).

I would also suggest making your char arrays sensible if you're going to do it like this. Make a char array [n][8], that's n, 8 character long words, and for this array check if every single letter is found. Make a char array [n][7] with 7 letter words. You know to terminate if 2 letters are found not to match. etc...

I will not put any algorithms up until you have had a go at this yourself because I don't think just giving you an answer is beneficial. =/
Last edited on
1
2
3
4
5
for ( int i = 0; i < 70549; i++)
			{
		 array1[i].find(result[0]);
			
			}


So i tried that and got the error " left of '.find' must have class/struct/union"
What does that mean?
It means that function find() is a member function of some class from std namespace, but it isn't member function of char. You can't use function find on char like that.
I have seen people use it before online. And when I change it to string it says the same thing
Ok so I've changed my array1 to a string and it runs fine but when i go to display array2 nothing comes out. What have i done wrong?
1
2
3
4
5
6
7
8
for(int i = 0; i <70549; i++)
 {
	 if (array1[i].find(result[0]))
	 {

		 array1 == array2;
	 }
 }
array1 == array2;

That returns a boolean... which gets thrown away.
Ok so now I have
array2 = array1
and now it gives out about the first 30 words.
Ok here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (int i =0; i < 70549; i ++)
			{
				std::size_t found = array1[i].find(result[0]);

				if (found!=std::string::npos)
					array2[i] = array1[i] ;
				
				
			}


cout << endl << endl<<"Your words are: ";
		for (int i =0; i <sizeof(array2[i]); i++)
		cout << array2[i] << endl;




and all i'm getting when i run it is a bunch of blank spaces. What am i doing wrong?
I'm very confused about how you attempted this. Here is a special case example you can generalize. I don't think it's the very best way to do this, but it's reasonably efficient and it works, you just will need to expand it to use a larger word list. I left lots of comments so hopefully you understand.
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
#include <iostream>
#include <string>
#include <random>
#include <ctime>

int main()
{
    srand(time(NULL)); //Seed random number generator.
    char letters[8]; //Letter set.
    bool found[8] = {0}; //Found letters.
    unsigned int i =  0, j = 0; //Just to iterate around loops.
    std::string s ("eat");  //Word to search for.
    std::string wordlist[1]; //Words we found in the letter list.
    unsigned int totalfound = 0; //Total words found.
    unsigned int hits = 0; //Hits so far on this word.
    for(i = 0; i < 8; ++i) //Generate random letters.
    {
        letters[i] = 'a' + rand() % 26; //Random letter.
        std::cout << letters[i] << "\n";
    }
   // letters[3] = 'e'; //Uncomment these three lines if you want a definite match. 
    //letters[5] = 't';
    //letters[6] = 'a';
    for(i = 0; i < 8; ++i)
    {
        //Check if each char in turn is in the word.
        for(j = 0; j < s.length(); ++j)
        {
            if(letters[i] == s.at(j) && found[j] == 0) //If the letters match and we didn't already find that letter.
            {
                found[j] = 1; //Mark that letter as being found.
                ++hits; //We found another letter!
                if(hits == s.length() - 1)
                {
                    wordlist[0] = s; //Add the found word to our word list. 
                    ++totalfound; //Keeping track of total words found. 
                }
                break; //Letter found, so break out of this for loop and move onto next letter.
            }
        }
    }
    if(totalfound > 0) //If we found any words.
    std::cout << "Matches:\n\n" << wordlist[0] << "\n"; //Display them.
    return 0;
}


Edit: Added some more comments.
Last edited on
Topic archived. No new replies allowed.