WHAT'S WRONG WITH MY CODE

The answer is always 0 for some reason, I already tried this code on Microsoft Visual C++ and it worked now I'm on a Mac so I am using Eclipse.
I would really appreciate it if someone can tell me what is happening.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
	double x = 0.0;
	double y = 0.0;
	double z = ( x + y );

	cin >> x;
	cin >> y;
	cout << z;

	cin.ignore();
	return 0;
}
On line 8 both x and y is 0. 0+0 is 0 so the value of z is set to 0. The value of z is never changed so when you print its value on line 12 it prints 0.
Last edited on
What would you suggest to change it?

PS: Thanks for replying so quick
Set the value of z after you have read in the values of x and y but before you print it.
Topic archived. No new replies allowed.