My code is always returning 0?

Ok, so I follow the video word for word with the coding. When I started this, I copied everything Ben had me do, but for some reason I don’t get the same outcome he does. Instead, I get 0’s when I’m running the program. If anyone could help please.

Attached I included snippets of FBullCowGame.h and FBullCowGame.cpp BEFORE THE VIDEO. If need be I can include anything after the video or my main.cpp if that might have the problem.

@varasj Can you screenshot an example of what happens when you compile and run the code? Any error codes that come through? Or what the program runs?

Also the main.cpp if you can, please. :smile:

It may be you are not initializing the variable in the main.cpp to properly be set to the value of CurrentTry or something.

Make sure in the main.cpp you have initialized your variable that you will actually print like so:

	int CurrentTry = BCGame.GetCurrentTry();

Then when you want to print it, you call it by:

std:: cout << "Try " << CurrentTry << ". Enter your guess: "

That’s what I’m thinking could be the problem. If I can see your main.cpp it may clarify something else.

@edwilke86 So I’ve decided to tackle it head on from the beginning. In the video, I am at the 2:30 timestamp and Ben is getting the value 666 outputted in the console window. I am not.

Instead I am getting the regular values.

Here is my .h file


and my main.cpp

From this point in the video, Ben has only made the definition of FBullCowGame into the .cpp and put the ‘int MyCurrentTry’ and ‘int MyMaxTries’ into the “FBullCowGame::FBullCowGame(){}” code. After Ben does this, he gets the output of 666 in the console, but HOW? I am trying to see where I went wrong with my code and NOTHING.

@edwilke86 THIS IS FANTASTIC I AM SUCH AN IDIOT! Ok not in a bad way but I just figured it out. What I didn’t do was delete the ‘int’ from MyCurrentTry and MyMaxTry when I put it in the FBullCowGame::FBullCowGame code! See the code:

Before I had it set as integers but OBVIOUSLY that wouldn’t change because they are constants! (At least I hope that’s the answer… if not I have some serious studying to do) Anyway I have the correct outcome:

Everything else seems to be correct in the code. Thanks for your help!

Awesome! Yeah, it’s usually something like that. The more mistakes you make, (and spend 30 minutes finding that you forgot a semicolon or something like that, and the compiler didn’t catch it to tell you) the more you learn, and the faster you get at it.

Privacy & Terms