Thread: Math function deg/rad query

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by GeorgeV View Post
    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.

  2. #17
    Registered User
    Join Date
    Aug 2007
    Posts
    11
    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.

  3. #18
    Registered User
    Join Date
    Aug 2007
    Posts
    11
    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.)

  4. #19
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by GeorgeV View Post
    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?

  5. #20
    Registered User
    Join Date
    Aug 2007
    Posts
    11
    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;
    }

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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, ...

  8. #23
    Registered User
    Join Date
    Aug 2007
    Posts
    11
    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

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM