Question:
C++ answer is always 0 problem?
Kjdf
2013-08-05 10:14:36 UTC
I'm new to C++ but I tried making a program to calculate the magnitude of 2D coordinates. According to me everything seems fine but when I run the program and type my numbers in the answer is always 0.

#include
#include
using namespace std;

float XPos;
float YPos;
float Xsquared;
float Ysquared;
float sum;
float total;




int main()
{
Xsquared = XPos * XPos;
Ysquared = YPos * YPos;
sum = Xsquared + Ysquared;
total = sqrt(sum);

cout<<"Enter X postion: ";
cin>>XPos;
cout<cout<<"Enter Y position: ";
cin>>YPos;
cout<cout<}
Four answers:
AnalProgrammer
2013-08-05 10:16:23 UTC
The order you do things matters.

Try

int main()

{



cout<<"Enter X postion: ";

cin>>XPos;

cout<
cout<<"Enter Y position: ";

cin>>YPos;

cout<
Xsquared = XPos * XPos;

Ysquared = YPos * YPos;

sum = Xsquared + Ysquared;

total = sqrt(sum);

cout<
}



Have fun.
2013-08-05 17:18:03 UTC
When you first make a variable the default value for a float or a integer is zero. You calculated the total before you assigned the variables with cin. Place the sum and total calcuations after the user input like so:



...

cout<<"Enter Y position: ";

cin>>YPos;

cout<
Xsquared = XPos * XPos;

Ysquared = YPos * YPos;

sum = Xsquared + Ysquared;

total = sqrt(sum);

cout<
...



Edit: I was ninja'd.



@Tumbleweed: It can depend on the language and compiler. And yes you should always declare your variables. In this case because they are all global variables, in C++ globals are zero initialized, and will have the value of zero. The results will vary with compiler if you make uninitialized local variables.
_Object
2013-08-05 21:21:04 UTC
What you need to keep in mind is that C does not explicitly let you write equations!

The '=' is an assignment!

If the correct values are not input before you assign XPos * XPos to XSquared, and that's where the problem is coming from.



It stores the value on the right (the rvalue) into the value on the left (lvalue).



Get input before you evaluate XSquared, and YSquared. That way you'll be okay.
tumbleweed_biff
2013-08-05 17:51:38 UTC
I am not sure about the default of zero ... I seem to recall that if you create a variable and do not initialize it, you may well end up with bad data which was already in that memory location but no longer had any pointers to it so was awaiting reuse/garbage collection. Be save and ALWAYS initialize your variables to a known state.



Also, it has been quite a while since I worked with C++, but putting your variable declarations outside of a procedure/function/method makes them global. This is considered a major NO-NO unless you have a very specific and compelling need to create a global variable. If not, always declare (and destroy) it locally. This will vastly improve your code and prevent many undesirable behaviors, including something called memory leaks ... If you declare a global variable and do not explicitly destroy it, it will sit there ad infinititum. Run the same procedure a few times and soon you could well have a nice cloud of them running around out there in Limbo.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...