I'm currently trying to input a formula into code but when I execute, it always comes up as zero. I tried looking around but more forum posts have specific errors that cause them to output zero, and aren't related to what I'm trying to do. So if anyone can be a second pair of eyes, that would be amazing.
-
9Please post the code as code not as images, take the [tour](https://stackoverflow.com/tour) and read the [help page](https://stackoverflow.com/help). Start by reading one of [these C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Ron Aug 06 '17 at 16:58
-
5Remember that C++ is not math. `a = b;` doesn't mean that `a` will *always* have the same value as `b`. – Rakete1111 Aug 06 '17 at 16:59
-
You are invoking undefined behavior on line 5 – Post Self Aug 06 '17 at 17:06
-
Why is the duplicate for this question an April Fool's Post? – Arnav Borborah Feb 11 '18 at 15:41
-
3@ArnavBorborah: because OP's source code above is an image, not text. – Paul R Jun 27 '18 at 14:27
2 Answers
You need to enter the values of "a" and "b" before the initialization of "c".
As it stands when calculating "c" both "a" and "b" are empty.
Also you will have loss of precision because c is not a double.
- 605
- 2
- 7
- 17
-
2To be more accurate, when calculating `c` both `a` and `b` are *uninitialized* and could contain anything at random. – Galik Aug 06 '17 at 17:23
-
@Galik I am aware I just used the word empty to not confusing him – Adin Sijamija Aug 06 '17 at 17:36
-
Thank you for this, im new and trying to learn c++, and you just answer me what my problem was! Thank you!!! – anasan Nov 05 '20 at 00:43
Unfortunately, you're doing the calculation (a / 5) * b on two empty undefined numbers.
Consider this, if you're reading your code from top to bottom (which is often how languages interpret functions), each statement would be interpreted as:
Create 'a' as an integer... (Since you didn't give it a value, I won't guarantee one for you.) in line "int a;"
Create 'b' as an integer... (You also didn't give me a value, I'll just set it to whatever was last in the location in memory.) in line "int b;"
Divide 'a' by 5 (Since you didn't explicitly provide 'a' a value, it would be hard to predict the result.) at "(a / 5)" in line "(a / 5) * b"
Multiply the last calculation by b (You didn't provide 'b' a value either, so you're creating a wildly unpredictable equation). at " * b" in line "(a / 5) * b;"
Fill in the value 'a' (Now you're actually filling in a, but you already did your calculation.) in line "cin >> a;"
Fill in the value 'b' (Same for variable b) in line "cin >> b;"
What you're experiencing is C++ undefined behavior. Basically, because you didn't set either 'a' or 'b' a value, it'll just leave whatever was last in it's spot in memory as it's value. You can read more about it here: https://en.wikipedia.org/wiki/Undefined_behavior
To solve this, run the two "cin >> x" operations before the calculation, but not before the declarations (or "int a; int b;").
Sorry about misusing the code selection. I felt like it wouldn't look right without it.
- 458
- 6
- 17