Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This code is basically a menu that allows the user to select a conversion, and t

ID: 3844866 • Letter: T

Question

This code is basically a menu that allows the user to select a conversion, and then allows the user to enter a number to convert to any units, however my problem is that I dont know how to make the program repeat itself with a loop, and I also want to make an option that allows the user to exit the program, can you please make those additions for me? (C++)

#include <iostream>

using namespace std;

int main ()

{

int n;

int a = 0;

double fah;

double cel;

double cent;

double inch;

double feet;

double meter;

double ms;

double kmh;

double b = 0;

  

  

  

cout << "1. Celsius to Fahrenheit" << endl;

cout << "2. Fahrenheit to Celsius" << endl;

cout << "3. Inches to Centimeters" << endl;

cout << "4. Centimeters to Inches" << endl;

cout << "5. Meters to Feet" << endl;

cout << "6. Feet to Meters" << endl;

cout << "7. Km/h to m/s" << endl;

cout << "8. m/s to Km/h" << endl;

cout << "9. Sine" << endl;

cout << "10. Power" << endl;

cout << "11. Factorial" << endl;

cout << "12. Exit" << endl;

cout << " Please select from the menu " << endl;

  

cin >> a;

  

cout << " Please enter a value for conversion : " << endl;

  

  

switch (a)

{

case 1 :

cin >> b;

fah = ((9/5)*b) + 32;

cout << b << "in fahrenheit is : " << fah << endl;

  

case 2 :

cin >> b;

cel = ((5/9)*(b - 32));

cout << b << " in celsius is : " << cel << endl;

  

case 3 :

cin >> b;

cent = (2.54*b);

cout << b << " in centimeters is : " << cent << endl;

  

case 4:

cin >> b;

inch = (0.39*b);

cout << b << " in inches is : " << inch << endl;

  

case 5:

cin >> b;

feet = (3.28*b);

cout << b << " in feet is :" << feet << endl;

  

case 6:

cin >> b;

meter = (0.3*b);

cout << b << " in meters is : " << meter << endl;

  

case 7:

cin >> b;

ms = (0.277*b);

cout << b << " in m/s is : " << ms << endl;

  

case 8:

cin >> b;

kmh = (3.6*b);

cout << b << " in km/h is : " << kmh << endl;

  

case 9 :

cin >> b;

int i;

double sum, t;

  

b=b*3.14159/180;

t=b;

sum=b;

  

for(i=1;i<=n;i++)

{

t=(t*(-1)*b*b)/(2*i*(2*i+1));

sum=sum+t;

}

cout << " The sine is : " << sum << endl;

  

  

case 10:

cin >> b;

int exp = 2;

float result = 1;

  

while (exp!= 0) {

result *= b;

--exp;

}

cout << " The power is : " << result << endl;

  

}

  

  

if (a == 11)

{

cin >> b;

double fact = 0;

for (int i = 0; i <= b; i++)

  

if (i == 0)

fact = 1;

else

fact = fact * i;

cout << " The factorial is : " << fact << endl;

  

}

  

  

  

}

  

  

Explanation / Answer

Modified your code according to your requirements.

1. Each case in the switch should have a break statement, otherwise control will flow into the next case. So fixed that by putting break for each of the case.

2. Include case 12 as well which does nothing... but the do while loop will terminate when menu 12 is selected.

3. Included a default case that will handle all invalid menu choices

Please do not forget to rate the answer if it helped. Thank you very much.

#include <iostream>
using namespace std;
int main ()
{
int n;
int a = 0;
double fah;
double cel;
double cent;
double inch;
double feet;
double meter;
double ms;
double kmh;
double b = 0;


do {

cout << "1. Celsius to Fahrenheit" << endl;
cout << "2. Fahrenheit to Celsius" << endl;
cout << "3. Inches to Centimeters" << endl;
cout << "4. Centimeters to Inches" << endl;
cout << "5. Meters to Feet" << endl;
cout << "6. Feet to Meters" << endl;
cout << "7. Km/h to m/s" << endl;
cout << "8. m/s to Km/h" << endl;
cout << "9. Sine" << endl;
cout << "10. Power" << endl;
cout << "11. Factorial" << endl;
cout << "12. Exit" << endl;
cout << " Please select from the menu " << endl;

cin >> a;

cout << " Please enter a value for conversion : " << endl;


switch (a)
{
case 1 :
cin >> b;
fah = ((9/5)*b) + 32;
cout << b << " in fahrenheit is : " << fah << endl;
break;

case 2 :
cin >> b;
cel = ((5/9)*(b - 32));
cout << b << " in celsius is : " << cel << endl;
break;

case 3 :
cin >> b;
cent = (2.54*b);
cout << b << " in centimeters is : " << cent << endl;
break;

case 4:
cin >> b;
inch = (0.39*b);
cout << b << " in inches is : " << inch << endl;
break;

case 5:
cin >> b;
feet = (3.28*b);
cout << b << " in feet is :" << feet << endl;
break;

case 6:
cin >> b;
meter = (0.3*b);
cout << b << " in meters is : " << meter << endl;
break;

case 7:
cin >> b;
ms = (0.277*b);
cout << b << " in m/s is : " << ms << endl;
break;

case 8:
cin >> b;
kmh = (3.6*b);
cout << b << " in km/h is : " << kmh << endl;
break;
case 9 :

cin >> b;
int i;
double sum, t;

b=b*3.14159/180;
t=b;
sum=b;

for(i=1;i<=n;i++)
{
t=(t*(-1)*b*b)/(2*i*(2*i+1));
sum=sum+t;
}
cout << " The sine is : " << sum << endl;

break;
case 10:
{
cin >> b;
int exp = 2;
float result = 1;

while (exp!= 0) {
result *= b;
--exp;
}
cout << " The power is : " << result << endl;
}
break;

case 11:
{
cin >> b;
double fact = 0;
for (int i = 0; i <= b; i++)
{
if (i == 0)
fact = 1;
else
fact = fact * i;
}
cout << " The factorial is : " << fact << endl;
}
break;

case 12:
break; //do nothing

default:
cout<<"Invalid menu choice! "<<endl;
}

}while(a != 12);
cout<<"Program done"<<endl;
}

output

1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Inches to Centimeters
4. Centimeters to Inches
5. Meters to Feet
6. Feet to Meters
7. Km/h to m/s
8. m/s to Km/h
9. Sine
10. Power
11. Factorial
12. Exit
Please select from the menu
11
Please enter a value for conversion :
4
The factorial is : 24
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Inches to Centimeters
4. Centimeters to Inches
5. Meters to Feet
6. Feet to Meters
7. Km/h to m/s
8. m/s to Km/h
9. Sine
10. Power
11. Factorial
12. Exit
Please select from the menu
20
Please enter a value for conversion :
Invalid menu choice!
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Inches to Centimeters
4. Centimeters to Inches
5. Meters to Feet
6. Feet to Meters
7. Km/h to m/s
8. m/s to Km/h
9. Sine
10. Power
11. Factorial
12. Exit
Please select from the menu
10
Please enter a value for conversion :
2
The power is : 4
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Inches to Centimeters
4. Centimeters to Inches
5. Meters to Feet
6. Feet to Meters
7. Km/h to m/s
8. m/s to Km/h
9. Sine
10. Power
11. Factorial
12. Exit
Please select from the menu
12
Please enter a value for conversion :
Program done