SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Functions For All Subtasks
Exercise:
Programming Projects
Question:9 | ISBN:9780321531346 | Edition: 7

Question

Write a program that reads in a weight in pounds and ounces and outputs the equivalent 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 computation for new input

values until the user says he or she wants to end the program. There are 2.2046 pounds in a

kilogram, 1,000 grams in a kilogram, and 16 ounces in a pound.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

//C++ function headers
#include <iostream> 
#include <cmath> 
using namespace std;  

//Prototypes of three functions
void inputFunction(double &poundsWeight, double &ouncesWeight);
void calculateWeight(double poundsWeight, double ouncesWeight, double &kilogramsWeight, double &gramsWeight);
void outputFunction(double poundsWeight, double ouncesWeight, double kilogramsWeight, double gramsWeight);

//Define constant values to variables
const double KILOGRAMS = 2.2046;
const double GRAMS = 1000;
const double OUNCES = 16;
//main method 
int main() 
{    

	double poundsWeight,ouncesWeight;

	double kilogramsWeight,gramsWeight; 
	char inputOption; 


	do 
	{ 
        //Call three functions
		inputFunction(poundsWeight, ouncesWeight);
        calculateWeight(poundsWeight, ouncesWeight, kilogramsWeight, gramsWeight);
        outputFunction(poundsWeight, ouncesWeight, kilogramsWeight, gramsWeight);
        //lets the user repeat this computation for new input values
        // until the user says he or she wants to end the program
		cout << "Repeat this computation for new input values?(Y/N): ";
		cin >> inputOption;
		cout << endl;
	}while(inputOption == 'Y' || inputOption == 'y');
	system("pause");
	return 0;
} 
//Read a valid input as weight in pounds and ounces
void inputFunction(double &poundsWeight, double &ouncesWeight)
{
	cout << "Enter pounds weight: "; 
	cin >> poundsWeight; 
	while(poundsWeight < 0)
	{
		cout << "Enter a valid number of pounds weight: ";
		cin >> poundsWeight; 
	} 
	cout << "Enter ounces weight: "; 
	cin >> ouncesWeight; 
	while(ouncesWeight < 0 || ouncesWeight >= OUNCES)
	{
		cout << "Enter a valid number of ounces weight: ";
		cin >> ouncesWeight; 
	} 
	cout << endl;
} 
//Calculate weight in kilograms and grams from the input values
void calculateWeight(double poundsWeight, double ouncesWeight, double &kilogramsWeight, double &gramsWeight)
{   
	
	double totalPoundsWeight;
	totalPoundsWeight = poundsWeight + ouncesWeight / OUNCES;
	kilogramsWeight =  totalPoundsWeight / KILOGRAMS;
	gramsWeight = kilogramsWeight * GRAMS;
	kilogramsWeight = floor(kilogramsWeight);
    gramsWeight = floor(gramsWeight - kilogramsWeight * GRAMS + 0.5);
} 
//Display output results on screen
void outputFunction(double poundsWeight, double ouncesWeight, double kilogramsWeight, double gramsWeight)
{
	cout <<"There are "<< poundsWeight 
    << " pounds and " << ouncesWeight 
    << " ounces are equal to " 
    << kilogramsWeight << " kilograms and " 
    << gramsWeight << " grams approximately." 
    << endl;

} 

Executed Output:

Enter pounds weight: 2000                                                                                                            
Enter ounces weight: 10

There are 2000 pounds and 10 ounces are equal to 907 kilograms and 478 grams approximately.                                          
Repeat this computation for new input values?(Y/N): Y

Enter pounds weight: 10000                                                                                                           
Enter ounces weight: 10
 
There are 10000 pounds and 10 ounces are equal to 4536 kilograms and 254 grams approximately.                                        
Repeat this computation for new input values?(Y/N): y 

Enter pounds weight: 100                                                                                                             
Enter ounces weight: 10 

There are 100 pounds and 10 ounces are equal to 45 kilograms and 643 grams approximately.                                            
Repeat this computation for new input values?(Y/N): N  

 

0 0

Discussions

Post the discussion to improve the above solution.