Write a program that asks the user to enter a distance in meters. The program will then present the following menu of selections:
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program
Here is an example session with the program, using console input. The user’s input is shown in bold.
Enter a distance in meters: 500 [enter]
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program
Enter your choice: 1 [enter]
500 meters is 0.5 kilometers.
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program
Enter your choice: 3 [enter]
500 meters is 1640.5 feet.
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program
Enter your choice: 4 [enter]
Bye!
#include <iostream>
using namespace std;
int main() {
bool flag = true;
while (flag) {
int n;
printf("Enter distance in meter: ");
scanf("%d", &n);
printf("1. Convert to kilometers");
printf("2. Convert to inches");
printf("3. Convert to feet");
printf("4. Quit the program");
int choice;
scanf("%d", &choice);
switch(choice) {
case 1:
printf("%f", 0.001 * n);
break;
case 2:
printf("%f", 39,3701 * n);
break;
case 3:
printf("%f", 3,28084 * n);
break;
case 4:
flag = false;
break;
}
}
return 0;
}
Comments
Leave a comment