Bjarne Stroustrup “Programming Principles and Practice Using C++”

**Chapter 4 Drill 10**

Using std_lib_facilities.h by Bjarne Stroustrup.

[code language=”cpp”]

// Philipp Siedler

// Bjarne Stroustrup’s PP

// Chapter 4 Drill 10

#include "std_lib_facilities.h"

int main()

{

cout << "Enter a double followed by m(meter) in(inch) ft(feet) or cm(centimeter or a ‘|’ to terminate the program:\n";

vector<double> numbers;

vector<double> numbersMeter;

double x;

double y;

string unit;

while (cin >> x >> unit)

{

if (unit == "m")

{

y = x;

numbersMeter.push_back(x);

}

else if (unit == "in")

{

y = (x * 2.54) / 100;

}

else if (unit == "ft")

{

y = ((x / 12)*2.54) / 100;

}

else if (unit == "cm")

{

y = x / 100;

}

else

{

cout << "I dont know what " << unit << " for a unit is, enter a valid unit \n";

continue;

}

cout << "you entered: " << x << " " << unit << "\n\n";

numbers.push_back(y);

double sum = 0;

for (int i = 0; i < numbers.size(); i++)

{

sum += numbers[i];

}

cout << "the sum of all numbers in m is: " << sum << " m\n";

sort(numbers);

cout << "the smallest so far is: " << numbers[0] << " " << "m\n";

cout << "the largest so far is: " << numbers[numbers.size() – 1] << " " << "m\n";

cout << "you have entered: " << numbers.size() << " values\n";

cout << "all the values entered as meters: \n";

for (int i = 0; i < numbersMeter.size(); i++)

{

cout << numbersMeter[i] << "\n";

}

}

}

[/code]

Output:Enter a double followed by m(meter) in(inch) ft(feet) or cm(centimeter or a '|' to terminate the program: 1.2 ft you entered: 1.2 ft the sum of all numbers in m is: 0.00254 m the smallest so far is: 0.00254 m the largest so far is: 0.00254 m you have entered: 1 values all the values entered as meters: 2.3 m you entered: 2.3 m the sum of all numbers in m is: 2.30254 m the smallest so far is: 0.00254 m the largest so far is: 2.3 m you have entered: 2 values all the values entered as meters: 2.3 4.5 m you entered: 4.5 m the sum of all numbers in m is: 6.80254 m the smallest so far is: 0.00254 m the largest so far is: 4.5 m you have entered: 3 values all the values entered as meters: 2.3 4.5