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:6 | ISBN:9780321531346 | Edition: 7

Question

Write a program that reads in a length in feet and inches and outputs the equivalent length in meters and centimeters. 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 0.3048 meters in a foot, 100 centimeters in a meter, and 12 inches in a foot.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

#include <iostream> 
#include <cmath> 
#include <string> 
using namespace std; 
void inputData(float &feet,float &inches);
void calcFeetInches(float feet,float inches,float &meters,
                                         float &cm);
void output(float meters,float cm,float feet,float inches);

int main() 
{    
	 float feet, inches, meters, cm; 
     char answer; 
  do 
  { 
      cout << " Feet and inches to meters and centimeters" 
           << endl; 
		inputData(feet,inches);
	   calcFeetInches(feet,inches,meters,cm);
	  output(meters,cm,feet,inches);
     cout<<"To repeat calculation enter 'y' or 'Y'"<<endl;
	 cin>>answer;
   
    } while (answer == 'y'||answer=='Y'); 

return 0; 
}
void inputData(float &feet,float &inches)
{
       cout << "Feet: "; 
       cin >> feet; 
       cout << "Inches: "; 
       cin >> inches; 
}

void calcFeetInches(float feet,float inches,
float &meters,float &cm)
{   
    meters = feet* 0.3048 + (inches*0.0254); 
     cm=(feet*30.48+inches*2.54);
}

Output:

Feet and inches to meters and centimeters
Feet: 100
Inches: 11
There are 30.7594 meters, 3075.94 centimeters in 100 feet, 11 inches
To repeat calculation enter 'y' or 'Y':n

 

0 0

Discussions

Post the discussion to improve the above solution.