Tuesday, 12 July 2016

Chapter 4 Drill // 4.9 - 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.9


Drill 4.9 - Keep track of the sum of values entered (as well as the smallest and the largest) and the
number of values entered. When the loop ends, print the smallest, the largest, the number of values
and the sum of values. Note that to keep the sum, you have to decide on a unit for that sum; use meters.

#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;


int main()
{
int no_values = 0;

double number = 0;
double converted = 0;
double finalConversion = 0;
double fc2 = 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_in = 12;
const double in_to_m = 0.0254;
const double ft_to_m = 0.3048;
const double cm_to_m = 0.01;


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;
finalConversion += number;
fc2 = number;
}
else if (unit == "in")
{
converted = number*in_to_cm;
finalConversion += number*in_to_m;
fc2 = number*in_to_m;
}
else if (unit == "ft")
{
converted = (number*ft_to_in)*in_to_cm;
finalConversion += number*ft_to_m;
fc2 = number*ft_to_m;
}
else if (unit == "cm")
{
converted = number;
finalConversion += number*cm_to_m;
fc2 = number*cm_to_m;
}

//

if (largest == 0 && smallest == 0)
{
largest = number;
smallest = number;
largestUnit = unit;
smallestUnit = unit;

lrgConverted = converted;
smlConverted = converted;
}

//

else if (converted > lrgConverted)
{
largest = number;
largestUnit = unit;
lrgConverted = converted;
}

else if (converted < lrgConverted)
{
if (converted < smlConverted)
{
smallest = number;
smallestUnit = unit;
smlConverted = converted;
}
}

cout << "\nThe number of values entered so far : " << ++no_values;
cout << "\nThe sum of values entered so far in meters is: " << finalConversion;
cout << "\nThe smallest value entered so far is: " << smallest << smallestUnit;
cout << "\nThe largest value entered so far is: " << largest << largestUnit << 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 was actually the easiest one of the bunch so far and required little tinkering to the code. Basically 4.9 wants us to keep a count of how many values are entered, and create a sum of all the values entered so far in meters. Then print these out at the end of the loop along with the smallest and the largest.


no_values was created to keep track of how many values we input. This was the easiest part. Since we only input 1 value each time round the loop, right at the end I just added ++no_values. Also, since it's inside the 'else' part of the entire code, it only increases if a valid unit is entered.

Next I created 3 more const variables to convert our number into meters. I added this to the beginning conversion so I didn't have to create anther set of if statements. I was wondering what loop to use to keep adding the number onto each other and then I remembered I was idiot and used += instead. 

No comments:

Post a Comment