User defined function issue - different answers

I have written a code which uses a number of functions but something bizarre happens for one of these functions.

it has two double parameters a and b.

cout<<expression(0,20)<<","; This gives the answer 0.179 - which is what I am expecting

but when i do the following:


1
2
3
4
  double a, b;
    cout<<"Insert values for a and b:";
    cin>>a>>b;
   cout<<"expression(a,b) = "<<expression(a,b)<<endl;

Here I get the value 1 which is not what I want.

I don't understand why this is happening. Can someone please help.
Make sure a and b is read successfully.
1
2
3
4
5
6
7
8
if (cin >> a >> b)
{
	cout << "expression(a,b) = " <<expression(a, b) << endl;
}
else
{
	cout << "Invalid input!" << endl;
}
I still get 1 using this :/
You enter 0 and 20 as the values for a and b?

Is the return value of expression only depending on the two arguments?
yep!

well, expression returns
return sqrt(a*a +b*b) *sum(20000);

sum(20000) is another user defined functions which then relies on another two user defined functions..all of which work perfectly well
Can you show sum and the two other functions, if you don't mind?
I can't Im sorry. But I evaluated all of the functions as above and have realised sum is the problem - it comes up with "nan"
Just for your own sanity, I'd modify your code to print out the values of a and b, just so you're sure they're correct:

1
2
3
4
5
6
7
8
if (cin >> a >> b)
{
	cout << "expression(" << a << "," << b << ") = " <<expression(a, b) << endl;
}
else
{
	cout << "Invalid input!" << endl;
}


Edit: Actually, what I'd really suggest is stepping through your code in a debugger, so that you can see exactly what's going on.
Last edited on
Topic archived. No new replies allowed.