-
Word Comparison Error
I posted a thread not too long ago about a word comparison program I was writing, but it vanished pretty fast, and now i have another problem/question.
Code:
while (x <= wordlength)
{
if (wordlist[x] != letterlist[y])
{
y++;
if (y == inputlength + 1)
{
Positive = false;
x = wordlength + 1;
}
}
else
{
++x;
Positive = true;
}//close else statement
}
Using this code, the program tries to find if a word (wordlist) can be made using the letters inputted by the user (letterlist). What I am wondering is, should it matter what order the user inputs the letters in, because apparently it does.
When I put in the word lunacy I get the following output:
lac
lacy
lay
lucy
luna
lunacy
lunn
naa
nay
lay
And when I put in ycanul (lunacy backwards) i get this output:
aal
ann
annul
cal
call
can
caul
cull
nul
null
ull
yan
all
-
forget this: simply from looking at the output i can tell you that you're only searching for words that have those letters in them, rather than searching for words that have those letters in them and that are consecutive.
edit: actually, it seems like you need another loop
-
>>simply from looking at the output i can tell you that you're only searching for words that have those letters in them
I'm pretty sure thats what I want, unless I mistake you. ;)
>>rather than searching for words that have those letters in them and that are consecutive.
Didn't quite catch ya there ;) .
>>edit: actually, it seems like you need another loop
Well, of course ... other wise you would only end up with one word, and thats if the first word on the list matches ... unless I mistake you once again.
-
First question: If the char i appears in the input string can it be used any number of times in the dictionary word or only as many times in the dictionary word as it appears in the input? That is, if the input string is niate would you expect the list to be restricted to words such as: a, i, at, in, ti, ate, eat, tan, ant, tie, tin, ten OR would the word initiate also be acceptable since all letters found in intiate are also found in niate, just with different frequency.
Second question: In the word lists posted it looks like all letters int the "found" words use characters that are in sequential order in the input string. For example, in neither of the lists generated by lunacy or ycanul did the word any appear. I suspect that's because the a is between the n and the y in both cases, rather than them being in the "correct" order but separated by "nonsense" char. Do you wish to limit the combinations to sequential order or do you wish to alow any and all permutations.
The any permutations thing can get complicated pretty quickly. I'm sure others here have had more experience than I at trying to solve this type of a problem, if that's the way you want to go.
-
Well, I originally intended that you could only use a letter once, but seeing as the program wasnt working when you could use the any number of times, I thought that it would be better to fix one problem then to trudge on. Secondly this program is supposed to create all permutations of the input, but it clearly doesnt. The logic of it looks to me like this:
1. The user enters in a series of letters.
2. The program reads in a word from the wordlist.
3. It checks to see if the first letter of the word read in from the wordlist does not match the first letter in the series of letters. (wordlist[x] != letterlist[y])
A. If the first letter of the word does not match with the first letter in the string, check to see if the first letter of the word matches the second letter of the string, and so on until a match. (y++; )
B. If the variable y becomes 1 greater than the length of the user input (effictively looking at and comparing all of the user input with the first letter of the word), then the word is not a permutation of the user input, and you exit the while loop
Code:
if (y == inputlength + 1)
{
Positive = false;
x = wordlength + 1;
}
4. If a match is found, look at the next letter of the word, and repeat step 3.
Code:
else
{
++x;
Positive = true;
}
5. If all of the letters of the word can be made using the user input then push the word into a vector and once all the words in the wordlist have been looked at and compared, display the words.
-
Using the wording for your logic, modified just a little I came up with the following. Not compiled/tested.
Code:
1. The user enters in a series of letters.
string series;
cin >> series;
2. The program reads in a word from the wordlist.
ifstream fin("wordlist.txt");
string word;
int x = 0;
int y = 0;
int wordLength;
int seriesLength = series.length();
std::vector<string> wordsFound;
while(fin >> word)
{
wordLength = word.length();
for(x = 0; x < wordLength; ++x)
{
3. It checks to see if the first letter of the word read
in from the wordlist matches a letter in
the series of letters.
A. If the first letter of the word does not match with
the first letter in the string, check to see if the
first letter of the word matches the second letter of
the string, and so on until a match.
for(y = 0; y < seriesLength; ++y)
{
if(wordlist[x] == series[y])
break;
}
B. If the variable y equals the length of
the user input (effictively looking at and comparing all
of the user input with the current letter of the word),
then the word is not a permutation of the user input,
and you exit the outer loop
if(y == seriesLength)
break;
//else found this letter of word in the series of letters
//input by user so check for the next letter of word.
}
4. If all letters in word are found in series of user input letters then push the word into a vector
if(x == wordLen)
wordsFound.push_back(word);
}
5. check why input from file stopped
if(!file.eof())
cout << "error reading file" << endl;
-
well....when I used this code ... :
Code:
ifstream fin("wordlist.txt");
char series[256];
char word[256];
int x = 0;
int y = 0;
int wordLength;
int seriesLength =strlen(series);
std::vector<string> wordsFound;
cin >> series;
while(1)
{
fin >> word;
cout << '\r' << x;
wordLength = strlen(word);
for(x = 0; x < wordLength; ++x)
{
for(y = 0; y < seriesLength; ++y)
{
if(word[x] == series[y])
break;
}
if(y == seriesLength)
break;
//else found this letter of word in the series of letters
//input by user so check for the next letter of word.
}
if(x == wordLength)
wordsFound.push_back(word);
}
if(!fin.eof())
cout << "error reading file" << endl;
for (int i = 0; i <= wordsFound.size(); i++)
cout << wordsFound[x] << '\n';
cin >> word;
it just sat at 0, and when I did while(fin >> word) it just skipped to the if (!fin.eof()) part .
-
This works fine for me. The only difference from what you posted, besides not using a file, is using i instead of x in the last line.
Code:
char series[256] = "lunacy";
char word[256] = "any";
int x = 0;
int y = 0;
int wordLength;
int seriesLength = strlen(series);
std::vector<string> wordsFound;
wordLength = strlen(word);
for(x = 0; x < wordLength; ++x)
{
for(y = 0; y < seriesLength; ++y)
{
if(word[x] == series[y])
break;
}
if(y == seriesLength)
break;
}
if(x == wordLength)
wordsFound.push_back(word);
for (int i = 0; i < wordsFound.size(); i++)
cout << wordsFound[i] << '\n';
-
ummm well, not using a file is a BIG difference (i think), and it wouldnt have mattered switching the i and the x in the last line because it would never get to the last line.....
-
Dealing with files is certainly a part of the solution, however, the meat of the program is the logic to analyze the letters of a given word with the letters of the input string. Whether the given word is hardcoded or obtained from user or obtained from a file is imaterial to the logic of the analysis part.
The problem I see with your code posted at 308PM was the while loop seems infinite. There is no mechanism to break out of the loop. I'd recommend using the fin >> word syntax as the conditional, rather than 1 as the conditional. That way when the EOF is encountered by >> it will evaluate to false and the loop will terminate. You can prove the while loop stopped secondary to finding EOF by evaluating the stream state using eof() as indicated previously.