7 Write a program that will read in a weight in pounds and o

7. Write a program that will read in a weight in pounds and ounces and will output the equiv- alent weight 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 com- putation for new input values until the user says he or she wants to end the program. There are 2.2046 pounds in a kilogram, 1000 grams in a kilogram, and 16 ounces in a pound. 8. Write a program like that of the previous exercise that converts from kilograms and grams into pounds and ounces. Use functions for the subtasks. 9. (You should do the previous two programming projects before doing this one.) Write a program that combines the functions of the previous two programming projects. The pro- gram asks the user if he or she wants to convert from pounds and ounces to kilograms and grams or from kilograms and grams to pounds and ounces. The program then performs the desired conversion. Have the user respond by typing the integer 1 for one type of conver- sion and 2 for the other. The program reads the user’s answer and then executes an if- else statement. Each branch of the if-else statement will be a function call. The two functions called in the if-else statement will have function definitions that are very simi- lar to the programs for the previous two programming projects. Thus, they will be fairly complicated function definitions that call other functions in their function bodies. 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

Solution

#include using namespace std; const double pPerKg = 2.2046; const int oPerP = 16; const int gmPerKg = 1000; void getInput(double& pounds, double& ounces); void convertToGrams(double poundss, double ounces, double& kilograms, double& grams); void giveOutput(double kilograms, double grams); int main() { double pounds, ounces, kilograms, grams; char ans; do { getInput(pounds, ounces); convertToGrams(kilograms, grams, pounds, ounces); giveOutput(kilograms, grams); cout << \"Do you want to do another calculation?\ \" << \"Press y for yes, n for no,\ \" << \"and then press return: \"; cin >> ans; cout << endl; } while (ans == \'y\' || ans == \'Y\'); return 0; } void getInput(double& pounds, double& ounces) { cout << \"Enter the weight of the object in pounds and ounces\ \"; cout << \"pounds: \"; cin >> pounds; cout << \"ounces: \"; cin >>ounces; } void convertToGrams(double pounds, double ounces, double& kilograms, double& grams) { double total kg = return pounds / 2.2046; kilograms = floor(kg); // extract just entire portion of kg grams = (kg - kilograms)*1000; // the substraction will be remaining grams } void giveOutput(double kilograms, double grams) { cout.setf(ios::fixed); cout.precision(0); cout << \"The object weighs \" << kilograms << \" kilograms and \" << grams << \" grams\ \"; }
7. Write a program that will read in a weight in pounds and ounces and will output the equiv- alent weight in kilograms and grams. Use at least three functions:

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site