#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <vector>
usingnamespace std;
string getFirstWord();
string getSecondWord();
string convert(string);
int main ()
{
string word1, word2;
string incomingword;
cout << "This program will try to establish a word link between two words.\n";
do
{
word1 = getFirstWord(); // returns the first word the user entered
word2 = getSecondWord(); // returns the second word the user entered
if (word1.length() != word2.length())
cout << "The two words must have the same length. Retry.\n";
} while (word1.length() != word2.length());
constint WORD_LENGTH = word1.length(); // stores the word length in WORD_LENGTH
ifstream inputFile("C:\\Users\\owner\\Desktop\\wordlist.txt");
if (inputFile)
{
cout << "File opened successfully.";
for(word1.at(0) = 'A'; word1.at(0) <= 'Z';(word1.at(0))++) //cycles the first letter of word1 through the alphabet
{
while (inputFile >> incomingword)
{
if (word1 == incomingword)
{
cout << word1 << endl;
}
}
}
}
else
{
cout << "The wordlist file could not be opened. Press enter to end the program.\n";
cin.get();
}
return 0;
}
//*****************************************************************************************
//getFirstWord and getSecondWord prompt the user for a word and return it converted to capital letters
//*****************************************************************************************
string getFirstWord()
{
string word1;
cout << "Enter the first word: ";
cin >> word1;
return convert(word1); //returns word1 converted to all capital letters
}
string getSecondWord()
{
string word2;
cout << "Enter the second word: ";
cin >> word2;
return convert(word2); //returns word2 converted to all capital letters
}
string convert(string word)
{
for (unsignedint x = 0; x < word.length(); x++)
{
word.at(x) = toupper (word.at(x));
}
return word;
}
The lines I'm having problems with are 33 - 45. The for loop in line 33 cycles the first letter of the string word1 through each letter of the alphabet. For each letter, I want the program to see if that word exists in the external file wordlist.txt, which is just a big list of English words (all in capital letters, each on a new line). If word1 does exist in the file, I want it to be displayed. So, for example, if word1 starts out as "FIG", I want "BIG", "DIG", "FIG", "GIG", etc. to be displayed because they are all English words and thus should exist in the file. However, when I run my code, no words are displayed at all. Why is this?
Sorry, I misread it.
You are comparing against the whole dictionary, but only the first time. Because when you reach EOF, it doesn't go to the begginning automatically.
So when you want to search 'B*', inputFile>>incomingword fails
IIRC that's because the file is in an invalid state, so the seek has no effect.
You could try to clear it, you could construct the stream inside the loop, or you could transfer the content of the file to a container and then search the element in the container.
std::set is a good option. A trie will be interesting too (especially if you reverse the words)
However, I realized the method I'm using to establish a word link between two words isn't going to work. This method only accounts for words that vary from the original word by only letter, whereas I also need to account for words that vary one letter from words that vary one letter from the original word.
And also thanks for mentioning sets and tries. I'm only two months into an introductory C++ course so I'm not familiar with any of this stuff.