Any idea why returning false isn't killing my while loop?

I’ll try and comment this up better and repaste the code but I’m walking a way for a bit

//includes basic functions to save time
#include <iostream>

//List Of Variables Used Throughout for quick reference

int RingOfHell = 9;
std::string UserResponse;
std::string Result;
std::string Question;
std::string Answer;

//List of Questions for quick reference

const std::string Question1 = "A Familiar figure from a past life appears...\nLooking you in the eye they ask:\n\n    \"Who is responsible for the worst thing you've ever done?\"";
const std::string Question2 = "Another Question2";
const std::string Question3 = "Another Question3";
const std::string Question4 = "Another Question4";
const std::string Question5 = "Another Question5";
const std::string Question6 = "Another Question6";
const std::string Question7 = "Another Question7";
const std::string Question8 = "Another Question8";
const std::string Question9 = "Another Question9";
const std::string Question10 = "Another Question10";

//List of Answers for quick reference

const std::string Answer1 = "Me";
const std::string Answer2 = "answer2";
const std::string Answer3 = "answer3";
const std::string Answer4 = "answer4";
const std::string Answer5 = "answer5";
const std::string Answer6 = "answer6";
const std::string Answer7 = "answer7";
const std::string Answer8 = "answer8";
const std::string Answer9 = "answer9";
const std::string Answer10 = "answer10";

//Included Funtions Ending with Main. Organized in order of occurence
//Except whe needed to include a function with-in a function 
void PlayIntro()
{
//Feeling
    std::cout << 
    "\nYou awaken cold and confused, sweating from every pore.\n" <<
    "A Sinister Voice echoes:\n\n";

//Dialogue
    std::cout <<
    "   \"Welcome to the ninth ring of Hell\n" <<
    "    You will be tested and tried\n" <<
    "    Answer the questions correctly to move on\n" <<
    "    Answer incorrectly and spend eternity\n" <<
    "    Hopelessly trying to escape.\" \n\n";

//Feeling
    std::cout << "  You feel the presence leave, you are completely alone.\n\n";
}

void AskQuestion()
{
    std::cout << Question << std::endl;
    std::cin >> UserResponse;
    std::cout << std::endl;
}

bool CheckAnswer()
{
    if (UserResponse == Answer)
    {
        RingOfHell = RingOfHell-1;
        if (RingOfHell > 1)
        {
            std::cout << RingOfHell << " Rings Remain.\n\n";
            return true;
        }
        else
        {
            if (RingOfHell == 1)
            {
                std::cout <<RingOfHell<< " Ring Remains.\n\n";
                return true;
            }
        
            else
            {
                std::cout << "Congratulations, You've made it to Purgatory. Continue?: ";
                std::cin >> UserResponse;
                std::cout <<std::endl;
                if (UserResponse == "yes")
                {
                    return true;
                }
                else
                {
                    std::cout << "  Thank You For Playing!\n\n";
                    return false;
                }
            }
        }
    }

    else
    {
        std::cout << "  Delusion betrays you...\n\n  Play again?: ";
        std::cin >> UserResponse;
        std::cout <<std::endl;
        if (UserResponse == "yes")
        {
            RingOfHell = 9;
            Answer = Answer1;
            return true;
        }
        else
        {
            std::cout << "  Thank You For Playing!\n\n";
            return false;
        }
    }
}

bool NextQuestion()
{
    if (Question == Question1)
    {
        Question = Question2;
        Answer = Answer2;
        return true;
    }
    if (Question == Question2)
    {
        Question = Question3;
        Answer = Answer3;
        return true;
    }
    else
    {
        std::cout << "  Conratulations, you looked inward and discovered the truth we are all part of the greater Self.\n" <<
        "  You are free from our reign.\n\n";
        return false;
    }
}
bool PlayGame()
{
    if (RingOfHell == 9)
    {
        PlayIntro();
    }
    AskQuestion();
    CheckAnswer();
    if (true)
    {
        NextQuestion();
        return true;
    }
    else
    {
        return false;
    }
}


    
//main function includes opening dialogue for game, formatted to make text more engaging.
int main()
{
    Question = Question1;
    Answer = Answer1;
    while (true)
    {
    PlayGame();
    }

    return 0;
}

PS I’m screwing with a lot of stuff that we’re not directly covering based off the idea I had for my text based game when they told us to come up with our own, and it has been incredibly helpful having goals for things and not just following along with my own version of the text and variable names. I have minimal experience in Python that gave me some edge since I had a vague understanding of programming concepts, but when I say minimal, I mean I’ve already spent more time with c++ in this course then I had on python in my intro to CS class. This is amazing and I love it so far, but the troubleshooting is getting under my skin so it’s time for a break.

Your while loop is set to while(true) it never receives a false value. As it is now it’s always true. It’d need a boolean variable or a function call in the () to change the conditional. Like calling while(PlayGame()) for instance. It would then put true or false in the parentheses based on what it returned.

Because you call the function twice.

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

Privacy & Terms