/*File: convert2metric.cpp Created by: ??? Creation Date: ??? Synopsis: This pro
ID: 3852987 • Letter: #
Question
/*File: convert2metric.cpp
Created by: ???
Creation Date: ???
Synopsis:
This program reads in a length yards, feet and inches,
and converts to meters and centimeters.
*/
#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
// FUNCTION PROTOTYPE FOR read_us_length
// FUNCTION PROTOTYPE FOR convert2inches
// FUNCTION PROTOTYPE FOR write_metric_length
//DO NOT MODIFY ANY OF CODE procedure in MAIN()
int main()
{
int yards, feet, inches; // length in yards, feet and inches
int total_inches; // total length in inches
int meters, centimeters; // length in meters and centimeters
read_us_length(yards, feet, inches);
total_inches = convert2inches(yards, feet, inches);
convert2metric(total_inches, meters, centimeters);
write_metric_length(meters, centimeters);
return 0;
}
// DEFINE FUNCTION read_us_length HERE:
// DEFINE FUNCTION convert2inches HERE:
// DEFINE FUNCTION convert2metric HERE
// DEFINE FUNCTION write_metric_length HERE:
Explanation / Answer
// DEFINE FUNCTION read_us_length HERE
int read_us_length(int & yards, int & feet, int & inches)
{
cout << "Enter number of yards: ";
cin >> yards;
if (yards < 0)
{
cout << "Illegal negative value " << yards << " for yards." << endl;
exit(0);
}
cout << "Enter number of feet: ";
cin >> feet;
if (feet < 0)
{
cout << "Illegal negative value " << feet << " for feet." << endl;
exit(1);
}
cout << "Enter number of inches: ";
cin >> inches;
if (inches < 0)
{
cout << "Illegal negative value " << inches << " for inches." << endl;
exit(2);
}
}
// DEFINE FUNCTION convert2inches HERE:
int convert2inches(int yards, int feet, int inches)
{
int total_inches;
total_inches = yards * 36.0 + feet * 12.0 + inches;
return(total_inches);
}
// DEFINE FUNCTION convert2metric HERE
void convert2metric(const int total_inches, int & meters, int & centimeters)
{
centimeters = total_inches * 2.54;
meters = centimeters/100;
centimeters = centimeters % 100;
}
// DEFINE FUNCTION write_metric_length HERE:
void write_metric_length(const int meters, const int centimeters)
{
cout << "The length is " << meters << " meters and " << centimeters << " centimeters.";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.