Drill 10 (Chapter 4)

26 views
Skip to first unread message

pantera

unread,
Dec 24, 2009, 5:28:56 AM12/24/09
to PPP-public
My program for Drill 10

// Drill 10 - Chapter 4
#include "std_lib_facilities.h"

int main()
{
const double cm_per_m = 100;
const double cm_per_inch = 2.54;
const double inch_per_foot = 12;
double x;
double smallest = 0;
double largest = 0;
string s = " ";
int counter = 0;
vector <double> mymeters;

cout << "Enter a number, followed by its unit: ";

while(cin>>x>>s && (s=="cm"||s=="m"||s=="in"||s=="ft")) {
++counter;
if (x<smallest) {
smallest = x;
}
else if (x>largest) {
largest = x;
}
if (s=="cm")
x = x/cm_per_m; // convert to m
else if (s=="in")
x = (x*cm_per_inch)/cm_per_m; // convert to cm to m
else if (s=="ft")
x = (x*inch_per_foot*cm_per_inch)/cm_per_m; // convert to inch to
cm to m

mymeters.push_back(x);
}

for (unsigned int i=0; i<mymeters.size();++i)
cout << mymeters[i] << endl;
}

Reply all
Reply to author
Forward
0 new messages