A measurement conversion program that converts interactively based on a database file of conversion constants. Class UnitMeasure holds the unit name, abbreviation, category (mass, distance, etc) and conversion factor from the standard as determined in the file.

What will be an ideal response?


```
const int MAX_UNITS = 5; // declare a static array of conversions

class UnitMeasure{

public:
UnitMeasure(){}
bool sameunit(const string&) const; // compares units
double getfactor() const; // accessor
private:
string name, abbrev, category;
double factor;

friend istream& operator>> (istream&, UnitMeasure&);
};
int main()
{
string filename;
UnitMeasure data[MAX_UNITS];
double measure;
string oldunit, newunit;
char another;
int i, j, count;
string errorStr;
cout << endl << "Please enter a datafile name => ";
cin >> filename;
ifstream infile(filename.c_str(), ios::in);

for (count=0; !infile.fail() && count < MAX_UNITS; ++count){
infile >> data[count];
}
--count; // adjust to current last element

cout << "Datafile read; ready to begin conversions." << endl;

do {
cout << endl << "Please enter a measurement to convert with "
<< "unit label (or abbreviation)" << endl << "=> ";
cin >> measure >> oldunit;
cout << endl << "Enter unit to convert to "
<< "(label or abbreviation) => ";
cin >> newunit;
if (cin.fail()) {
cin.clear();
getline( cin, errorStr);
another = 'y';
cerr << endl << "Invalid input => " << errorStr << endl << endl;
} else {
for (i = 0; i < count && !data[i].sameunit(oldunit); ++i);
// find old unit data
for (j = 0; j < count && !data[j].sameunit(newunit); ++j);
// find new unit data
if (i >= count)
// can't find old unit
cout << oldunit << " was not found." << endl << endl;
else if (j >= count)
// can't find new unit
cout << newunit << " was not found." << endl << endl;
else
// both units found
cout << endl << measure << " " << oldunit
<< " is equal to "
<< (measure * data[i].getfactor() / data[j].getfactor())
<< " " << newunit << endl << endl;

cout << "Convert Another? (Y/N) => ";
cin >> another;
}
} while (another == 'Y' || another == 'y');
cout << endl;

return 0;
}

```

Computer Science & Information Technology

You might also like to view...

Find the error in each of the following program segments. Assume the following declara- tions and statements:

``` int *zPtr; // zPtr will reference array z void *sPtr = 0; int number; int z[ 5 ] = { 1, 2, 3, 4, 5 }; ``` a) ++zPtr; b) // use pointer to get first value of array number = zPtr; c) // assign array element 2 (the value 3) to number number = *zPtr[ 2 ]; d) // print entire array z for ( int i = 0; i <= 5; i++ ) cout << zPtr[ i ] << endl; e) // assign the value pointed to by sPtr to number number = *sPtr; f) ++z;

Computer Science & Information Technology

If your SQL query does not include a WHERE statement, then _______ records will be selected

Fill in the blank(s) with correct word

Computer Science & Information Technology

When you receive an invitation to chat, ________ to reply to your friend's initial message

A) click OK B) start typing C) click Chat D) press Enter on your keyboard

Computer Science & Information Technology

The ____ directory allows companies to offer their Web services by acting like a telephone book for Web services.

A. SOAP B. XML C. UDDI D. WSDL

Computer Science & Information Technology