In all these exercises I am using Visual Studio Community 2015 and the header file "std_lib_facilities.h" which can be found here:
http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h
My version is spelt differently so adjust the code accordingly if copying and pasting.
Chapter 5 // Exercise 2
The following program takes in a temperature value in Celsius and converts it to kelvin. This code has many errors in it. Find the errors, list them and correct the code.
double ctok(double c)
{
int k = c + 273.15;
return int
}
int main()
{
double c = 0;
cin >> d;
double k = ctok("c");
Cout << k << '\n';
}
1. return int has no ;
2. return has nothing to return
3. int k will truncate floating point
3. 'd' is unidentified
4. trying to convert "c" string to a double
5. capital C on cout
6. return 0 missing on int main
#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;
double ctok(double c)
{
double k = c + 273.15;
return k;
}
int main()
{
double c = 0;
cin >> c;
double k = ctok(c);
cout << k << '\n';
keep_window_open();
return 0;
}
Chapter 5 // Exercise 3
Absolute zero is the lowest temperature that can be reached; it is -273.15C, or 0K. The above program, even when corrected, will produce erroneus results when given a temperature below this. Place a check in the main program that will produce an error is a temperature is given below -273.15C.
#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;
double ctok(double c)
{
double k = c + 273.15;
return k;
}
int main()
try
{
double c = 0;
cin >> c;
if (c < -273.15)
error("This is below absolute zero.");
double k = ctok(c);
cout << k << '\n';
keep_window_open();
return 0;
}
catch (exception& e)
{
cerr << "Error: " << e.what() << '\n';
keep_window_open();
return 1;
}
Chapter 5 // Exercise 4
Do exercise 3 again, but this time handle the error inside ctok().
#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;
double ctok(double c)
{
double k = c + 273.15;
if (c < -273.15)
error("This is below absolute zero.");
return k;
}
int main()
try
{
double c = 0;
cin >> c;
double k = ctok(c);
cout << k << '\n';
keep_window_open();
return 0;
}
catch (exception& e)
{
cerr << "Error: " << e.what() << '\n';
keep_window_open();
return 1;
}
Chapter 5 // Exercise 5
Add to the program so that it can also convert from Kelvin to Celsius.
#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;
double ctok(double c)
{
double k = c + 273.15;
if (c < -273.15)
error("This is below absolute zero.");
return k;
}
double kToC(double c)
{
double k = c - 273.15;
if (c < 0)
error("This is below absolute zero.");
return k;
}
char getkc()
{
char t;
cin >> t;
if (t != 'k' && t != 'c')
error("please use either k or c (lower case)");
return t;
}
double getTemp()
{
cout << "Enter Temperature: \n";
double c = 0;
cin >> c;
return c;
}
int main()
try
{
cout << "This converts Kelvin to Celsius and vice versa.\n";
cout << "What do you want to convert? k or c: \n";
char temp = getkc();
switch (temp)
{
case 'k':
{
double k = getTemp();
k = kToC(k);
cout << k << '\n';
break;
}
case 'c':
{
double c = getTemp();
c = ctok(c);
cout << c << '\'n';
break;
}
}
keep_window_open();
return 0;
}
catch (exception& e)
{
cerr << "Error: " << e.what() << '\n';
keep_window_open();
return 1;
}
Chapter 5 // Exercise 6
Write a program that converts from Celsius to Fahrenheit and from Fahrenheit to Celsius. Use estimation to see if your results are plausible.
#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;
double cToF(double t)
{
double c = ((t * (9.0/5.0)) + 32);
return c;
}
double fToC(double t)
{
double f = ((t - 32) * (5.0/9.0));
return f;
}
char getfc()
{
char t;
cin >> t;
if (t != 'f' && t != 'c')
error("please use either f or c (lower case)");
return t;
}
double getTemp()
{
cout << "Enter Temperature: \n";
double c = 0;
cin >> c;
return c;
}
int main()
try
{
cout << "This converts Fahrenheit to Celsius and vice versa.\n";
cout << "What do you want to convert? f or c: \n";
char temp = getfc();
switch (temp)
{
case 'f':
{
double f = getTemp();
f = fToC(f);
cout << f << '\n';
break;
}
case 'c':
{
double c = getTemp();
c = cToF(c);
cout << c << '\n';
break;
}
}
keep_window_open();
return 0;
}
catch (exception& e)
{
cerr << "Error: " << e.what() << '\n';
keep_window_open();
return 1;
}
No comments:
Post a Comment