GuessSum and GuessProduct declaration procedure

Hi, I was testing to see if there is any difference when declaring GuessSum and GuessProduct before or after the cin operation, turns out…it does give two different outcome

Declare before cin operation, value churned out is random and numbers are incorrectly huge
Declare after cin operation, value of sum and product generated are correct

Can anyone explain the logic behind this? Thanks.

In the first screenshot you have uninitialised variables. Reading their values is undefined behaviour (the standard doesn’t say what should happen so technically quite literally anything can happen). That explains the weird numbers.

The code is executed sequentially, fixing the unitialised issue what would you expect the values of GuesSum and GuessProduct be?

int GuessA = 0, GuessB = 0, GuessC = 0;
int GuessSum = GuessA + GuessB  + GuessC;
int GuessProduct = GuessA * GuessB  * GuessC;
std::cin >> GuessA >> GuessB  >> GuessC;

It’s 0 for both GuessProduct and GuessSum

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

Privacy & Terms