Using a do/while loop with a switch statement.

This is definitely a homework problem, so hopefully I don't get some ToS breach warning, but I can't seem to find any help in reference as to why I keep having problems. I'm not looking for a solution or hand out, but moreso explanation as to why the loop goes berserk. I can't seem to find any explanation on the subject of loops and switch statements to clarify what I'm doing wrong.

Whatever I do, it seems to either,

A. repeat infinitely crazy when I enter a non-numeric character at the end, or
B. refuses to exit when I enter 1.

As of now it falls under B. I'm not sure if it's how I'm initializing the variable for the switch, since I do get notifications in some cases that I haven't initialized the switch variable. Or perhaps it's a restriction on the nature of the switch statement itself. I'm not sure.

Here's the code:

#include<iostream> // Required for cout.
#include<cmath> // Required for math computations functions.

using namespace std;

const double PI = acos(-1.0);

int main()
{
// Declare objects.
double X, Y, magnitude, angle, angle_deg;
char reply;

// Destination for loop.
do
{

// Prompt user for input
cout << "Please enter a value for x: " <<endl;
cin >> X;
cout << "Now enter a value for y, please: " <<endl;
cin >> Y;

// Compute magnitude.
magnitude = sqrt(X*X + Y*Y);

// Determine angle.
angle = atan2(Y,X);

// Converting radians to degrees.
angle_deg = angle*(180/PI);

// Print magnitude and angle.
cout << "The magnitude of the vector is: "
<< magnitude << endl;
cout << "The angle of the vector in degrees is: "
<< angle_deg << endl;
cout << "The angle measured in radians is: "
<< angle << endl;

// Prompt to either continue or exit program.
cout << "Evaluate another vector?" <<endl
<<"Enter 1 and ENTER to exit or any key and ENTER to continue:" <<endl;
cin >> reply;

switch (reply) // Switch statement.
{
case 1: cout << "Goodbye!" <<endl;
break;
default:
continue; // Returns to loop.
}
break;
}
while(reply !=1);
// Exit program.
return(0);
}
change the case statement to case '1':

in the switch control structure if you are comparing chars you need to include the 'apostrophe, otherwise the compiler assumes the case to be a number.
Wow thanks. I can't believe I overlooked that. That definitely fixed the problem. Thanks again!
Also the cmath header file has built in constants for Pi, E etc. You can use M_PI directly.

Consider using a bool variable Quit in conjunction with a while loop to control exiting of the program.
Topic archived. No new replies allowed.