```
#include <iostream>
#include <iomanip> //for formatted output
#include <string> //for strings
using namespace std;
//named constants
const double CENTIMETERS_PER_INCH = 2.54;
const int INCHES_PER_YARD = 36;
const int INCHES_PER_FOOT = 12;
//function prototypes
double toCentimeters(int, int, int);
void fromCentimeters(double, int&, int&, int&);
char inputData(int, string&, double&, int&, int&, int&);
void outputData(int, string, double, int, int, int);
int main ()
{
//declare variables
int yards, feet, inches;
double centimeters;
char convType;
string name;
string header = " # Last, First Name Yards Feet Inches Centimeters ";
int numConv; //# of conversions
double total = 0.0; //total # of cm
// 1. Prompt the user how many conversions are needed.
cout << "Enter the number of conversions: ";
cin >> numConv;
for(int i = 1; i <= numConv; i++, total += centimeters)
{
convType = inputData(i, name, centimeters, yards, feet, inches);
if (convType == 'f') //To centimeters .
centimeters = toCentimeters(yards, feet, inches);
else //From centimeters
fromCentimeters(centimeters, yards, feet, inches);
cout << header << endl;
outputData(i, name, centimeters, yards, feet, inches);
} //end of the loop by # numConv
// The last two rows in the table should be: Total and Average.
fromCentimeters(total, yards, feet, inches);
outputData(numConv+1, "Total", total, yards, feet, inches);
fromCentimeters(total/numConv, yards, feet, inches);
outputData(numConv+2, "Average", total/numConv, yards, feet, inches);
return 0;
}
//---------------------------------------------------------------------------------------------------------------
char inputData(int convNum, string& name, double& centimeters, int& yards, int& feet,int& inches)
{
char convType;
string temp;
//2. Repeatedly prompt the user for the Last, First Name and length.
// For each prompt of the user perform the input validity checks using sentinels and either while or do loop.
cout << "Please enter Last name for " << convNum <<" student: \n";
cin >> name;
cout << "Please enter First name for " << convNum <<" student: \n";
cin >> temp;
name = name + ", " + temp;
// Prompt the user which conversion is needed: to or from centimeters.
do {
cout << "\nEnter a letter f if conersion is from yards feet inches to centimeters";
cout << "\nEnter a letter c if conersion is from centimeters to yards feet inches\n ";
cin >> convType;
}
while(convType != 'f' && convType != 'c');
if (convType == 'f')
//2. To centimeters .
do {
cout << "Enter three integers, one for yards, one for feet and "
<< "one for inches\n separated by space for " << convNum <<" student: \n";
cin >> yards >> feet >> inches;
cout << endl;
} while(yards<0|| feet<0|| inches <0 ||yards > 10); // 0 and 10yd are sentinels
//3. From centimeters. The program prompts the user to enter the length in centimeters,
else
do {
cout << "Enter the length in centimeters for " << convNum <<" student: \n";
cin >> centimeters;
cout << endl;
} while(centimeters<0 || centimeters>300); // 0 and 300cm are sentinels
return convType;
}
//----------------------------------------------------------------------------------------------------------------------
double toCentimeters(int yards, int feet, int inches)
{
int totalInches = INCHES_PER_YARD * yards + INCHES_PER_FOOT * feet + inches;
return CENTIMETERS_PER_INCH * totalInches;
}
//----------------------------------------------------------------------------------------------------------------------
void fromCentimeters(double centimeters, int& yards, int& feet, int& inches)
{
int remainder;
inches = static_cast<int> (centimeters / CENTIMETERS_PER_INCH + 0.5);
// we added 0.5 to round to the nearest inch, say <int>(0.7 + 0.5) is 1
yards = inches / INCHES_PER_YARD ;
remainder = inches %= INCHES_PER_YARD;
feet = remainder / INCHES_PER_FOOT;
inches = remainder % INCHES_PER_FOOT;
}
//--------------------------------------------------------------------------------------------------------------------
//4. The output should be printed as formatted table with the header: Last, First Name, length in yards, feet, inches
// in that order and length in centimeters.
void outputData(int studentNumber, string name, double cm, int yd, int ft, int in)
{
cout << left << setw(3) << studentNumber;
cout << left << setw(20)<< name; //name
cout << right << setw(5) << yd;
cout << right << setw(5) << ft;
cout << right << setw(7) << in;
cout << right << setw(10) << cm ;
cout << endl;
}
```

-1

*Edited
by Nick Evan*: Added code-tags