Hello vaderboi,
Now that I have everything to work with I see things ahat are half righr and some things that are completely wrong.
Main is a ".cpp" file and should start this way:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <string>
#include <iostream>
//#include <fstream> // <--- Not needed in main.
//#include <ctime> // <--- Should be ctime not time.h and not needed in main.
#include "FBullCowGame.h"
#include "ToLowerCase.h"
void PrintIntro();
void PlayGame();
FText GetGuess();
int32 GetCurrentTry(); // <--- No function definition for this function.
bool AskToPlayAgain();
|
Putting the header files "iostream" and "string" in a header file is the wrong place. Header files should never contain "#include" files. Although there are some rare cases.
Notice my note for line 12. Not a problem right now, but is "int32 GetCurrentTry()" a member function or a stand alone function?
The rest of main looks like it works OK for now.
In the "FBullCowGame.h" file:
The header files are not needed here nor is it the best way to program this. See
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/ This should give you an idea of what not to do.
"using FText = std::string;" is OK to give "std::string" a shorter name.
"using int32 = int;" just gives "int" a new name. The "32" has no meaning or changes the size of an "int" its just a new name.
Defining the struct here is OK, but the class has problems.
In the class in the "public" section the ctor and detor is missing. These should be:
1 2
|
FBullCowGame();
~FBullCowGame();
|
In the "private" section:
Although I applaud you for initializeing your variables do not initialize your variables here. This should be done in the ctor. Now when you define these functions in the ".cpp" file the ctor is where you set the variables equal to zero. The string does not need any action because it is already empty.
The rest of the class looks OK for now and the functions that are used do work.
In the "FBullCowGame.cpp" file :
Being a ".cpp" file you should start with the needed include files. Most of the time order does not make any difference, but I like to start with the "#include" files followed with any header files like "FBullCowGame.h". I changed "time.h" to "ctime" and added the header files "limits", "chrono" and "thread" more on the last two shortly.
In the "SetSecretWord()" member function I added the middle line to allow the program to payse long enough to read the message before the program exits.
1 2 3 4 5 6
|
if (inFile.fail())
{
std::cerr << "Error opening file" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2)); // Requires header files "chrono" and "thread"
exit(1);
}
|
The rest of the functions look OK for now.
For the file "ToLowerCase.h":
First this contains a function and should be a ".cpp" file not a header file.
Line 5 should be in main with all the other prototypes not here. I generally take all these prototypes and put them in a header file I call "proto.hpp". I have been using the ".hpp" extension to go with the ".cpp" extension to keep with being a C++ file. The ".h" extension works just fine too.
In the function. I would define "i" as an unsigned int and remove the type cast in the for loop and it will work just fine. Since "word.length()" returns an unsigned int and "word[i]" can only use a positive number, this works better than all that type casting.
If you include the header file "<cctype>" you can shorten the if statement to just:
word[i]=std::tolower(word[i])
. if the character is a lower case letter then nothing happens otherwise an upper case letter is changed to lower case. The same could be used in the class member function.
Some suggestions that might be useful:
in the class member function "SetSecretWord()". Instead of reading the file each time you need a word, read the file once and put the words into an array or vector, I prefer the vector, this way you do not have to open and close the file each time you need a new word.The random number will become the subscript to the array or vector to retrive the word.Another thought is that a vector could hold 50, 100, 150 or anything in brtween or greater.Then you couse use
vectorName.size()[/ code] to retrieve the number of elements in the vector, i.e., the number of entries.Then you could use[code]rand() % vectorName.size();
making this a more generic bit of code that could use any number of secert words. At the least I would put the vector in main and pass it to where it is needed, but I am leaning more twards putting the vector in the class where it would work better with class member function "SetSecretWord()".
I would put "srand()"at the beginning of main because it only needs to be done once. putting it in the function may not always generate a good random number. Since you are using "time" for the seed, if not enough time has passed between calls to "srand" you could end up with the same number twice.
This last point is really your choice, but consider this:
Your code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
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;
}; // <--- The ";" is not needed here.
|
My code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
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;
}
|
Although white space makes no difference to the compiler my use of the{}s along with proper indenting makes the code easier to read.ANd if you conside a set of{}s that are not on the same screen having them in the same column makes searching for the mate easier.Some day when you write some code that ends with several }s on different lines and have to find the one that is missing it helps when{}s all line up in the same column.
Two last thing for now.When you have an if, else if, while loop or for loop that only has one line of code the{}s are not really needed, but OK if you use them.
2. When I first started working with the program I notice the you put a; after the closing } of the functions. This is not needed at the end of a function and will cause a compile error.
You have a good start to your program. For now there are a few things that need fixed.
Sorry for being so long as I was trying to cover alot at one time.
Hope that helps,
Andy