Pages

Friday, 8 July 2016

Chapter 4 Drill // 4.7 - Principles & Practice Using C++

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.7

Add a unit to each double entered; that is, enter values such as 10cm, 2.5in, 5ft or 3.33m. Accept thr four units cm, m , in ft. Assume conversion factors 1m == 100cm, 1in == 2.54cm, 1ft == 12in. Read th eunit indicator into a string. You may consider 12 m (with a space between the number and the unit to 12m (without a space).


#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)
{
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 took me roughly around 6 hours to complete, at first I thought it couldn't be done but as I stared at the screen wondering why I had decided to become a games developer instead of a photographer, it slowly came to me.

The hardest part was comparing all the values but still getting them to show in their original form, a lot of variables are needed for this one.

Basically, after the data has been inputted, we need to convert that into a universal number that we can use for comparing. I chose cm. 

The first If statement, like 4.6, gets the ball rolling and sets all the variables up for the next number. 

Another value is entered and it converts it to cm based on what unit you put in and then checks if that number is smaller or larger than the original smallest or highest number. 

If it is larger, the original largest number is replaced by the current, same for the unit. If it's smaller, it then checks to see if it's smaller than the current smallest number, if not, nothing happens. If it is smaller though, the smallest number is replaced by that number and unit is replaced too. 

Now it's finished, it seems stupidly simple and whereas I'm half elated that I finally managed to figure it out and get it to work, I'm half annoyed at myself. I'm pretty sure that there is a way to simply the code but I'm glad that Bjarne is making us figure out how to do it the long way first.


No comments:

Post a Comment