please assist in the c swap code Problem 2 Write a program t
please assist in the c++ swap code.
Problem 2:
Write a program that will convert miles to kilometers and kilometers to miles. The main
function will prompt the user to input both a distance number and a choice of whether that number
is in miles to be converted to kilometers or kilometers to be converted to miles. The main function
must call a value returning function to convert the distance either to miles or kilometers. You may
use the following values for conversions.
1 kilometer = .621 miles 1 mile = 1.61 kilometers
Note: All messages must be printed out by your main function.
Sample Run:
1. Convert miles to kilometers
2. Convert kilometers to miles
3. Quit
Your choice? 1
Please input the miles to be converted: 120
120 miles = 193.2 kilometers
1. Convert miles to kilometers
2. Convert kilometers to miles
3. Quit
Your choice? 2
Please input the kilometers to be converted: 235
235 kilometers = 145.935 miles
1. Convert miles to kilometers
2. Convert kilometers to miles
3. Quit
Your choice? 5
Invalid input!
1. Convert miles to kilometers
2. Convert kilometers to miles
3. Quit
Your choice? 3
The program is finishing. Thank you for using the program!
Solution
Please follow the code and comments for description :
CODE :
#include <iostream> // required header files
using namespace std;
int main() // driver method
{
double km = 0.621, miles = 1.61, result; // required local variables
int choice;
while(true) { // iterate till the user exists
cout << \"1. Convert miles to kilometers.\" << endl; // prompt for the user
cout << \"2. Convert kilometers to miles.\" << endl;
cout << \"3. Quit.\" << endl;
cout << \"Your choice..? \";
cin >> choice; // get the choice
switch(choice) { // switch over the choice
case 1 : // for first case
double inMiles;
cout << \"Please input the miles to be converted : \"; // prompt
cin >> inMiles; // get the data
result = inMiles * miles; // calculate the result
cout << inMiles << \" miles = \" << result << \" kilometers.\" << endl; // message
break;
case 2 : // for second case
double inKm;
cout << \"Please input the kilometers to be converted : \"; // prompt
cin >> inKm; // get the data
result = inKm * km; // calculate the result
cout << inKm << \" kilometers = \" << result << \" miles.\" << endl; // message
break;
case 3 :
cout << \"The program is finishing. Thank you for using the program!\" << endl; // message
exit(0); // exit the code
default :
cout << \"Invalid input!\" << endl; // default case
}
}
return 0;
}
OUTPUT :
1. Convert miles to kilometers.
2. Convert kilometers to miles.
3. Quit.
Your choice..? 1
Please input the miles to be converted : 120
120 miles = 193.2 kilometers.
1. Convert miles to kilometers.
2. Convert kilometers to miles.
3. Quit.
Your choice..? 2
Please input the kilometers to be converted : 235
235 kilometers = 145.935 miles.
1. Convert miles to kilometers.
2. Convert kilometers to miles.
3. Quit.
Your choice..? 5
Invalid input!
1. Convert miles to kilometers.
2. Convert kilometers to miles.
3. Quit.
Your choice..? 3
The program is finishing. Thank you for using the program!
Hope this is helpful.

