Home > Software engineering >  verifyExists function is throwing a crazy amount of errrors
verifyExists function is throwing a crazy amount of errrors

Time:03-02

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.

CodePudding user response:

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;
}

CodePudding user response:

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;
}
  •  Tags:  
  • c
  • Related