Why does this alwas return an else statement? I'm desperate [Solved]

#include <iostream>

int main() 
{    

    //5-7 lines: Story intro printed to the console
    std::cout << "You've been here for hours, lurking in the shadows of your office. Finally you decided to see what the safe in the boss's office holds after you've discovered the algorithm principle on which his password works.";
    std::cout << std::endl;
    std::cout << "The door creaks as it opens and you bring yourself closer to the safe, you can recognize 3 number slots on the safe digital screen." << std::endl;

    //Declaring unchangeable variables for a,b,c
    const int CodeA = 4;
    const int CodeB = 2;
    const int CodeC = 3;

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

    //Prints CodeSum and CodeProduct to terminal
    std::cout << std::endl;
    std::cout << "There are 3 numbers in the code" << std::endl;
    std::cout << std::endl;
    std::cout << "The codes add up to: " << CodeSum << std::endl;
    std::cout << "The codes multiply to give: " << CodeProduct << std::endl;

    int GuessA;
    int GuessB;
    int GuessC;

    int GuessSum = GuessA + GuessB + GuessC;
    int GuessProduct = GuessA * GuessB * GuessC;

    std::cin >> GuessA;
    std::cin >> GuessB;
    std::cin >> GuessC;

    if(GuessSum == CodeSum && GuessProduct == CodeProduct)
    {
        std::cout << "The screen lights up green with a confirmation sound, you're in!";
    }

    else
    {
        std::cout << "Alarms go off, you scramble to retreat as you can hear the sound of police sirens in the distance, you messed up!";
    }
  

    return 0;   
}

It seems the compiler assigns random values every time to my GuessSum and GuessProduct because they’re uninitialized, but I thought thats what IM supposed to do with console input… So confused.

I solved it by putting declared GuessSum and GuessProduct AFTER cin, makes sense in hindsight I was assigning values to GuessSum and GuessProduct after they already calculated the values so I was assigning nothing…

Nice self-solve :slight_smile:

1 Like

Thank you, took me a while… Its frustrating but thats how you learn the most I guess.

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

Privacy & Terms