# celsius to kelvin error handling

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

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

//input: 24

#include "std_lib_facilities.h"

double ctok(double c) {

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 k = c + kelvin;

if (k >= (lowestTempinC + kelvin) && k <= (highestTempinC + kelvin)) {
return k;
}
else {
error("post-condition ctok()");
}
}

int main()
try
{
double c = 0;
string s;
cout << "Enter temperature in celsius to be converted to kelvin: \n";

if (cin >> c) {
double k = ctok(c);
cout << k << '\n';
}
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 to be converted to kelvin:
24
297.15

# celsius to kelvin 02

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

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

#include "std_lib_facilities.h"

//input: 24

double ctok(double c) {

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 k = c + kelvin;

if (k >= (lowestTempinC + kelvin) && k <= (highestTempinC + kelvin)) {
return k;
}
else {
error("post-condition ctok()");
}
}

int main()
try
{
double c = 0;
string s;
cout << "Enter temperature in celsius to be converted to kelvin: \n";

if (cin >> c) {
double k = ctok(c);
cout << k << '\n';
}
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 to be converted to kelvin:
24
297.15

# celsius to kelvin 01

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

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

//input: 24

double ctok(double c) {
int k = c + 273.15;
return k;
}

int main() {
double c = 0;
cin >> c;
double k = ctok(c);
cout << k << '\n';

keep_window_open();
}
Output:
24
297

# error training

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 5 Drill 1

#include "std_lib_facilities.h"

int main()
try
{
/*1
Cout << "Success!\n";

cout << "1. Success!\n";
*/

/*2
cout << "Success!\n;

cout << "2. Success!\n";
*/

/*3
cout << "Sucess" << !\n"

cout << "3. Success!" << "\n";
*/

/*4
cout << sucess << '\n';

cout << "4. Success!" << '\n';
*/

/*5
string res = 7;
vector<int> v10;
v[5] = res;
cout << "Success!\n";

int res = 7;
vector<int> v(10);
v[5] = res;
cout << "5. Success!\n";
*/

/*6
vector<int> v(10);
v(5) = 7;
if (v(5) != 7) cout << "Success!\n";

vector<int> v(10);
v[5] = 7;
if (v[5] == 7) cout << "Success!\n";
*/

/*7
if(cond) cout << "Success!\n";
else cout << "Fail!\n";

if (true) cout << "Success!\n";
else cout << "Fail!\n";
*/

/*8
bool c = false;
if(c) cout << "Sucess!\n";
else cout << "Fail!\n";

bool c = true;
if (c) cout << "Success!\n";
else cout << "Fail!\n";
*/

/*9
string s = "ape";
boo c = "fool" < s;
if(c) cout << "Success!\n";

string s = "ape";
bool c = "fool" > s;
if (c) cout << "Success!\n";
*/

/*10
string s = "ape";
if(s == "fool") cout << "Success!\n";

string s = "ape";
if (s != "fool") cout << "Success!\n";
*/

/*11
string s = "ape";
if(s == "fool") cout < "Success!\n";

string s = "ape";
if (s != "fool") cout << "Success!\n";
*/

/*12
string s = "ape";
if(s + "fool") cout < "Success!\n";

string s = "ape";
string check = "nothing";
if (check != s + "fool") { cout << "Success!\n"; }
*/

/*13
vector<char> v(5);
for(int i = 0; 0 < v.size(); ++i);
cout << "Success!\n";

vector<char> v(5);
for (int i = 0; i < v.size(); ++i);
cout << "Success!\n";
*/

/*14
vector<char> v(5);
for(int i = 0; i <= v.size(); ++i);
cout << "Success!\n";

vector<char> v(5);
for (int i = 0; i <= v.size(); ++i);
cout << "Success!\n";
*/

/*15
string s = "Success!\n";
for (int i = 0; i < 6; ++i) cout s[i];

string s = "Success!\n";
for (int i = 0; i < s.size(); ++i) cout << s[i];
*/

/*16
if(true) then cout << "Success!\n";
else cout << "Fail!\n";

if (true) cout << "Success!\n";
else cout << "Fail!\n";
*/

/*17
int x = 2000;
char c = x;
if(c == 2000) cout << "Success!\n";

int x = 2000;
int c = x;
if (c == 2000) cout << "Success!\n";
*/

/*18
string s = "Success!\n";
for(int = 0; i < 10; i++) cout << s[i];

string s = "Success!\n";
for (int i = 0; i < 10; i++) cout << s[i];
*/

/*19
vector v(5);
for(int i = 0; i <= v.size(); ++i);
cout << "Success!\n";

vector<int> v(5);
for (int i = 0; i <= v.size(); ++i);
cout << "Success!\n";
*/

/*20
int i = 0;
int j = 9;
while (i < 10) ++j;
if( j < i) cout << "Success!\n";

int i = 0;
int j = 9;
while (j < 10) j++;
if (j > i) cout << "Success!\n";
*/

/*21
int x = 2;
double d = 5 / (x - 2);
if(d == 2 * x + 0.5) cout << "Success!\n";

double x = 2;
double d = 5 / (x - 1);
if (d == 2 * (x + 0.5)) cout << "Success!\n";
*/

/*22
string<char> s = "Success!\n";
for(int i = 0; i <= 10; ++i) cout << s[i];

vector<string> s;
s.push_back("Success!\n");
for (int i = 0; i < s.size(); ++i) cout << s[i];
*/

/*23
int i = 0;
while(i < 10) ++j;
if(j < i) cout << "Success!\n";

int i = 0;
int j = 11;
while (i < 10) ++i;
if (j > i) cout << "Success!\n";
*/

/*24
int x = 4;
double d = 5 / (x - 2);
if(d = 2 * x + 0.5) cout << "Success!\n";

double x = 2;
double d = 5 / (x - 1);
if (d = 2 * (x + 0.5)) cout << "Success!\n";
*/

/*25
//cin << "Success!\n";

cout << "Success!\n";
*/

keep_window_open();
}
catch (exception& e) {
cerr << "error: " << e.what() << '\n';
keep_window_open();
return 1;
}
catch (...) {
cerr << "Unknown exception!\n";
keep_window_open();
return 2;
}

# pre and post condition in function

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP

#include "std_lib_facilities.h"

int area(int length, int width) {
if (length <= 0 || width <= 0) {
error("area() pre-condition");
}

int a = length * width;

if (a <= 0) {
error("area() post-condition");
}
return a;
}

int main()
try
{
int area1;
area1 = area(60000, 60000);
cout << area1;

keep_window_open("x");
}
catch (exception& e) {
cerr << "error: " << e.what() << '\n';
keep_window_open();
return 1;
}
catch (...) {
cerr << "Unknown exception!\n";
keep_window_open();
return 2;
}
Output:
High temperature: 42.6
Low temperature: -26.1
Average temperature: 9.28333

# temperature high, low and average [set range]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP

#include "std_lib_facilities.h"

/*
First set of doubles to be tested
-16.5, -23.2, -24.0, -25.7, -26.1, -18.6, -9.7, -2.4,
7.5, 12.6, 23.8, 25.3, 28.0, 34.8, 36.7, 41.5,
40.3, 42.6, 39.4, 35.4, 12.6, 6.5, -3.7, -14.3
*/
/*
Second set of doubles to be tested
76.5, 73.5, 71.0, 73.6, 70.1, 73.5, 77.6, 85.3,
88.5, 91.7, 95.9, 99.2, 98.2, 100.6, 106.3, 112.4,
110.2, 103.6, 94.9, 91.7, 88.4, 85.2, 85.4, 87.7
*/

int main() {
vector<double> temps{ -16.5, -23.2, -24.0, -25.7, -26.1, -18.6, -9.7, -2.4,
7.5, 12.6, 23.8, 25.3, 28.0, 34.8, 36.7, 41.5,
40.3, 42.6, 39.4, 35.4, 12.6, 6.5, -3.7, -14.3 };

double sum = 0;
double highTemp = -128.6;
double lowTemp = 136;

for (double temp : temps) {
sum += temp;
if (temp > highTemp) highTemp = temp;
if (temp < lowTemp) lowTemp = temp;
}

cout << "High temperature: " << highTemp << "\n";
cout << "Low temperature: " << lowTemp << "\n";
cout << "Average temperature: " << sum / temps.size() << "\n";

keep_window_open("x");
}
Output:
High temperature: 42.6
Low temperature: -26.1
Average temperature: 9.28333

# temperature high, low and average

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP

#include "std_lib_facilities.h"

/*
First set of doubles to be tested
-16.5, -23.2, -24.0, -25.7, -26.1, -18.6, -9.7, -2.4,
7.5, 12.6, 23.8, 25.3, 28.0, 34.8, 36.7, 41.5,
40.3, 42.6, 39.4, 35.4, 12.6, 6.5, -3.7, -14.3
*/

/*
Second set of doubles to be tested
76.5, 73.5, 71.0, 73.6, 70.1, 73.5, 77.6, 85.3,
88.5, 91.7, 95.9, 99.2, 98.2, 100.6, 106.3, 112.4,
110.2, 103.6, 94.9, 91.7, 88.4, 85.2, 85.4, 87.7
*/

int main(){

vector<double> temps{ 76.5, 73.5, 71.0, 73.6, 70.1, 73.5, 77.6, 85.3,
88.5, 91.7, 95.9, 99.2, 98.2, 100.6, 106.3, 112.4,
110.2, 103.6, 94.9, 91.7, 88.4, 85.2, 85.4, 87.7 };

double highTemp = 0;
double lowTemp = 0;
double sum = 0;

for (double x : temps) {
if (x > highTemp) {
highTemp = x;
}
if (x < lowTemp) {
lowTemp = x;
}
sum += x;
}

cout << "High temperature: " << highTemp << '\n';
cout << "Low temperature: " << lowTemp << '\n';
cout << "Average temperature: " << sum/temps.size() << '\n';

keep_window_open(".");
}
Output:
High temperature: 112.4
Low temperature: 0
Average temperature: 89.2083

# uncaught exception

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP

#include "std_lib_facilities.h"

int main(){
if (true) {
error("Error to be caught");
}
}

/*
catch (exception& e) {
cerr << "error: " << e.what() << '\n';
keep_window_open();
return 1;
}
catch (...) {
cerr << "Unknown exception!\n";
keep_window_open();
return 2;
}

*/
/*Error Message
Unhandled exception at 0x7494A882 in BjarneStroustrup_PrinciplesPractice.exe:
Microsoft C++ exception: std::runtime_error at memory location 0x00CFF6F0.
*/

# area function debugging

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP

#include "std_lib_facilities.h"

int area(int length, int width) {
if (length <= 0 || width <= 0) {
return -1;}
return length * width;
}

int framed_area(int x, int y) {
constexpr int frame_width = 2;
if (x - frame_width <= 0 || y - frame_width <= 0) {
error("non-positive area() argument called by framed_area()");
}
return area(x - frame_width, y - frame_width);
}

double f(int x, int y, int z) {
int area1 = area(x, y);
if (area1 <= 0) {
error("non-positive area");
}
cout << "area1 = " << area1 << '\n';

if (x > 2 && y > 2 && z > 2) {
int area2 = framed_area(3, z);
int area3 = framed_area(y, z);
double ratio = double(area1) / double(area3);

cout << "area2 = " << area2 << '\n';
cout << "area3 = " << area3 << '\n';
cout << "ratio = " << ratio << '\n';
return 0;
}
else {
error("Input for x, y and z must be larger than frame width");
return -1;
}
}

int main()
try
{
f(3, 3, 3);
keep_window_open();
}
catch (exception& e) {
cerr << "error: " << e.what() << '\n';
keep_window_open();
return 1;
}
catch (...) {
cerr << "Unknown exception!\n";
keep_window_open();
return 2;
}
Output:
area1 = 9
area2 = 1
area3 = 1
ratio = 9

# testing error response 02

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP

#include "std_lib_facilities.h"

int area(int length, int width) {
return length * width;
}

int main()
try
{
//int x0 = arena(7);
//int x1 = area(7);
//int x2 = area("seven",2);
keep_window_open();
}

catch (exception& e) {
cerr << "error: " << e.what() << '\n';
keep_window_open();
return 1;
}

catch (...) {
cerr << "Unknown exception!\n";
keep_window_open();
return 2;
}