#Variable "sum{a+b}" isnt working how i thought it would.

1 messages · Page 1 of 1 (latest)

lavish pilot
#

int a{}, b{21}, sum{a+b}
isnt working how i thought it would.
i use std::cin >>a;
to input a number then i use << sum * 2 <<
in order to mulitply the total of a+b by 2.
instead what happens is that a{} is considered zero and it adds b{21} creating 21 and then it multiplies it by 2 and i just keep getting 42 no matter the number i input for std::cin >>.

i thought once i input a number for a{}, the number i input replaces the zero and then it would add with 21 then multiply by 2.

sudden kayakBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

lavish pilot
#

i couldve swore i was making it work yesterday lol

#

oh. or maybe instead of using a{}; i should just put a; so the computer doesnt think its a zero

#

let me try that.

#

nvm doesnt even build when i do that lol

tranquil dome
#

Yeah that’ll be undefined behavior since ur trying to do addiction on a variable that has no value, or trash value

#

Can u just move the sum = a + b;
After the cin >> a; ?

vale jacinth
#

Looks like defined behavior to me.

#

The issue is that sum doesn't retroactively change its value because a changed.

#

You changed a with std::cin >> a;, but that has no effect on sum. If you want it to affect sum you have to do it after changing a.

maiden scaffold
#

a isnt set with a value so it's ub

vale jacinth
#

a is set to 0.

#

int a{}; is one of the many ways to initialize an int to 0.

#

"value initialization"

maiden scaffold
#

"israel"

tranquil dome
#

He changed it to a semi colon though in his last change, so it’s ub since it’s a “trash value” (if that’s the right term?)

maiden scaffold
#

yep

tranquil dome
# lavish pilot

All’s you gotta do is move the

sum = a + b;

AFTER you do cin >> a;

Because after it then a will have the value you need, to do the addition in sum

maiden scaffold
#

int a, b = 21, sum;
std::cin >> a;
sum = a + b;

lavish pilot
#

oh yeah , i didnt mean to write a{}; i just did that because i wrote it there singley. but in my code i had it as a{}, because it was the first

maiden scaffold
#

let us know how it goes

lavish pilot
#

okay this seemed to work. i was gonna actually try this eventually but the thing is, i couldve swore last night i was doing it the initial way haha. maybe i was just super tired and i was doing something else.

tranquil dome
#

Ik it’s confusing at first but basically, the program will step through one line at a time going from top to bottom.

So what u did in ur program was, first you created

int a{}, b{21}, sum{ a + b};

Okay, “a” is equal to zero because you did a{}. So sum is equal to “0 + 21”

Then you did cin >> a;

Okay, that changes the value of “a” ONLY, but not the value of sum.

So then at ur last line u did

sum * 2; Which is just 21 * 2

Since sum never got updated

tranquil dome
lavish pilot
#

thank you guys for the help

#

🙏

tranquil dome
#

Haha yeah, and cool. Learn cpp.com is def a good resource, good luck

lavish pilot
#

thank you 🤝

#

!solved