Click to See Complete Forum and Search --> : C++ Problem


DSwain
06-19-2005, 07:44 PM
I'm working on learning some C++ at the moment, and I've run into a bit of an issue. I managed to get it to build/compile properly, so it isn't a syntax issue, but it's how the output is working.

In this case, I have a few variables which the user will give an integer value for it. After that, the value of x is a*a + b*b. When it gets to this portion of the code, it gives me an extremely inaccurate value. For example, when I enter a=2 and b=3 it says the result is 11374... pretty off balance. I believe what I need to do is seperate the a*a and b*b in some way so they're done apart from each other and then added together. Here's the section of the code which deals with it.

f=d*d+e*e;
cout << " Enter Your Numbers!"<<"\n";
cin >> d;
cin >> e;
cout << "Your value is:"<<f;

Any help or ideas is appreciated. Also I can post all the code if deemed nessisary.

mrhoyt4
06-19-2005, 08:26 PM
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main() {
int d = 0;
int e = 0;
int f = 0;
cout << " Enter Your Numbers!"<<"\n";
cin >> d;
cin >> e;
f=d*d+e*e;
cout << "Your value is:"<<f << endl;
return 0;
}


You never and the variables together after you get them.

endoalpha
06-19-2005, 08:46 PM
f=d*d+e*e;
cout << " Enter Your Numbers!"<<"\n";
cin >> d;
cin >> e;
cout << "Your value is:"<<f;

The main problem with this code is that you are assigning f the value of d*d+e*e BEFORE you input the values for d and e. That could cause a big mess.

bwkaz
06-19-2005, 09:00 PM
The main problem with this code is that you are assigning f the value of d*d+e*e BEFORE you input the values for d and e. That could cause a big mess. Well, not a huge mess, but it'll use random values for d and e when calculating f. Which is where your enormous value came from. (It may have even been different on different runs of the program. The exact value used for d and e depends on the stack contents when that function is called.)

DSwain
06-19-2005, 09:24 PM
Ah I see it now. I'll give it a shot right now.

Thanks for being smart guys... seriously I'd be all messed up. Now that it's been mentioned though, I remember reading the guide that said one difference between C and C++ is that variables can be assigned anywhere in the code as opposed to in the beginning part, so this makes sense how it's built.

sharth
06-20-2005, 11:36 AM
not that kind of assignment though... In c and c++, it doesn't make f always equal d*d+e*e, it makes f equal to what d*d+e*e equals at that exact moment. f stores a number, not a function.

In c, you can't do something like this..

int main() {
int a = 0;
a=a+1;
int b = 0;
return b;
}

Because you have initialized another variable "b" after you've done a more actiony kind of statement. That will fail in C. However, it will work perfectly fine in C++. That's the difference that you were referring to.

:edit: Okay.. that's what i remember as being the problem. And I know it's been a problem for me before because of initilaizing counters in for loops... but for whatever reason, my compiler is taking this no problem... Oh well, if someone wants to fix it feel free, but that's still the right idea.. mostly :)

DSwain
06-20-2005, 12:07 PM
Haha I see, I see. That seems to make sense. At any rate, the solutions I got seemed to work perfectly fine in this case. Now I'm on to the real tough stuff: if statements and stuff like that! :eek:

It shouldn't be too tough cause I learned this type of stuff with bash and such. Thanks for the help once again, though.

bwkaz
06-20-2005, 10:25 PM
but for whatever reason, my compiler is taking this no problem... I think it's a gcc extension. I think you can turn it off with -std=c89 or -std=c99 (I'm not sure which, though, and I'm not sure what else that'll turn off that you might need).