Search code examples
c++referenceidentifierreference-parameters

Use of Undeclared Identifier "angle"?


I am making a program that converts rectangular coordinates into polar coordinates and whenever I go to run the program it tells me that the "angle" is undeclared even though I am sure I have declared it. As well I know that the program isn't returning anything, I just want to be able to run it for now.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

double random_float(double min, double max);
void rect_to_polar(double x, double y, double &distance, double &angle);

int main() {
    double x, y;
    x = random_float(-1, 1);
    y = random_float(-1, 1);

    rect_to_polar(x, y, distance, angle);
}

double random_float(double min, double max) {
    unsigned int n = 2;
    srand(n);
    return ((double(rand()) / double(RAND_MAX)) * (max - min)) + min;
}


void rect_to_polar(double x, double y, double &distance, double &angle) {
    const double toDegrees = 180.0/3.141593;

    distance = sqrt(x*x + y*y);
    angle = atan(y/x) * toDegrees;

}

Solution

  • You did not declare anything called angle in your main(), but still used the name angle there. Thus the error.

    You might want to read up on scopes.