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...

Search engines that search other search engines are called

a. megasearch engines. b. betasearch engines. c. gigasearch engines. d. metasearch engines.

Computer Science & Information Technology

Which STP mode is the default on most modern Cisco Catalyst switches?

A) MST B) RPVST+ C) PVST+ D) CST

Computer Science & Information Technology