Else statement not working

So I just completed lecture 60 and I am facing a problem with my code

// Fill out your copyright notice in the Description page of Project Settings.

#include "BullCowCartridge.h"

void UBullCowCartridge::BeginPlay() // When the game starts

{   

    Super::BeginPlay();

    SetupGame(); // Setting Up Game

    PrintLine(TEXT("The HiddenWord is: %s"), *HiddenWord); // Debug Line

}

void UBullCowCartridge::OnInput(const FString & Input) // When the player hits enter

{

    if (bGameOver)

    {

        ClearScreen();

        SetupGame();

    }

    else // Checking Player Guess

    {

        if (Input == HiddenWord)

        {

            PrintLine (TEXT("You Win!")); // Winning Message

            EndGame();

        }

        else

        {   

            PrintLine(TEXT("Lost a life!"));

            PrintLine(TEXT("%i"), --Lives);

            if (Lives > 0)

            {   

                if (Input.Len() != HiddenWord.Len())

                {

                    PrintLine(TEXT("Sorry, try guessing again! \nYou have %i lives remaining"), Lives);    

                }

                else 

                {

                PrintLine(TEXT("You have no lives left!"));

                EndGame();

                }

                

            }

        }

    }

}

      

    // Need Not To Be Case Sensitive

    // Check If Isogram

    // Prompt To Guess Again

    // Check Number Of Letters

    // Prompt To Guess Again

    

    // Remove Life

    // Check If Lives > 0

    // If Yes, GuessAgain

    // Show Remaining Lives

    // If No, Print Loosing Message and HiddenWord?

    // Prompt To Restart, Press Enter To PlayAgain

    // Check User Input

    // PlayAgain Or Quit

void UBullCowCartridge::SetupGame()

{

    // Welcome Message

    PrintLine (TEXT("Howdy Farmer! Welcome to Bulls & Cows game")); 

    HiddenWord = TEXT("game");

    Lives = HiddenWord.Len(); 

    bGameOver = false;

    PrintLine (TEXT("Guess the %i letter word!"), HiddenWord.Len());

    PrintLine (TEXT("You have %i lives"), Lives);

    PrintLine (TEXT("Type in your guess and \npress ENTER to continue...")); // Prompt Player For Guess

}

void UBullCowCartridge::EndGame()

{

    bGameOver = true;

    PrintLine(TEXT("Press enter to restart."));

    

}

When I run my code my lives just start depreciating below 0 whereas I have an else function which tells the game to stop if lives is less than 0. How to solve this?

Think about the control flow

else // Checking Player Guess
{
    if (Input == HiddenWord)
    {
        PrintLine (TEXT("You Win!")); // Winning Message
        EndGame();
    }
    else
    {   
        PrintLine(TEXT("Lost a life!"));
        PrintLine(TEXT("%i"), --Lives);
        if (Lives > 0)
        {   
            if (Input.Len() != HiddenWord.Len())
            {
                PrintLine(TEXT("Sorry, try guessing again! \nYou have %i lives remaining"), Lives);    
            }
            else 
            {
                PrintLine(TEXT("You have no lives left!"));
                EndGame();
            }
        }
    }
}

What’s going to happen when Input isn’t HiddenWord?

If the Input isn’t the HiddenWord, it will execute this bit of code-

 else
    {   
        PrintLine(TEXT("Lost a life!"));
        PrintLine(TEXT("%i"), --Lives);
        if (Lives > 0)
        {   
            if (Input.Len() != HiddenWord.Len())
            {
                PrintLine(TEXT("Sorry, try guessing again! \nYou have %i lives remaining"), Lives);    
            }
            else 
            {
                PrintLine(TEXT("You have no lives left!"));
                EndGame();
            }
        }
    }
}

But in this case it is not executing the else statement within it even if the number of lives is less than 0.

You don’t have an else statement for

if (Lives > 0)

You have an else for if (bGameOver) and one for if (Input.Len() != HiddenWord.Len()) but that only happens when if (Lives > 0)

Oh my god thank you for helping. As it turns out it was just a mistake of curly brackets :stuck_out_tongue:

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

Privacy & Terms