# TripleX finished...With a caveat. Help?

I’ve finished TripleX. A small deviation I tried to make from the course is to digress a difficulty level upon failure. It seems to work, however at first, failing level 1 would crash the game. So I added another if/else condition. It now allows failure from level 1. But now, if you fail level 1, the game will require you to complete level 1 twice before progressing to level 2. Any insight on how to implement this properly? Thanks!

``````#include <iostream>

#include <ctime>

void PrintIntro(int Difficulty)

{

//This is the beginning of the story!

std::cout << "\n\nYou are the motherfucking LockPickingLawyer, crack this lock at level " << Difficulty;

std::cout << "\nEnter the code to open this bitch ass lock\n\n";

}

bool PlayGame(int Difficulty)

{

PrintIntro(Difficulty);

const int CodeA = rand() % Difficulty + Difficulty;

const int CodeB = rand() % Difficulty + Difficulty;

const int CodeC = rand() % Difficulty + Difficulty;

const int CodeSum = CodeA + CodeB + CodeC;

const int CodeProduct = CodeA * CodeB * CodeC;

std::cout << "+The code on this shitty lock is a 3 digit combination\n";

std::cout << "+The sum of the 3 numbers are: " << CodeSum;

std::cout << "\n+The product of the 3 numbers are: " << CodeProduct;

//Player interaction

int PlayerGuessA, PlayerGuessB, PlayerGuessC;

std::cout << "\n\n*Input your guess for the first digit: ";

std::cin >> PlayerGuessA;

std::cout << "*Input your guess for the second digit: ";

std::cin >> PlayerGuessB;

std::cout << "*Input your guess for the third digit: ";

std::cin >> PlayerGuessC;

int PlayerGuessSum = PlayerGuessA + PlayerGuessB + PlayerGuessC;

int PlayerGuessProd = PlayerGuessA * PlayerGuessB * PlayerGuessC;

if (PlayerGuessSum == CodeSum && PlayerGuessProd == CodeProduct)

{

std::cout << "\n***Of course you got it right, you're the LockPickingLawyer! Carry on to the next lock!***\n";

return true;

}

else

{

std::cout << "\n***Oh no, wrong! Maybe you're better suited for the desctructive approach... You'll need to practice on the previous lock again.***\n";

return false;

}

}

int main()

{

srand(time(NULL));

int LevelDifficulty = 1;

int const MinLevel = 1;

int const MaxLevel = 10;

while (LevelDifficulty <= MaxLevel) //Loop Game until levels complete

{

bool bLevelWin = PlayGame(LevelDifficulty);

std::cin.clear(); //Clears errors

if (bLevelWin)

{

++LevelDifficulty;

}

else

{

if (LevelDifficulty == MinLevel)

{

PlayGame(LevelDifficulty);

}

else

{

--LevelDifficulty;

}

}

}

std::cout << "\n\n*****Congratulations, you truly are the LockPickingLawyer.*****";

return 0;

}``````

Not exactly sure why it crashes on a failed level 1 specifically, but maybe something you’d want to try:

In your main() function, where you check to see if the player is on MinLevel, you use the == operator, which will only return true if it is exactly the same. you might want to try changing it to <= which will return true if it is less than OR equal to MinLevel. This way, in the unlikely, but maybe possible event that your level is 0 or less, you still play the proper level. It shouldn’t affect the functionality of your code, but it may help protect yourself. Be sure to re-assign Level Difficulty to MinLevel in that if statement though to make sure they don’t play the same level twice by accident.

As for the crash, if the above recommendation doesn’t fix it, I’ve got a hunch that it has something to do with how your processing the result of the game. Try checking your if statement for anything that could cause an unexpected event to occur. Aside from that, your code looks fine to me.

Let me know what you find!

Remember, debugging is your friend…

The game crashes without the if statement you added, because when you failed, you never checked if the value is less than 1, you just reduced Difficulty by 1 making it 0, therefore rand() % Difficulty gets you a division by 0 error:

Of course, you won’t be able to see this in console because it closes immediately, these are logic and not syntax errors, which take some thinking and using the IDE’s debugging tools to discover, the compiler won’t complain because it is the corrent syntax, but not the correct logic. About playing Level 1 two times now, I wasn’t able to replicate this, works fine for me…

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.