In: Computer Science
My code is working fine but after selecting an option and at the end I want user could enter another option again without going back to menu. However, in my code when I enter another option, it prints the whole menu again then I can select option.Could anyone help me on that?
#include "stdafx.h"
#include 
#include
#include
using namespace std;
const double pi = 3.14159265358979323846;
const double deg_to_rad = (pi / 180);
int main()
{
        int option;
        
        do
        {
                cout << "Enter the Desired Option to Perform: "    // options are initialized.
                        << "\n1 for Distance Calculations "
                        << "\n2 for Temperature Conversion "
                        << "\n3 for Angle Conversion "
                        << "\n-1 for exiting the menu " << endl;
                cin >> option;
                switch (option)       //start of switch       
                                                          // switch function is used to select options.
                {
                case 1:
                        cout << "Option 1 is selected" << endl;          // prints out which option was selected.
                        int x1, x2, y1, y2, x, y;
                        double distance;
                        cout << " Enter the 1st point in x and y: ";      // option 1 computes the distance between x and y points.
                        cin >> x1 >> y1;
                        cout << " Enter the 2nd point in x and y: ";
                        cin >> x2 >> y2;
                        x = x1 - x2;
                        y = y2 - y1;
                        cout << "Your first point is: " << "(" << x1 << "," << y1 << ")"
                                << "\n Your second point is: " << "(" << x2 << "," << y2 << ")"
                                << "\nX value is: " << x
                                << "\nY value is:" << y
                                << "\nDistance is: " << (distance = sqrt(x*x + y*y)) << endl;
                        cout << "Enter another option" << endl;
                        cin >> option;
                        break;
                case 2:
                        cout << "Case 2 is selected" << endl;
                        
                                double f, c;
                                char tempc;
                                cout << "Enter F for Fahrenheit or C for Celcius for intput type:";    //asks the user for temperature.
                                cin >> tempc;             //option for converting celsius into fahernheit
                                if (tempc == 'c')
                                {
                                        cout << "Enter the temperature in Celsius: ";
                                        cin >> c;
                                        f = (1.8 * c) + 32.0;   //temperature conversion formula
                                        cout << "\nTemperature in degree Fahrenheit: " << f << " F" << endl;
                                }
                                else if (tempc == 'f')    //option for converting Fahrenheit into Celsius
                                {
                                        cout << "Enter the temperature in Fahrenheit: ";
                                        cin >> f;
                                        c = (f - 32) / 1.8; //temperature conversion formula
                                        cout << "\nTemperature in degree Celsius: " << c << " C" << endl;
                                }
                                else
                                        cout << "Error Wrong Input." << endl;
                         // end of if statement.
                        break;
                case 3:
                
                        cout << "Case 3 is selected" << endl;
                        double angle, sine, cosine, tangent;
                        cout << "Enter an angle (in degree): ";       // asks the user for an angle in radians.
                        cin >> angle;
                        cout << "\nThe " << angle << " degree angle in radians: " << (deg_to_rad*angle) << endl;
                        sine = sin(angle*deg_to_rad);
                        cosine = cos(angle*deg_to_rad);
                        cout << setprecision(4) << fixed;   // displays the sine,cosine, and tangent of the angle
                        cout << "Sine : " << sine << endl;
                        cout << "Cosine: " << cosine << endl;
                        
                        if (fabs(cos(angle*deg_to_rad)) < 0.001)
                                cout << "Cosine of the angle is almost zero, Tangent does not exist." << endl;
                        else
                                cout << "Tangent value of " << angle << "Degree Angle is " << tan(angle*deg_to_rad) << endl;
                
                break;
                case 4:
                        cout << "Exit Case is selected" << endl;
                        break;
                
                
                
                } // end of switch
                cout << "Enter other option:";
                
                cin >> option;
                
        } while (option != -1);
        return 0;
}
The Corrected Code is attached below:-
#include <math.h>
#include <stdio.h>
#include <iostream>
using namespace std;
const double pi = 3.14159265358979323846;
const double deg_to_rad = (pi / 180);
int main()
{
int option;
cout << "Enter the Desired Option to Perform: " // options
are initialized.
<< "\n1 for Distance Calculations "
<< "\n2 for Temperature Conversion "
<< "\n3 for Angle Conversion "
<< "\n-1 for exiting the menu " << endl;
do
{
  
cin >> option;
switch (option) //start of switch   
// switch function is used to select options.
{
case 1:
cout << "Option 1 is selected" << endl; // prints out
which option was selected.
int x1, x2, y1, y2, x, y;
double distance;
cout << " Enter the 1st point in x and y: "; // option 1
computes the distance between x and y points.
cin >> x1 >> y1;
cout << " Enter the 2nd point in x and y: ";
cin >> x2 >> y2;
x = x1 - x2;
y = y2 - y1;
cout << "Your first point is: " << "(" << x1
<< "," << y1 << ")"
<< "\n Your second point is: " << "(" << x2
<< "," << y2 << ")"
<< "\nX value is: " << x
<< "\nY value is:" << y
<< "\nDistance is: " << (distance = sqrt(x*x + y*y))
<< endl;
cout << "Enter another option" << endl;
cin >> option;
break;
case 2:
cout << "Case 2 is selected" << endl;
  
double f, c;
char tempc;
cout << "Enter F for Fahrenheit or C for Celcius for intput
type:"; //asks the user for temperature.
cin >> tempc; //option for converting celsius into
fahernheit
if (tempc == 'c')
{
cout << "Enter the temperature in Celsius: ";
cin >> c;
f = (1.8 * c) + 32.0; //temperature conversion formula
cout << "\nTemperature in degree Fahrenheit: " << f
<< " F" << endl;
}
else if (tempc == 'f') //option for converting Fahrenheit into
Celsius
{
cout << "Enter the temperature in Fahrenheit: ";
cin >> f;
c = (f - 32) / 1.8; //temperature conversion formula
cout << "\nTemperature in degree Celsius: " << c
<< " C" << endl;
}
else
cout << "Error Wrong Input." << endl;
// end of if statement.
break;
case 3:
  
cout << "Case 3 is selected" << endl;
double angle, sine, cosine, tangent;
cout << "Enter an angle (in degree): "; // asks the user for
an angle in radians.
cin >> angle;
cout << "\nThe " << angle << " degree angle in radians: " << (deg_to_rad*angle) << endl;
sine = sin(angle*deg_to_rad);
cosine = cos(angle*deg_to_rad);
cout << setprecision(4) << fixed; // displays the
sine,cosine, and tangent of the angle
cout << "Sine : " << sine << endl;
cout << "Cosine: " << cosine << endl;
  
if (fabs(cos(angle*deg_to_rad)) < 0.001)
cout << "Cosine of the angle is almost zero, Tangent does not
exist." << endl;
else
cout << "Tangent value of " << angle << "Degree
Angle is " << tan(angle*deg_to_rad) << endl;
  
break;
case 4:
cout << "Exit Case is selected" << endl;
break;
  
  
  
} // end of switch
cout << "Enter other option:";
  
cin >> option;
  
} while (option != -1);
return 0;
}
Output:-
