# Math function deg/rad query

Printable View

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 08-22-2007
robatino
Quote:

Originally Posted by GeorgeV
Again, thanks for the help. I'm now using <cmath> instead, and at the moment I'm also using references. So this bit reads something like so:

Code:

```void height (float & x, float & y) {     x = x *(180/M_PI);     y = y *(180/M_PI); }```
But I'm having trouble calling the 'height' function properly. What is the correct syntax I should be using?

Code:

`  height(x, y);`
where x and y are the floats to be converted.
• 08-22-2007
GeorgeV
I thought it was that simple, but then I'm sure I did try that already.

Probably just missed off the ; or something stupid.

Cheers.
• 08-22-2007
GeorgeV
Ok, I did try that, and here's why it didn't work.

I get the error: "name lookup of 'y' changed for new ISO 'for' scoping" :(

(using Dev C++ by the way.)
• 08-22-2007
Bench82
Quote:

Originally Posted by GeorgeV
Ok, I did try that, and here's why it didn't work.

I get the error: "name lookup of 'y' changed for new ISO 'for' scoping" :(

(using Dev C++ by the way.)

The problem is somewhere else then. Could you paste the entire block of code that gave you the error?
• 08-23-2007
GeorgeV
Here's the full code, as it is at the moment. Doesn't matter what x and y values are, still produces the same error.

Code:

```#include <cstdlib> #include <iostream> #include <iomanip> #include <cmath> using namespace std; void height (double & x, double & y) { x =  x*(180/M_PI); y = y*(180/M_PI); } int main(void) { for (double x=0; x <= 60; x+=10) { for (double y=0; y <= 85; y+=5) cout << setw(12) << (tan(y)*x); height (x, y); cout << endl; }     system("PAUSE");     return EXIT_SUCCESS; }```
• 08-23-2007
laserlight
It is your poor indentation that is causing your problems. Consider this indented version of your code:
Code:

```#include <cstdlib> #include <iostream> #include <iomanip> #include <cmath> using namespace std; void height(double& x, double& y) {     x *= 180 / M_PI;     y *= 180 / M_PI; } int main() {     for (double x = 0; x <= 60; x += 10)     {         for (double y = 0; y <= 85; y += 5)             cout << setw(12) << (tan(y) * x);         height(x, y);         cout << endl;     }     system("PAUSE");     return EXIT_SUCCESS; }```
Now, it is clear that the height(x, y) call in main() uses a y that does not exist, as y exists within the scope of the inner for loop, but height() is called after that loop.
• 08-23-2007
robatino
Not to mention that you probably didn't intend to multiply both x and y by 180/M_PI in every iteration of the x loop, since the x loop itself suggests that x is supposed to take the values 0, 10, 20, ...
• 08-23-2007
GeorgeV
But, doesn't x also only exist within the scope of the outer for loop? Plus, height is called after this loop too.

Good point Robatino, no I did not.

Forgive my ignorance, and apparent lack of understanding of basic coding fundamentals, but could you point me on the right track to reolving these issues?

Oh, and I'll be sure to indent my code from now on :)
• 08-23-2007
robatino
If you need the loop variable to continue to be in scope after the loop, declare it before the loop, not inside it. Although in general variable scopes should be as small as possible, so declaring variables in the loop is a good idea when correctness allows.
Code:

```  double x;   for (x = 0; x <= 60; x += 10)   {     // code   }   // x still in scope now```
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12