'verifyExists function is throwing a crazy amount of errrors
I'm making a wordle program for a class assignment and the basic concept is to load all 5 letter words in the English language from a text file into an array, then pick one randomly to be the correct one, and I have that part correct (probably isn't that efficient but it works for now). I need to verify the user input that the 5 letter word they entered is actually in that array. For example they enter "djghd", which isn't a word that would be in that array. here is the code that I used:
#include <string>
#include <fstream>
#include <ctime>
using namespace std;
bool verifyExists(string word, string verifyArr) {
for (int i = 0; i < 2315; i++)
{
if (word == verifyArr[i])
{
return true;
} else {
return false;
}
}
}
void playGame(string word, string arr) {
string guessWord;
cout << "Ok. I am thinking of a word with 5 letters." << endl;
cout << "What word would you like to guess?" << endl;
getline(cin, guessWord);
verifyExists(guessWord, arr[2315]);
cout << guessWord << endl;
}
int main() {
string word;
int loop = 0;
string wordArray[2315];
ifstream myfile ("proj1_data.txt");
cout << "Welcome to UMBC Wordle" << endl;
if (myfile.is_open())
{
cout << "Your file was imported!" << endl;
cout << "2315 Words imported" << endl;
while (! myfile.eof())
{
getline(myfile, word);
wordArray[loop] = word;
loop++;
}
myfile.close();
}
int max;
max = 2315;
srand(time(0));
string chosenWord = wordArray[rand() % max];
playGame(chosenWord, wordArray[2315]);
return 0;
}
When I try to compile it, it throws a ton of errors (Which might just be the compiler I'm using) and i need to use infinite scroll to get through them all so I cant add them here. They only show up after I added the verifyExists function so I know that's the source of the error I just don't know what is causing it. I'm also not allowed to use pointers so that makes it difficult. Any help is greatly appreciated, thank you.
Solution 1:[1]
Everything about this code is wrong. The #include statements are wrong. The way you load the file is wrong. The way you use the array is wrong. The logic of the game is wrong. It needs a complete rewrite to do things the right way.
Try something more like this instead:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
bool verifyExists(const string &word, const vector<string> &verifyArr) {
for (size_t i = 0; i < verifyArr.size(); ++i) {
if (word == verifyArr[i]) {
return true;
}
}
return false;
// Alternatively, use std::find() instead...
// return find(verifyArr.begin(), verifyArr.end(), word) != verifyArr.end();
}
void playGame(const string &chosenWord, const vector<string> &wordArray) {
string guessWord;
cout << "Ok. I am thinking of a word with 5 letters." << endl;
cout << "What word would you like to guess?" << endl;
getline(cin, guessWord);
if (guessWord == chosenWord) {
cout << "You guessed correct" << endl;
} else {
cout << "You guessed incorrect" << endl;
}
if (verifyExists(guessWord, wordArray)) {
cout << "Your guess exists in the list" << endl;
} else {
cout << "Your guess does not exist in the list" << endl;
}
}
int main() {
cout << "Welcome to UMBC Wordle" << endl;
vector<string> wordArray;
ifstream myfile ("proj1_data.txt");
if (myfile.is_open()) {
string word;
while (getline(myfile, word)) {
wordArray.push_back(word);
}
myfile.close();
}
if (wordArray.empty()) {
cout << "No words were imported!" << endl;
return 0;
}
cout << "Your file was imported!" << endl;
cout << wordArray.size() << " Words imported" << endl;
srand(time(0));
string chosenWord = wordArray[rand() % wordArray.size()];
playGame(chosenWord, wordArray);
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Remy Lebeau |
