Question
Write a C++ program that will convert miles to kilometers and kilometers to miles. The user will indicate both a number (representing a distance) and
Write a C++ program that will convert miles to kilometers and kilometers to miles. The user will indicate both a number (representing a distance) and a choice of whether that number is in miles to be converted to kilo- meters or kilometers to be converted to miles. Each conversion is done with a value returning function. You may use the following conversions.
1 kilometer = .621 miles
1 mile = 1.61 kilometers
I have created the following code but i am struggling to call the functions and having the program actually work
#include "pch.h" #include
using namespace std; //function miles to kilometers void convertToKilometers(float miles, float& kilometers); void convertToMiles(float kilometers, float& miles);
int main() { float miles; float kilometers; int option;
do { cout << "1. Convert miles to kilometers. " << endl; cout << "2. Conver kilometers to miles. " << endl; cout << "3. End program. " << endl; cin >> option; if (option == 1) { cout << "Enter the number of miles: " << endl; cin >> miles; kilometers = convertToKilometers(miles, kilometers); cout << miles << "equals to " << kilometers << endl; } else if (option == 2) { cout << "Enter the number of kilometers: " << endl; cin >> kilometers; convertToMiles(kilometers, miles); cout << kilometers << "equals to " << miles << endl; } else if (option == 3) { cout << "This program has been closed thank you for using it!! " << endl; }
} while (option != 3);
return 0; }
float convertToKilometers(float miles, float kilometers) { kilometers = miles * 1.61; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started