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

Thank you all. my other code is working fine. It was my IDE issue. This is...
Thank you all. my other code is working fine. It was my IDE issue. This is the new code i am writing for the below question. please help as you always do: a=int(input('Input a: ')); b=int(input('Input b: ')); c=int(input('Input c: ')); r=int(input('Input r: ')); s=int(input('Input s: ')); t=int(input('Input t: ')); x=2; min_rst = min(r,s,t); while(1): if((x-a)%r==0): if((x-b)%s==0): if((x-c)%t==0): break; x=x+1; print('The required number is: ',x); Questions: Write a Python program called crt.py that finds the value of x that satisfies...
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 *...
This code needs to run a working Reverse Polish Calculator, but when I input my commands...
This code needs to run a working Reverse Polish Calculator, but when I input my commands it only pops my inputs never push them. So my answer is always zero. Here is my code. #include <stdio.h> #include <stdlib.h> #include <stdbool.h> struct Node { int element; struct Node *next; }; //Global variable struct Node *top = NULL; void push(int ele) { struct Node *newNode; newNode = (struct Node *) malloc(sizeof(struct Node)); newNode->element = ele; newNode->next = top; top = newNode; }...
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT