-7

Right now, I'm learning C++. I have made my first little program, an addition calculator. I write two numbers and it adds them. Can you tell me why my output is 0?

#include <iostream>
#include <string>
using namespace std;

int main () {
    int a;
    int b;
    string number1;
    string number2;
    number1 = a;
    number2 = b;
    int output;
    output = a + b;
    getline (cin, number1);
    getline (cin, number2);
    cout << output;
}

Output:

1
1
0
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • `number1 = a;` is wrong, do `number1 = to_string(a);` (though I don't see why you do that at all since you overwrite it with `getline`). Also, `a` and `b` are never initialized, so using them is UB. Also, you don't add the result of the input (which you are storing in a string BTW). You do input _after_ adding two numbers (which are not initialized) so the input is basically useless. – mediocrevegetable1 Apr 13 '21 at 14:40
  • 7
    It looks like you believe the part before `getline` are equations that define relationships between the variables, but that's not how C++ works. Get yourself [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start at the beginning. – molbdnilo Apr 13 '21 at 14:43
  • 3
    C++ doesn't work like math. The mathematical expression `output = a + b` would define a relationship between `a`, `b` and `output`. But C++ uses value semantics. The expression `output = a + b` will assign the immediate result of `a + b` to `output`, it will evaluate `a` and `b`, add those values, then copy the result to `output`. It does not create a relationship between those variables. Further changes to `a` or `b` will not be reflected on `output`. You would need to perform `output = a + b;` again to update the current value of `output`. – François Andrieux Apr 13 '21 at 14:47
  • Looks like you are coming from a different perspective and might your first programming language. For example x=x+1 is completely fine in a programming perspective, while in mathematics doesn't make sense, you are not describing relationships, you are giving execution instruction to be executed at that moment. I would recommend using python/c/java/javascript instead as the C++ is not trivial to learn. – Anton Krug Apr 13 '21 at 15:11
  • The highly technical term for this kind of a bug is called "putting the cart before the horse". You have to read the input into variables ***before*** you try to use those variables. – Sam Varshavchik Apr 13 '21 at 15:14

1 Answers1

2

Step through your code in order and you'll see the problem:

int main () {
    int a;
    int b;
    string number1;
    string number2;
    number1 = a;
    number2 = b;
    int output;
    output = a + b;
    getline (cin, number1);
    getline (cin, number2);
    cout << output;
}

You define four variables, fiddle with two of them in a weird way, and then calculate output as a+b.

By this point you haven't actually interacted with the user. You finish doing all that, and THEN you accept your two values (as strings) and output the previously-calculated value.

You never use the values you pull in from input, as you do that after doing anything else.

Maybe try this:

#include <iostream>
#include <string>

int main () {
    std::string number1;
    std::string number2;
    std::getline (std::cin, number1);
    std::getline (std::cin, number2);

    int a = std::stol(number1);
    int b = std::stol(number2);
    int output = a + b;
    std::cout << output << std::endl;
}

That is, define your strings and then get them from the user.

std::stol is "string to long". It converts the strings to numbers.

The rest should make sense.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36