Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include <string> #include <cstdlib> #include \"Word.h\" #in

ID: 3548898 • Letter: #

Question

#include <iostream>

#include <string>

#include <cstdlib>

#include "Word.h"

#include <fstream>

#include <vector>

#include <ctime>

?

using namespace std;

class WordGame

{

protected:

int charCount, letterCount;

int totalScore = 0;

string word1, validTemp, invalidTemp, userWord, word;

bool isvalid(string userWord);

void getScore(int score);

vector<string> invalidEntered;

vector<string> validEntered;

vector<string> allPossibleValid;

vector<string> W;

vector<int> scores;

};

void WordGame::getScore(int score)

{

scores.push_back(score);

}

bool WordGame::isvalid(string userWord)

{

bool validWord = false;

charCount = 0;

letterCount = 0;

if (userWord.length() <= 3)

{

return validWord;

}

//how much a letter appears

for (int i = 0; i < userWord.length(); i++)

{

char a = userWord[i];

//counts appearance of a in userWord

for (int j = 0; j < userWord.length(); j++)

{

if (a == userWord[j])

{

charCount++;

}

}

//how much char a appears in original word from list

for (int i = 0; i < word1.length(); i++)

{

if (a == word1[i])

{

letterCount++;

}

}

// if the number of times the letter appear in user input is less than or equal to how many times it appear in selected word, then its temporarily

// valid

if (charCount <= letterCount)

{

validTemp = userWord;

}

else if (charCount >= letterCount)

{

invalidTemp = userWord;

invalidEntered.push_back(userWord);

}

else

{

charCount = 0;

letterCount = 0;

}

}

//now can check if validTemp is on word list.....

for (int i = 0; i < W.size(); i++)

{

if ((word1 == validTemp) && (validTemp.length() >= 3))

{

validEntered.push_back(userWord);

validWord = true;

break;

}

}

return validWord;

}

//update score

void score(string userWord, bool validWord)

{

int length = userWord.length();

int totalScore;

if (validWord == true)

{

totalScore += (length - 3) + 1;

}

else

{

totalScore -= 1;

}

if (totalScore < 0)

{

totalScore = 0;

}

}

?

int main()

{

WordGame first;

srand(time(0));

ifstream wordFile("C:\Users\David\Desktop\wordlist.txt");

vector<string> W;

string userWord, word, word1;

cout << "Game loading.............." << endl;

if (wordFile.is_open())

{

while (!wordFile.eof())

{

getline(wordFile, word);

W.push_back(word);

/*for (int i = 0; i < W.size(); i++)

{

cout << W[i] << endl;

}*/

}

wordFile.close();

}

else

{

cout << "Unable to open file";

    }

?

int randomWord = rand() % W.size();

cout << "Your word is ";

cout << W[randomWord] << endl;

word1 = W[randomWord];

cout << "" << endl;

cout << "Enter your word." << endl;

cin >> userWord;

first.isValid(userWord);

system("pause");

return 0;

}

Explanation / Answer

/* ERROR CAME BECAUSE
isvalid is declared as protected means outside class we cant access it.
to make it accessible outside class make that to public.
now u wont get any error
HIGHLIGHTED is the required answer.
*/

#include <iostream>
#include <string>
#include <cstdlib>
#include "Word.h"
#include <fstream>
#include <vector>
#include <ctime>
using namespace std;
class WordGame
{
protected:
int charCount, letterCount;
int totalScore = 0;
string word1, validTemp, invalidTemp, userWord, word;
void getScore(int score);
vector<string> invalidEntered;
vector<string> validEntered;
vector<string> allPossibleValid;
vector<string> W;
vector<int> scores;
public:
bool isvalid(string userWord);
};
void WordGame::getScore(int score)
{
scores.push_back(score);
}
bool WordGame::isvalid(string userWord)
{
bool validWord = false;
charCount = 0;
letterCount = 0;
if (userWord.length() <= 3)
{
return validWord;
}
//how much a letter appears
for (int i = 0; i < userWord.length(); i++)
{
char a = userWord[i];
//counts appearance of a in userWord
for (int j = 0; j < userWord.length(); j++)
{
if (a == userWord[j])
{
charCount++;
}
}
//how much char a appears in original word from list
for (int i = 0; i < word1.length(); i++)
{
if (a == word1[i])
{
letterCount++;
}
}
// if the number of times the letter appear in user input is less than or equal to how many times it appear in selected word, then its temporarily
// valid
if (charCount <= letterCount)
{
validTemp = userWord;
}
else if (charCount >= letterCount)
{
invalidTemp = userWord;
invalidEntered.push_back(userWord);
}
else
{
charCount = 0;
letterCount = 0;
}
}
//now can check if validTemp is on word list.....
for (int i = 0; i < W.size(); i++)
{
if ((word1 == validTemp) && (validTemp.length() >= 3))
{
validEntered.push_back(userWord);
validWord = true;
break;
}
}
return validWord;
}
//update score
void score(string userWord, bool validWord)
{
int length = userWord.length();
int totalScore;
if (validWord == true)
{
totalScore += (length - 3) + 1;
}
else
{
totalScore -= 1;
}
if (totalScore < 0)
{
totalScore = 0;
}
}
int main()
{
WordGame first;
srand(time(0));
ifstream wordFile("C:\Users\David\Desktop\wordlist.txt");
vector<string> W;
string userWord, word, word1;
cout << "Game loading.............." << endl;
if (wordFile.is_open())
{
while (!wordFile.eof())
{
getline(wordFile, word);
W.push_back(word);
/*for (int i = 0; i < W.size(); i++)
{
cout << W[i] << endl;
}*/
}
wordFile.close();
}
else
{
cout << "Unable to open file";
}
int randomWord = rand() % W.size();
cout << "Your word is ";
cout << W[randomWord] << endl;
word1 = W[randomWord];
cout << "" << endl;
cout << "Enter your word." << endl;
cin >> userWord;
first.isValid(userWord);
system("pause");
return 0;
}