-1

[My program will not loop and my I dont know what is wrong with my math for REC -> POL][1]

-If the character is a P/p, the two floating point numbers should be interpreted as a set of Polar Coordinates and the program should first calculate the Rectangular Coordinate equivalent and then display both the Rectangular Coordinate and Polar Coordinate values.

-The program should continuously read sets of coordinate values until a Q/q (quit) is entered. -A set of coordinate values consist of a single character follow ed by two floating point numbers. The single character can be an R/r or a P/p. If the character is an R/r, the two floating point numbers should be interpreted as a set of Rectangular Coordinates and the program should first calculate the Polar Coordinate equivalent and then display both the Rectangular Coordinate and Polar Coordinate values.

If any other character other than R/r, P/p, or Q/q is entered, the program should display an error message. Note the program needs to do two “dummy” reads to disregard the two coordinate values following the illegal character. In the example below, after the illegal ‘d’ character is detected, the program had to read, and disregard, the 99.9 and 11.1 following the ‘d’. Likewise for the illegal ‘L’ input

int main() {
    double x;
    double y;
    double M;
    double th;
    char input;

    cin >> input;
    cin >> x >> y;

    while ((input != 'q') && (input != 'Q')) {
        if ((input == 'r') || (input == 'R')) {

            M = sqrt((x*x) + (y*y));
            th = atan2(y, x);
            x = M * cos(th);
            y = M * sin(th);
            th = th * (180 / M_PI);

            cout << "POL -> REC: REC: X = " << x << " Y = " << y << " POL: M = " << M << " A = " << th << endl;
            cin >> input >> x >> y;

        }

        if ((input == 'p') || (input == 'P')) {

            th = atan2(y, x);
            M = sqrt((x*x) + (y*y));

            M = M * (180 / M_PI);
            th = th * (180 / M_PI);

            x = M * cos(th);
            y = M * sin(th);


            x = x * (M_PI / 180);
            y = x * (M_PI / 180);

            cout << "REC -> POL: REC: X = " << x << " Y = " << y << " POL: M = " << M << " A = " << th << endl;
            cin >> input >> x >> y;
        }

        if ((input != 'r') && (input != 'R') && (input != 'p') && (input != 'P')) {
            cout << "Format Error!" << endl;
            cin >> input >> x >> y;

        }
        cin >> input >> x >> y;

        return 0;
    }
}
François Andrieux
  • 28,148
  • 6
  • 56
  • 87
  • 3
    Fixing the indentation would go a long way into making your code easier to read, and problems easier for spot. Edit : I've fixed it for you, and now clearly `return 0;` is inside your loop body. – François Andrieux Jul 15 '20 at 19:39
  • Now is probably a very good time to learn how to use a *debugger*. With a debugger you can step through your code, statement by statement, while monitoring variables and see how their values change. Though in this case, consistent indentation (as already mentioned) and some [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging) should be enough. – Some programmer dude Jul 15 '20 at 19:40
  • 1
    Seems like you always do an extra `cin >> input >> x >> y;` per iteration. Every branch does one, then at the end of the loop, you do it again. – François Andrieux Jul 15 '20 at 19:41

2 Answers2

2

The return 0; at the end of the loop is the culprit. You can clean it up completely, it is not needed in the main function

stefaanv
  • 14,072
  • 2
  • 31
  • 53
0

return 0; is in your if loop. By moving return 0; below the } and inbetween the last } it should run.

You should also consider not using using namespace std;. Read more about it, Here.

Geno C
  • 1,401
  • 3
  • 11
  • 26