In: Computer Science
a C++ program that reads in weight in pounds and outputs the equivalent length in Kilograms and grams. Use at least three functions: one for input, one or more for calculating, and one for output. Include a loop that lets the user repeat this computation for new input values until the user says he or she wants to end the program. 1 pound (lb) is equal to 0.45359237 kilograms (kg). There are 1000 grams in a kilogram and 16 ounces in a pound. Its important that (lets the user repeat this computation for new input values until the user says he or she wants to end the program)!!
Program:
#include <iostream>
using namespace std;
int getInput() { // This method helps to take the input from user
int poundWeight;
// Take weight as input from user in pounds.
// Enter -1 to exit
cout << "Enter -1 to exit" << endl;
cout << "Enter weight in pounds : ";
cin >> poundWeight;
return poundWeight;
}
// This method returns weight in Kilograms for given pounds
float calculateWeight(int poundWeight) {
float weightInKilos = poundWeight * 0.45359237;
return weightInKilos;
}
void getOutput(int poundWeight) {
float weightInKilos = calculateWeight(poundWeight);
// Calculate weight in grams for the given Kilograms
float weightInGrams = weightInKilos * 1000;
// Printing output
cout << "Weight in Kilograms : " << weightInKilos << " Kilos"<< endl;
cout << "Weight in Grams : " << weightInGrams << " grams" << endl;
}
int main()
{
// Taking input from user
int inputWeight = getInput();
// Checking condition to exit the loop
while(inputWeight != -1) {
getOutput(inputWeight);
inputWeight = getInput();
}
return 0;
}
Output: