False not killing while loop, restarted course

#include <iostream>
using namespace std;

//declare base variables

string UserResponse;
bool bLevelComplete;

int Level = 1;
int CodeA = 1;
int CodeB = 2;
int CodeC = 3;

int CodeSum = CodeA+CodeB+CodeC;
int CodeProduct = CodeA*CodeB*CodeC;

int PlayerGuess1, PlayerGuess2, PlayerGuess3;

int GuessSum;
int GuessProduct;

//define base Functions

void PrintIntro()
{
    cout <<"\n    Welcome to my game!\n    Solve The Codes To Move On...\n    Good Luck!\n";
}

void AskQuestion()
{
    cout << "\n -There are three numbers in the code...\n -The numbers add up to " << CodeSum << "...\n -The numbers multiply out to " << CodeProduct << "...\n\n    Input Code (separate values with spaces): ";
    cin >> PlayerGuess1 >> PlayerGuess2 >> PlayerGuess3;
    cout << "\n You entered: " << PlayerGuess1 << ", " << PlayerGuess2 << ", " << PlayerGuess3 << ".\n";
    GuessSum = PlayerGuess1+PlayerGuess2+PlayerGuess3;
    GuessProduct = PlayerGuess1*PlayerGuess2*PlayerGuess3;
}

void YouWin()
{
    cout << "\n    You Win! Congratulations and thank you so much for playing!\n    Feel free to reach out to @the_L1ghtBr1nger on telegram if you're interested in collaborating in the future!\n\n    Good Luck With Your Projects!\n";
}

bool PlayGame()
{
        if (Level == 10)
        {
            YouWin();
            return false;
        }
    //Output current level
    cout << "\n    Level: " << Level << endl;
    AskQuestion();
    if(GuessSum == CodeSum && GuessProduct == CodeProduct)
    {
        cout << " Good Job!\n";
        Level = Level+1;
        return true;
    }
    else
    {
        cout << " Sorry wrong input\n Try Again? (Y/n) ";
        cin >> UserResponse;
        cout << endl;
        if (UserResponse == "n")
        {
            return false;
        }
        else
        {
            Level = 1;
            return true;
        }
    }

}

//TripleX game: Ask user to find 3 digit code based on provided sum and product of numbers within code.
int main()
{
    PrintIntro();
    while(true)
    {

        bLevelComplete = PlayGame();
        cin.clear(); //Clears error from non-int input
        cin.ignore(); //Discards buffer
    }

    return 0;
}

I just recently restarted as my last program diverted too heavily from the course it became too difficult to troubleshoot, worried I’ve diverted here but can’t figure out why this isn’t working

Because you have an infinite loop.

 while(true)

Non-const global variables should generally be avoided. There is no need for any of these variables to be global.

C++ Core Guidelines

Using using namespace std is not a good idea and I strongly suggest you stop using it

Why I don't "using namespace std" - YouTube
C++ Weekly - Ep 305 - Stop Using `using namespace` - YouTube
c++ - Why is "using namespace std;" considered bad practice? - Stack Overflow

1 Like

As far as I can tell he uses while(true) still at the end of the video, would you mind elaborating a little?

As far as the global variables go, should i just move each of them to the top of the first functions that call them?

No chance you wanna sum up the why using namespace std is bad? I understand the need to dig into external resources in the field of programming but the only reason I signed up for this class was to tackle c++ in a way that felt more approachable and there’s just a lot of overwhelming aspects so I have been trying to avoid unnecessary deep dives when possible so-as to sustain motivation as I’ve had trouble doing so with programming in the past

Thank you the video was helpful, I just try not to rabbit hole too much

He hasn’t set up the condition yet. That is what the lecture “Comparing Values” is about.

No. Declaring all variables at the top of a function that is a super old practice that comes from C where the language says that you have to.

Variables should be declared at the point of use and with a value to initialise it.

Keep scopes small: C++ Core Guidelines
Always initialise a variable: C++ Core Guidelines
Don’t introduce a variable before you need it: C++ Core Guidelines
Don’t declare a variable until you have a valuew to initialise it (not always possible) C++ Core Guidelines

So what if the variable needs called by another function? Would that need to be a global variable or is there a work around?

Oh Lol I try not to move to the next video till the code behaves as I feel it should so I’ve been hung on this for like over a week lol just rewatching over and over trying to understand what I missed

So before I move on, how is this for a skeleton continuing the course?

//Basic functions such as cout/cin
#include <iostream>

const std::string Intro = "\n\n    Welcome to my game!\n    Crack the 3 digit code on each level to move on!\n";
const std::string SumHint = "\n  The Numbers Add Up To: ";
const std::string ProductHint = "\n  The Numbers Multiply Out To: ";
const std::string InputCode = "\n\n    Input Code: ";

bool PlayGame()
{
    //Declaring variables for question
    int CodeA = 1;
    int CodeB = 2;
    int CodeC = 3;
    int CodeSum = CodeA+CodeB+CodeC;
    int CodeProduct = CodeA*CodeB*CodeC;

    //Prints hints based on variables for the level.
    std::cout << SumHint << CodeSum << ProductHint << CodeProduct << InputCode;

    // Initializing Variables for user input
    int CodeInputA;
    int CodeInputB;
    int CodeInputC;
    int InputSum;
    int InputProduct;

    //Collect user input for 3 digit code
    std::cin >> CodeInputA >> CodeInputB >> CodeInputC;


    InputSum = CodeInputA+CodeInputB+CodeInputC;
    InputProduct = CodeInputA*CodeInputB*CodeInputC;
    
    std::cout << "\n\n    You Entered: " << CodeInputA << " " << CodeInputB << " " << CodeInputC << std::endl;

    //Check user input against code
    if (InputSum == CodeSum && InputProduct == CodeProduct)
    {
        std::cout << "\n  Good Job!\n";
        return true;
    }
    else
    {
        std::string Continue;
        std::cout << "\n    Sorry, wrong input.\n    Try again? (Y/n) ";
        std::cin >> Continue;
        
        if (Continue == "n")
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}

int main()
{
    std::cout << Intro;
    bool bLevelComplete = true;
    while(bLevelComplete)
    {
       bLevelComplete=PlayGame();
    }
    return 0;
}

Looking a lot better. My notes

  1. I’m not really sure that those global std::string's are buying you much in terms of readability. In terms of efficiency/performance that would be worse than without them as std::string is not a simple type and construction of one is non-trivial.

    Just in case you have this misconception "This" isn’t a std::string, "This" is a const char[5].

  2.  int InputSum;
     int InputProduct;
    
     //Collect user input for 3 digit code
     std::cin >> CodeInputA >> CodeInputB >> CodeInputC;
    
     InputSum = CodeInputA+CodeInputB+CodeInputC;
     InputProduct = CodeInputA*CodeInputB*CodeInputC;
    

    There is no reason to declare InputSum and Product before you give them a value. Refer back to the guidelines from my previous reply. Doing so allows you to make them const as well.

Will we go over const char in this class or no?

Ok sorry for bothering you so much I’m doing a little research on const char implementation and screwed up a few times, so for the time being I just plugged in the text to cout, but as far as readability and format goes, do we look okayish down here…?

//Basic functions such as cout/cin
#include <iostream>

void PrintIntro(int Difficulty)
{
    if (Difficulty == 1)
    {
    std::cout << "\n----------------------------------------------------------------------------------------";
    std::cout << "\n\n    Welcome to my game!\n    Crack the 3 digit code on each level to move on!\n";
    }
    std::cout << "\n    Level: " << Difficulty << std::endl;
}

bool PlayGame(int Difficulty)
{
    PrintIntro(Difficulty);
    //Declaring variables for question
    int CodeA = 1;
    int CodeB = 2;
    int CodeC = 3;
    int CodeSum = CodeA+CodeB+CodeC;
    int CodeProduct = CodeA*CodeB*CodeC;
    
    std::cout << "\n  The Numbers Add Up To: " << CodeSum << "\n  The Numbers Multiply Out To: " << CodeProduct;
    std::cout << "\n\n    Input Code: ";

    //Initializing Variables for user input
    int CodeInputA;
    int CodeInputB;
    int CodeInputC;

    std::cin >> CodeInputA >> CodeInputB >> CodeInputC;

    int InputSum = CodeInputA+CodeInputB+CodeInputC;
    int InputProduct = CodeInputA*CodeInputB*CodeInputC;
    
    std::cout << "\n\n    You Entered: " << CodeInputA << " " << CodeInputB << " " << CodeInputC << std::endl;

    //Check user input against code
    if (InputSum == CodeSum && InputProduct == CodeProduct)
    {
        std::cout << "\n    Good Job!\n";
        return true;
    }
    else
    {
        std::cout << "\n    Sorry, wrong input.\n    Try again? (Y/n) ";
        return false;
    }
}

int main()
{
    int LevelDifficulty = 1;
    int MaxLevel;
    std::cout << "\n  How many levels would you like to play? ";
    std::cin >> MaxLevel;
    while (LevelDifficulty <= MaxLevel)
    {
        bool bLevelComplete = PlayGame(LevelDifficulty);
        std::cin.clear(); //Clears error from non-int input
        std::cin.ignore(); //Discards buffer
        
        if (bLevelComplete)
        {
            ++LevelDifficulty;
            if (LevelDifficulty == MaxLevel +1)
            std::cout << "\n  Congratulations and thank you for playing my game!\n  Feel free to reach out if you'd like to collaborate or study together!\n";
        }
        else
        {
            std::string Continue;
            std::cin >> Continue;
            if (Continue == "n")
            {
                LevelDifficulty = MaxLevel + 1;
            }
            else
            {

                LevelDifficulty = 1;
            }
        }
    }
    
    return 0;
}

In both of these snippets a new scope is created; when a new scope is created the code within it should be indented.

I’d also strongly recommend always using {} even for single lime if's as they can prevent hard to spot bugs.

Thank you, I think that was a result of me copying and pasting poorly but it’s definitely something I need to get better at catching.

I totally missed that I didn’t put these {} I didn’t think it would compile without them, so if you don’t have them does it just fire the next line of code and end the scope of that if?

Anyway, just to show I listened

//Basic functions such as cout/cin
#include <iostream>
#include <ctime>

void PrintIntro(int Difficulty)
{
    if (Difficulty == 1)
    {
        std::cout << "\n----------------------------------------------------------------------------------------";
        std::cout << "\n\n    Welcome to my game!\n    Crack the 3 digit code on each level to move on!\n";
    }
    std::cout << "\n    Level: " << Difficulty << std::endl;
}

bool PlayGame(int Difficulty)
{
    PrintIntro(Difficulty);
    //Declaring variables for question
    int CodeA = (rand()%Difficulty+Difficulty)+(rand()%3);
    int CodeB = (rand()%Difficulty+Difficulty)+(rand()%3);
    int CodeC = (rand()%Difficulty+Difficulty)+(rand()%3);
    int CodeSum = CodeA+CodeB+CodeC;
    int CodeProduct = CodeA*CodeB*CodeC;
    
    std::cout << "\n  The Numbers Add Up To: " << CodeSum << "\n  The Numbers Multiply Out To: " << CodeProduct;
    std::cout << "\n\n    Input Code: ";

    //Initializing Variables for user input
    int CodeInputA;
    int CodeInputB;
    int CodeInputC;

    std::cin >> CodeInputA >> CodeInputB >> CodeInputC;

    int InputSum = CodeInputA+CodeInputB+CodeInputC;
    int InputProduct = CodeInputA*CodeInputB*CodeInputC;
    
    std::cout << "\n\n    You Entered: " << CodeInputA << " " << CodeInputB << " " << CodeInputC << std::endl;

    //Check user input against code
    if (InputSum == CodeSum && InputProduct == CodeProduct)
    {
        std::cout << "\n    Good Job!\n";
        return true;
    }
    else
    {
        std::cout << "\n    Sorry, wrong input.\n    Try again? (Y/n) ";
        return false;
    }
}

int main()
{
    srand(time(NULL));
    
    int LevelDifficulty = 1;
    int MaxLevel;
    std::cout << "\n  How many levels would you like to play? ";
    std::cin >> MaxLevel;
    while (LevelDifficulty <= MaxLevel)
    {
        bool bLevelComplete = PlayGame(LevelDifficulty);
        std::cin.clear(); //Clears error from non-int input
        std::cin.ignore(); //Discards buffer
        
        if (bLevelComplete)
        {
            ++LevelDifficulty;
            if (LevelDifficulty == MaxLevel +1)
            {
                std::cout << "\n  Congratulations and thank you for playing my game!\n  Feel free to reach out to @the_L1ghtbr1nger on Telegram \n  if you'd like to collaborate or study together!\n";
            }
        }
        else
        {
            std::string Continue;
            std::cin >> Continue;
            if (Continue == "n")
            {
                LevelDifficulty = MaxLevel + 1;
            }
            else
            {

                LevelDifficulty = 1;
            }
        }
    }
    
    return 0;
}

Formally, an if statement has the following syntax

if (condition) statement

And {} is just a compound statement, as the name suggests it allows you to compound statements, typically used where one statement is expected.

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

Privacy & Terms