// strctfun.cpp -- functions with a structure argument
#include <iostream>
#include <cmath>
using namespace std;

// structure templates
struct Polar
{
    double distance;// distance from origin
    double angle;   // direction from origin
};
struct rect
{
    double x;       // horizontal distance from origin
    double y;       // vertical distance from origin
};

// prototypes
Polar rect_to_polar(rect xypos);
void show_polar(Polar dapos);

int main()
{
    rect rplace;
    Polar pplace;

    cout << "Enter the x and y values: ";
    while (cin >> rplace.x >> rplace.y)  // slick use of cin
    {
        pplace = rect_to_polar(rplace);
        show_polar(pplace);
        cout << "Next two numbers (q to quit): ";
    }
    return 0;
}

// convert rectangular to polar coordinates
Polar rect_to_polar(rect xypos)
{
    Polar answer;

    answer.distance =
        sqrt( xypos.x * xypos.x + xypos.y * xypos.y);
    answer.angle = atan2(xypos.y, xypos.x);
    return answer;      // returns a Polar structure
}

// show polar coordinates, converting angle to degrees
void show_polar (Polar dapos)
{
    const double Rad_to_deg = 57.29577951;

    cout << "distance = " << dapos.distance;
    cout << ", angle = " << dapos.angle * Rad_to_deg;
    cout << " degrees\n";
}

