Hey all,
I'm trying to write a console program in C++ that will search a list of words from a file (a dictionary word list), check them to see if they're in alphabetical order, check that they're at least 4 characters long, and then check them against a list of restricted words. If they pass, they are saved to a vector of strings to output later on. However, whenever I run my program in the IDE, I get an error. There are no syntax errors in the code, so I know it's a logical error somewhere. I've narrowed the problem down to one function, but can't find the error. Here's the code for my function:
// Function that reads in a list of words from a file, checks them for alphebetical order,
// checks for punctuation, and that the word is at least 4 characters in length, then saves
// them to a vector of strings. Total words and words that match the pattern are both counted.
// Preconditions: must receive ifstream from calling function and vector<string> must be
// previously declared.
// Postconditions: returns values for words, wordCount and vectorCount to calling function.
void readFile (ifstream& inFile, vector<string>& words, string resWords[], int& wordCount, int& vectorCount) {
bool inOrder = false;
bool matched = false;
string temp;
while (inFile >> temp) {
for (unsignedint i = 0; i < temp.length(); i++) {
while (!isalpha(temp[i])) {
temp.erase(1,i);
}
if (temp[i] <= temp[i + 1] && temp.length() >= 4) { // tests the first two characters and total length >= 4.
inOrder = true;
}
else {
inOrder = false;
break; // no need to continue
}
}
if (inOrder) {
for (int k = 0; k <= RES_NUM; k++) {
if (temp == resWords[k]) // checks word against restricted words list.
matched = true;
}
if (!matched) {
words[vectorCount] = temp;
vectorCount++;
}
}
wordCount++;
temp.clear(); // resets string x to 0
inOrder = false;
matched = false; // resets bool values to default.
}
}
Can anyone spot the mistake I've made? I keep looking at it over and over, but I can't manage to spot what's going on. Any help would be greatly appreciated!
When I try to run the program with debugging it still crashes immediately. I know, however, that the file is itself opening:
1 2 3 4 5 6 7
ifstream inFile;
inFile.open("wordlist.txt");
if (!inFile) {
cout << "File open error." << endl;
cout << "Please check for valid file and run program again." << endl;
return 1;
}
And not getting the console printout for the error. If I remove the file from the root directory this error message is displayed upon start. The word list is approximately 72,000 words long, with single words on each line, and includes punctuation (hence the "isalpha" in my code).
I don't know if this helps at all, but this is the debug information for the error. I honestly have no idea what it all means, but maybe it means something to one of you.
Also note that erasing characters changes the length of the string. You chould start a new for loop after you erase everything. Also you should only run that for loop until length - 1, because you check temp[i + 1], and you don't want to run out of bounds.
By the way, it looks like you're check that the strings are in alphabetical order, not the entries. Is that what you want?
This looks like code that produces the debug msg, not the debug info. The debugger has stepped into this code.
Did you mange to put a breakpoint at the start of your function, so that you can step through line by line? Then you can look at the watchlist without it crashing first.
Also it might be where the function is called, that causes the problem.
I'm trying to check and see if the words themselves are in alphabetical order, such as "ghost." I have a sort to make sure each string is in alphabetical order in the vector. But I'm assuming that the code I have written should accomplish this, right? As far as I can tell at least, this code should produce the output I'm desiring; a vector of strings that has only a list of strings that match the requirements: alphabetical order, at least 4 characters long and do not match a word in a previously input list of restricted words.
OK, I've tried debugging the code and stepping over it line by line. Even after going through dozens of words, everything seems to work correctly. All the variables are what they should be, but it's still crashing with errors. The error I'm getting now is:
Can anyone tell me what could be causing this? Also, if it's helpful, I can post the entire code I've written if needed. But I know it's got to be in this function.