celsius to kelvin and fahrenheit

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 5 Exercise 6
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 5 Exercise 6

//input: 24

#include "std_lib_facilities.h"

double ctoftok(double c) {

	//celsius to fahrenheit to kelvin

	double lowestTempinC = -273.15;
	double highestTempinC = 57.8;
	double kelvin = 273.15;

	if (lowestTempinC >= c) {
		error("Temperature is unrealisticly low: ", c);
	}
	if (highestTempinC <= c) {
		error("Temperature is unrealisticly high: ", c);
	}

	double f = 9.0 / 5 * c + 32;
	double k = c + kelvin;

	cout << "celsius: " << c << ", fahrenheit: " << f << ", kelvin: " << k << "\n";
	return 0;
}

int main()
try
{
	double value = 0;
	string s;

	cout << "Enter temperature in celsius degree to get fahrenheit and kelvin: \n";

	if (cin >> value) {
		ctoftok(value);
	}

	else {
		cin.clear();
		cin >> s;
		error("Illegal input entered: ", s);
	}

	keep_window_open();
}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch (...) {
	cout << "Exiting \n";
	keep_window_open(".");
}
Output:
Enter temperature in celsius degree to get fahrenheit and kelvin:
24
celsius: 24, fahrenheit: 75.2, kelvin: 297.15

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.