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 ".
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. =/
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.
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.
#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.
unsignedint 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.
unsignedint totalfound = 0; //Total words found.
unsignedint 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;
}