dimanche 30 août 2015

Using If and Switch statements together in a program

I am a student and I am trying to make a very simple program that displays the quadrant of an angle that is entered in by the user.

However I want it to display that the angle is on the X axis or Y axis if the user enters the angle as 0,90,180 or 270.

I did put these 4 conditions in an if statement but the code is not terminating there and I am also getting the quadrant number corresponding to that angle. How do I terminate the code for those 4 angles after the if statement?

#include <iostream.h>
void main()

{
int angle;

cout<<"Enter an angle: ";
cin>>angle;

if (angle==90) cout<<"The angle lies on the positive Y axis";
else if (angle==0) cout<<"The angle lies on the positive X axis";
else if (angle==180) cout<<"The angle lies on the negative X axis";
else if (angle==270) cout<<"The angle lies on the negative Y axis";


angle=(angle/90)+1;

switch(angle)
{
case 1: cout<<"First Quadrant"; break;
case 2: cout<<"Second Quadrant"; break;
case 3: cout<<"Third Quadrant"; break;
case 4: cout<<"Fourth Quadrant"; break;
default: cout<<"That is not a valid angle.";

}
}

Aucun commentaire:

Enregistrer un commentaire