Checking string against words in a file problem

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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <vector>
using namespace 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());
	
	const int 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 (unsigned int 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?

Thank you so much.
Last edited on
bump!
Because you aren't searching your word in all the dictionary but in the current line of the file.
I thought

while (inputFile >> incomingword)

would keep reading the contents of wordlist.txt until the end of the file was reached... what am I missing?
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
Okay, I tried adding in some code that would reset the read position to the beginning of the file for each iteration of the for loop. So now I have:

1
2
3
4
5
6
7
8
9
10
11
for (word1.at(0) = 'A'; word1.at(0) <= 'Z'; (word1.at(0))++)		
		{
			while (inputFile >> incomingword)
			{
				if (word1 == incomingword)		
				{	
					cout << word1 << endl;
				}
			}
			inputFile.seekg(0, ios::beg);           //reset read pos to beginning
		}


But that didn't help, the same problem persists. Any ideas?
Last edited on
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)
Clearing it solved all the problems. Thanks.

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.
Topic archived. No new replies allowed.