Having trouble running a do while with a if else statement. I'm getting an error saying the "variable 'distanceM' is being used without being initialized."

int main ()
{
	const double PI = 3.141592;
	double xM, yTheta, distanceM, thetaRad, thetaDeg, xRad, yRad, X, Y;
	char choice;

	cout << "This program converts a set of polar coordinates ( M , theta in degrees ) to" << endl;
	cout << "their rectangular equivalents ( x, y), or a set of of rectangular coordinates" << endl;
	cout << "to their polar equivalents." << endl;

	cout << "\nEnter the first coordinate ( x or M): ";
	cin >> xM;
	cout << "Enter the second coordinate ( y or theta ): ";
	cin >> yTheta;

	cout << "Enter r or R if these are Rectangular";
	cout << "\nor p or P if they are polar: ";
	cin >> choice;

	do
	{
		if ( 'r' == choice)
		{
			distanceM = sqrt((pow(xM,2))+(pow(yTheta,2)));
			thetaRad = atan(yTheta/xM);
			thetaDeg = (thetaRad) * (180/PI);

			cout << "\nThe Polar Coordinates ( M, theta ) are ( "<< fixed << setprecision(2) << distanceM <<" , "<< thetaDeg <<" )";
		}
		else if ( 'p' == choice)
		{
			xRad = (yTheta)*(PI/180);
			yRad = (yTheta)*(PI/180);
			X = distanceM*(cos(xRad));
			Y = distanceM*(sin(yRad));

			cout << "\nThe Rectangular Coordinates ( x, y ) pair is ( "<< fixed << setprecision(2) << X <<" , "<<Y<<" )";
		}
		else 
			break;
	} while(1);

	getchar ();
	return (0);
}

Can anyone help me out?

Recommended Answers

All 3 Replies

This program doesn't calculate distancem when p is pressed. Try moving line 24 outside if statement.

Thank you i jus noticed my error distanceM on line 34 & 35 is supposed to be variable Xm. Now im getting an issue though with my do while loop it won't break

You are reading input only once, it should be in loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.