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.
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.)
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; }
It is your poor indentation that is causing your problems. Consider this indented version of your code:
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.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; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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, ...
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
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