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 4 Drill // 4.8
Reject values without units or with 'illegal' representations of units, such as y, yard, meter, km, and gallons.
#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;
int main()
{
double number = 0;
double converted = 0;
double largest = 0;
double lrgConverted = 0;
double smallest = 0;
double smlConverted = 0;
string unit = "";
string largestUnit;
string smallestUnit;
const double m_to_cm = 100;
const double in_to_cm = 2.54;
const double ft_to_cm = 12;
cout << "Please enter a number: \n";
cout << "Please enter a measurement unit, cm, m, in or ft: \n";
while (cin >> number >> unit)
{
if (unit != "cm" && unit != "ft" && unit != "in" && unit != "m")
{
cout << "\nSorry, that is an invalid unit. Please enter a number and valid unit: \n";
}
else
{
cout << "\nYou entered: " << number << unit;
if (unit == "m")
{
converted = number*m_to_cm;
}
else if (unit == "in")
{
converted = number*in_to_cm;
}
else if (unit == "ft")
{
converted = ((number*ft_to_cm)*in_to_cm);
}
else if (unit == "cm")
{
converted = number;
}
//
if (largest == 0 && smallest == 0)
{
largest = number;
smallest = number;
largestUnit = unit;
smallestUnit = unit;
lrgConverted = converted;
smlConverted = converted;
cout << "\nThe largest number so far is: " << largest << largestUnit;
cout << "\nThe smallest number so far is: " << smallest << smallestUnit << endl;
}
//
else if (converted > lrgConverted)
{
largest = number;
largestUnit = unit;
lrgConverted = converted;
cout << "\nThe largest number so far is: " << largest << largestUnit;
cout << "\nThe smallest number so far is: " << smallest << smallestUnit << endl;
}
else if (converted < lrgConverted)
{
if (converted < smlConverted)
{
smallest = number;
smallestUnit = unit;
smlConverted = converted;
}
cout << "\nThe largest number so far is: " << largest << largestUnit;
cout << "\nThe smallest number so far is: " << smallest << smallestUnit << endl;
}
cout << "\nPlease enter a number: ";
cout << "\nPlease enter a measurement unit, cm, m, in or ft: \n";
}
}
keep_window_open();
return 0;
}
This one seemed relatively easy to me as I've gotten into quite the habit of checking for bad input.
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 4 Drill // 4.8
Reject values without units or with 'illegal' representations of units, such as y, yard, meter, km, and gallons.
#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;
int main()
{
double number = 0;
double converted = 0;
double largest = 0;
double lrgConverted = 0;
double smallest = 0;
double smlConverted = 0;
string unit = "";
string largestUnit;
string smallestUnit;
const double m_to_cm = 100;
const double in_to_cm = 2.54;
const double ft_to_cm = 12;
cout << "Please enter a number: \n";
cout << "Please enter a measurement unit, cm, m, in or ft: \n";
while (cin >> number >> unit)
{
if (unit != "cm" && unit != "ft" && unit != "in" && unit != "m")
{
cout << "\nSorry, that is an invalid unit. Please enter a number and valid unit: \n";
}
else
{
cout << "\nYou entered: " << number << unit;
if (unit == "m")
{
converted = number*m_to_cm;
}
else if (unit == "in")
{
converted = number*in_to_cm;
}
else if (unit == "ft")
{
converted = ((number*ft_to_cm)*in_to_cm);
}
else if (unit == "cm")
{
converted = number;
}
//
if (largest == 0 && smallest == 0)
{
largest = number;
smallest = number;
largestUnit = unit;
smallestUnit = unit;
lrgConverted = converted;
smlConverted = converted;
cout << "\nThe largest number so far is: " << largest << largestUnit;
cout << "\nThe smallest number so far is: " << smallest << smallestUnit << endl;
}
//
else if (converted > lrgConverted)
{
largest = number;
largestUnit = unit;
lrgConverted = converted;
cout << "\nThe largest number so far is: " << largest << largestUnit;
cout << "\nThe smallest number so far is: " << smallest << smallestUnit << endl;
}
else if (converted < lrgConverted)
{
if (converted < smlConverted)
{
smallest = number;
smallestUnit = unit;
smlConverted = converted;
}
cout << "\nThe largest number so far is: " << largest << largestUnit;
cout << "\nThe smallest number so far is: " << smallest << smallestUnit << endl;
}
cout << "\nPlease enter a number: ";
cout << "\nPlease enter a measurement unit, cm, m, in or ft: \n";
}
}
keep_window_open();
return 0;
}
This one seemed relatively easy to me as I've gotten into quite the habit of checking for bad input.
No comments:
Post a Comment