Question

In: Computer Science

My code is working fine but after selecting an option and at the end I want...

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;
}

Solutions

Expert Solution

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:-


Related Solutions

this is my code I want the opposite i want to convert a postfix expression to...
this is my code I want the opposite i want to convert a postfix expression to infix expression #include <iostream> #include <string> #define SIZE 50 using namespace std; // structure to represent a stack struct Stack {   char s[SIZE];   int top; }; void push(Stack *st, char c) {   st->top++;   st->s[st->top] = c; } char pop(Stack *st) {   char c = st->s[st->top];   st->top--;   //(A+B)*(C+D)   return c; } /* function to check whether a character is an operator or not. this function...
Python I want to name my hero and my alien in my code how do I...
Python I want to name my hero and my alien in my code how do I do that: Keep in mind I don't want to change my code except to give the hero and alien a name import random class Hero:     def __init__(self,ammo,health):         self.ammo=ammo         self.health=health     def blast(self):         print("The Hero blasts an Alien!")         if self.ammo>0:             self.ammo-=1             return True         else:             print("Oh no! Hero is out of ammo.")             return False     def...
This is my code I want the average temp for the week to be printed when...
This is my code I want the average temp for the week to be printed when the user types : 'week' currently when the user types  'week' it only prints  Monday - Sunday and the average temp for each day. import java.util.Arrays; import java.util.ArrayList; import java.util.Scanner; public class weeklytemps {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);                  ArrayList Day = new ArrayList(Arrays.asList(    "Monday","Tuesday","Wednesday","Thurday","Friday","Saturday","Sunday")); // Stores days of the week...
Working with Python. I am trying to make my code go through each subject in my...
Working with Python. I am trying to make my code go through each subject in my sample size and request something about it. For example, I have my code request from the user to input a sample size N. If I said sample size was 5 for example, I want the code to ask the user the following: "Enter age of subject 1" "Enter age of subject 2" "Enter age of subject 3" "Enter age of subject 4" "Enter age...
i want to master reset my laptop but i dont get the "repair your computer option"...
i want to master reset my laptop but i dont get the "repair your computer option" even tho i press f8 and went to "advance boot" my laptop only has two options windows home and windows professional....windows professional is the only that works but needs an admin password any other way i can factory reset this laptop dell inspiron mini
My Javascript code isn't working (when i press calculate button) - what's wrong with it ?...
My Javascript code isn't working (when i press calculate button) - what's wrong with it ? Car Rental Enter Number of Rental Days: Select Car Type: onclick= "priceofcar = 50;"/> Compact onclick= "priceofcar = 60;"/> Economy onclick= "priceofcar = 70;"/> Intermediate Select Loss Damage Waiver onclick= "damagewaiver='yes'"/> Yes onclick= "damagewaiver='no'"/> No damagewaiver = boxDays.value * 25;} else if (damagewaiver == No) { damagewaiver = 0; }/> Select Roadside Issues Coverage: onclick= "issuescoverage='yes'"/> Yes onclick= "issuescoverage='no'"/> No issuescoverage = boxDays.value *...
Give me a working code in MATLAB for Crout Decomposition. The code must work totally fine...
Give me a working code in MATLAB for Crout Decomposition. The code must work totally fine and must break into 2 matrix L and U It must be in MATLAB The code used must use continue statement. If no continue statement is there in code it will be downvoted. Answer only if you know or else i will dislike badly
I want my answer typed.                                      I. What i
I want my answer typed.                                      I. What is presbyopia? Hyperopia? Myopia? 2. Which cranial nerves assesses eye function and acoustic function? 3. What is the Rinne test? Weber test? 4. Do you or anyone you know have any of the issues/symptoms/conditions mentioned in the video? Is it due to genetics or aging? How is it being managed?
Could you modify my code so it meets the following requirement? (Python Flask) I want the...
Could you modify my code so it meets the following requirement? (Python Flask) I want the user to register for account using email and password, then store that data into a text file. Then I want the data to be read when logging in allowing the user to go to home page. -------------Code-------------------- routes.py from flask import Flask, render_template, redirect, url_for, request, session import json, re app = Flask(__name__) '''@app.before_request def before_request(): if 'visited' not in session: return render_template("login.html") else:...
I want you guys to rewrite this lab procedure, and it is fine if it was...
I want you guys to rewrite this lab procedure, and it is fine if it was shorter than this. An air track was fitted with two photocell bridges, one on each side of the collision region. Each bridge was operating in a gate mode timer that allows the collection of time intervals before and after collision. To ensure that friction and gravity have minimal effects, the track was leveled. Before starting the experiment, we ensured that loss in velocity was...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT