-1

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.

  • 2
    what is this `word == verifyArr[i]` ? I suppose its a typo. Further your loop returns on the very first iteration. I guess you want to move at least one of the `return` out of the loop – 463035818_is_not_an_ai Mar 01 '22 at 09:12
  • Hi, may be you can redirect the output of your compilation into a file ? this way you can read the initial error. – Martin Morterol Mar 01 '22 at 09:12
  • 1
    There are so many things wrong with this code, I don't even know where to start. Nothing about this code is done right. You need to scrap this code and start over. – Remy Lebeau Mar 01 '22 at 09:19
  • @RemyLebeau yeah i cant do that man i know its bad but it just needs to be fucntional – Gavin Hayes Mar 01 '22 at 09:21
  • 1
    @GavinHayes it is not just bad. The way you load the file is wrong. The way you use the array is wrong. The logic of the game is wrong. Your teacher should not accept this code even if you got it "working". It needs a complete rewrite to do things the right way. – Remy Lebeau Mar 01 '22 at 09:24
  • 1
    @GavinHayes It's not even functional. Take a close look at `verifyExists`. It will always return after the first iteration, no matter what. First you should write down what the function is supposed to do, then figure out how you would achieve that with pen & paper. E.g. a mathematical formula, or a algorithm, maybe even pseudo code. Then coding should be easy. – Lukas-T Mar 01 '22 at 09:27
  • I think you need to review the introduction to arrays in your favourite C++ book, and think more about the difference between one string and an array of strings. – molbdnilo Mar 01 '22 at 10:23

2 Answers2

1

Your code has a lot of problems in it:

1.Your includes are wrong and some of them are missing:

#include <string>
#include <fstream>
#include <ctime> // There's no need of this. Just #include <iostream> and you should be good

Here are the correct includes:

#include <iostream>
#include <string>
#include <stream>

2.Max can be defined globally as const int:

#include <fstream>

const int max = 2315;

3.You're comparing std::string with char:

word == verifyArr[i]

..so change your function definition to:

bool verifyExists(std::string word, std::string verifyArr[max])

Same goes for playGame():

void playGame(std::string word, std::string arr[max])

4.Your file accessing can be way better:

std::string word;
int loop = 0; // I have defined these variables here so that they get destroyed after the code inside `if (myfile.is_open())` ends.

while (std::getline(myfile, word))
{
    wordArray[loop++] = word;
}

5.Return value of verifyExists() ignored

verifyExists(guessWord, arr);

Replace it with:

if (verifyExists(guessWord, arr))
{
    // Word exists
    std::cout << guessWord << std::endl;
}
else
{
    // Word does not exist
}

6.using namespace std is considered as bad practice

Click here to know why.

So after all of these fixes, here is the correct code:

Final Code:

#include <iostream>
#include <string>
#include <fstream>

const int max = 2315;

bool verifyExists(std::string word, std::string verifyArr[max]) 
{
    for (int i = 0; i < max; i++)
    {
        if (word == verifyArr[i])
        {
            return true;
        }
    }
    return false;
}

void playGame(std::string word, std::string arr[max])
{
    std::string guessWord;

    std::cout << "Ok. I am thinking of a word with 5 letters." << std::endl;
    std::cout << "What word would you like to guess?" << std::endl;

    std::getline(std::cin, guessWord);
    if (verifyExists(guessWord, arr))
    {
        // Word exists
        std::cout << guessWord << std::endl;
    }
    else
    {
        // Word does not exist
    }
}

int main() 
{
    std::string wordArray[max];

    std::ifstream myfile("proj1_data.txt");

    std::cout << "Welcome to UMBC Wordle" << std::endl;

    if (myfile.is_open())
    {
        std::string word;
        int loop = 0;
        while (std::getline(myfile, word))
        {
            wordArray[loop++] = word;
        }

        std::cout << "Your file was imported!" << std::endl;
        std::cout << "2315 Words imported" << std::endl;
        myfile.close();
    }
    
    srand(time(0));
    std::string chosenWord = wordArray[rand() % max];

    playGame(chosenWord, wordArray);

    return 0;
}
The Coding Fox
  • 1,488
  • 1
  • 4
  • 18
  • This code has a major flaw, namely the `if(...) return true; else return false`. Basically an unconditional return in the loop. – Lukas-T Mar 01 '22 at 09:36
  • Oh right, forgot to remove that. – The Coding Fox Mar 01 '22 at 09:40
  • It's also printing "Your file was imported!" and "2315 Words imported" before the import even began ;) It also breaks if there are more then 2315 lines in the file. The original code is so broken, you'd probably have to rewrite it completely. – Lukas-T Mar 01 '22 at 09:45
  • That's right. But I just showed the errors that were causing compiler errors. But I've edited the answer. – The Coding Fox Mar 01 '22 at 09:47
0

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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770