Mar 2, 2018 at 11:13pm UTC
In visual studio I am trying to build my solution but continually get C2065 errors that claim that many of my variables have not been declared. This is not true so I'm wondering what is happening. My compiler's error list says that variables "CurrentWord", "Difficulty", "Guess", "Response", and "SecretWord" have not been declared even though I explicitly did.
Note: I don't know if this is relevant but the intention of this solution is to output to the console a Bull Cow Game in which a user has to guess a secret word. Kind of like hang man except that the user has to guess an entire word at a time and the console outputs the number of letters the user guessed correctly (cows) and the number of letters the user guessed incorrectly (bulls). This isn't even close to finished. I just want to know why I am getting this error.
Here are my files that are to be linked together:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
//main.cpp
#include "FBullCowGame.h"
#include <string>
#include <iostream>
#include <fstream>
void PrintIntro();
void PlayGame(); //TO IMPLEMENT
bool AskToPlayAgain();
int main(){
PrintIntro();
bool bPlayAgain = false ;
do {
PlayGame();
bPlayAgain = AskToPlayAgain();
} while (bPlayAgain);
return 0; // exit the application
};
void PrintIntro(){
//constexpr int WORLD_LENGTH = 9;
std::cout << "Welcome to Bulls and Cows, a fun word game.\n" ;
//std::cout << "Can you guess the " << WORLD_LENGTH;
//std::cout << " letter isogram I'm thinking of?\n";
std::cout << std::endl;
return ;
};
void PlayGame() {
FBullCowGame BCGame; // instantiate a new game
//variable "Difficulty" is assigned difficulty based on user input
FText Difficulty = BCGame.DifficultySelect();
//sets the max tries based on the difficulty user selected
BCGame.SetMaxTries(Difficulty);
int32 MaxTries = BCGame.GetMaxTries();
//TO CREATE: Method that finds word from ListOfWords and assigns it to SecretWord
BCGame.SetSecretWord();
for (int32 count = 1; count <= MaxTries; count++) {
FText Guess = BCGame.GetGuess();
std::cout << "Your guess was: " << Guess << std::endl;
std::cout << std::endl;
}
return ;
};
bool AskToPlayAgain(){
std::cout << "Do you want to play again (yes/no)? " ;
FText Response = "" ;
std::getline(std::cin, Response);
//converts all characters of string in to lower case characters
Response = toLowerCase(Response);
bool validAnswer = false ;
//do-while loop ensures that user enters valid response before loop exits
do {
if (Response == "yes" || Response == "y" ) {
validAnswer = true ;
}
else if (Response == "no" || Response == "n" ) {
validAnswer = false ;
}
else {
std::cout << "Please enter a valid response (yes/no): " << std::endl;
std::getline(std::cin, Response);
Response = toLowerCase(Response);
}
} while (validAnswer = false );
return validAnswer;
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
//FBullCowGame.h
#ifndef __FBULLCOWGAME_H_INCLUDED__
#define __FBULLCOWGAME_H_INCLUDED__
#include "toLowerCase.h" //[mine]
#include <string>
using FText = std::string;
using int32 = int ;
//values intitialized to zero
struct BullCowCount {
int32 Bulls = 0;
int32 Cows = 0;
};
class FBullCowGame {
public :
//void Reset(); //TO DO: Make a more rich return value
FText DifficultySelect(); //[mine]
void SetMaxTries(FText Difficulty);
int32 GetMaxTries() const ;
void SetSecretWord(); //[mine]
int32 GetCurrentTry();
//bool IsGameWon() const;
FText GetGuess();
FText ToLowerCase(FText word);
//bool GuessCheckValidity(FText); TO DO: Make a more rich return value
//FText SubmitGuess(FText);
private :
int32 MyCurrentTrys = 0;
int32 MyMaxTries = 0;
FText SecretWord = "" ;
FText Guess = "" ;
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
//FBullCowGame.cpp
#include "FBullCowGame.h"
#include <string>
#include <iostream>
#include <fstream>
FText FBullCowGame::DifficultySelect() {
std::cout << "Please select a difficulty (easy, medium, hard, brutal): " ;
FText Difficulty = "" ;
std::cin >> Difficulty;
Difficulty = toLowerCase(Difficulty);
bool ValidAnswer = false ;
//makes sure user inputs valid difficulty
do {
if (Difficulty == "easy" || "medium" || "hard" || "brutal" ) {
ValidAnswer = true ;
}
else {
std::cout << "Please enter valid choice (easy, medium, hard, brutal): " ;
std::cin >> Difficulty;
Difficulty = toLowerCase(Difficulty);
}
} while (ValidAnswer == false );
return Difficulty;
};
void FBullCowGame::SetMaxTries(FText Difficulty) {
if (Difficulty == "easy" ) {
MyMaxTries = 15;
}
else if (Difficulty == "medium" ) {
MyMaxTries = 10;
}
else if (Difficulty == "hard" ) {
MyMaxTries = 5;
}
else if (Difficulty == "brutal" ) {
MyMaxTries = 3;
}
return ;
};
int32 FBullCowGame::GetMaxTries() const {
return MyMaxTries;
};
void FBullCowGame::SetSecretWord(){
std::ifstream inFile;
//opens ListOfWords.txt file which has 100 words to select from
inFile.open("ListOfWords.txt" );
if (inFile.fail()) {
std::cerr << "Error opening file" << std::endl;
exit(1);
}
//pick a random number from 0 to 99
int32 RandomNumber = rand() % 100;
int32 Count = 0;
FText CurrentWord = "" ;
inFile >> CurrentWord;
//conditional expression searches for word in text file until the Count matches
if (Count == RandomNumber) {
SecretWord = CurrentWord;
}
else {
while (Count != RandomNumber) {
Count++;
inFile >> CurrentWord;
}
SecretWord = CurrentWord;
}
inFile.close();
};
int32 FBullCowGame::GetCurrentTry() {
return MyCurrentTrys;
};
/*void FBullCowGame::Reset() {
constexpr int32 MAX_TRIES = 8;
MyMaxTries = MAX_TRIES;
const FString HIDDEN_WORD = "Planet";
MyHiddenWord = HIDDEN_WORD;
MyHiddenWord = HIDDEN_WORD;
MyCurrentTry = 1;
return;
}*/
/*bool FBullCowGame::IsGameWon() const {
return false;
}*/
FText FBullCowGame::GetGuess() {
int32 CurrentTry = GetCurrentTry();
//get a guess from the player
std::cout << "Try " << CurrentTry << ". Enter your guess: " ;
FText Guess = "" ;
std::getline(std::cin, Guess);
//converts all characters to lower case
Guess = toLowerCase(Guess);
return Guess;
};
//turns all characters of a String in to lower case
FText FBullCowGame::ToLowerCase(FText word) {
int32 i = 0;
//iterate through the word and make every letter lower case
for (i = 0; i < (int32)(word.length()); i++) {
//condition met if character is uppercase
if ((word[i] >= 65) && (word[i] <= 90)) {
//character is then transformed from uppercase to lower case
word[i] = word[i] + 32;
}
}
return word;
};
/*bool FBullCowGame::GuessCheckValidity(FText) {
return false;
};*/
/*BullCowCount FBullCowGame::SubmitGuess(FString){
//increment the turn number
MyCurrentTry++;
//setup a return variable
BullCowCount BullCowCount;
//loop through the letters in the guess
//compare letters against the hidden word
return BullCowCount();
}*/
Last edited on Mar 3, 2018 at 12:06am UTC
Mar 2, 2018 at 11:49pm UTC
Your using
declarations of FText
and int32
are not in the proper file, move the declarations into your toLowerCase header file so the new types can be seen in all of your source files.
Either make your toLowerCase()
function be inlined, or remove the function definition out of your header file into a source (cpp) file.
Last edited on Mar 2, 2018 at 11:50pm UTC
Mar 2, 2018 at 11:55pm UTC
Why would I put the declarations in "toLowerCase.h"? Wouldn't the using declarations in "FBullCowGame.h" be enough for the entire solution since the FBullCowGame header file is included in both "main.cpp" and "FBullCowGame.cpp"?
Also, I got rid of the "toLowerCase.h" file and just placed the function it contained in "FBullCowGame.cpp" and the definition in "FBullCowGame.h".
Last edited on Mar 3, 2018 at 12:08am UTC
Mar 3, 2018 at 12:40am UTC
That did the trick! Thank you very much.