keeping track of numbers 1.0

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

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

#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;
	double x;
	double y;
	string unit;

	while (cin >> x >> unit)
	{
		if (unit == "m")
		{
			y = 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";
		for (int i = 0; i < numbers.size(); i++)
		{
			cout << numbers[i] << ", ";
		}
		cout << "\n";
	}
}
Output:
type in two doubles, followed by m(meter) in(inch) ft(feet) or cm(centimeter or a '|' to terminate the program:
5.4 m
you entered: 5.4 m

the sum of all numbers in m is: 5.4 m
the smallest so far is: 5.4 m
the largest so far is: 5.4 m
you have entered: 1 values
5.4,
4.3 ft
you entered: 4.3 ft

the sum of all numbers in m is: 5.4091 m
the smallest so far is: 0.00910167 m
the largest so far is: 5.4 m
you have entered: 2 values
0.00910167, 5.4,

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.