keeping track of numbers 1.2

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 4 Drill 11
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Drill 11

#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";
		sort(numbersMeter);

		for (int i = 0; i < numbersMeter.size(); i++)
		{
			cout << numbersMeter[i] << "\n";
		}
	}
}
Output:
Enter a double followed by m(meter) in(inch) ft(feet) or cm(centimeter or a '|' to terminate the program:
4.5 m
you entered: 4.5 m

the sum of all numbers in m is: 4.5 m
the smallest so far is: 4.5 m
the largest so far is: 4.5 m
you have entered: 1 values
all the values entered as meters:
4.5
5.6 m
you entered: 5.6 m

the sum of all numbers in m is: 10.1 m
the smallest so far is: 4.5 m
the largest so far is: 5.6 m
you have entered: 2 values
all the values entered as meters:
4.5
5.6
6.7 ft
you entered: 6.7 ft

the sum of all numbers in m is: 10.1142 m
the smallest so far is: 0.0141817 m
the largest so far is: 5.6 m
you have entered: 3 values
all the values entered as meters:
4.5
5.6

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.