Here is an exercise I completed today from Bjarne Stroustrup's Programming Principles and Practice in C++ book. Any tips about coding style, simplification, or perhaps modularization by means of using functions for some of the data processing would be sincerely appreciated. FYI I have only been programming for 5 months, apologies in advance for any blatant mistakes.
// This program takes floating-point numbers and select units as input, and displays the sum,
// the amount of measurements entered, and the high and low measurements entered.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// Globals
const double cm_to_m = 0.01;
const double m_to_cm = 100;
const double in_to_m = 2.54 * 0.01;
const double m_to_in = 100 * (1.0/2.54);
const double ft_to_m = 12 * 2.54 * 0.01;
const double m_to_ft = 100 * (1.0/2.54) * (1.0/12.0);
// Main Function
int main()
{
// Declarations
int count = 0;
double num, conv, low, high, sum = 0;
string unit, low_unit, high_unit;
vector<double> input_array;
// Input
cout << "Enter a measurement in one of the following accepted units (cm, m, in, ft): " << endl;
while (cin >> num >> unit) {
if (unit == "m" || unit == "cm" || unit == "in" || unit == "ft") {
if (unit == "m") {
input_array.push_back(num);
if (count == 0) low = num, low_unit = unit, high = num, high_unit = unit;
else if (num < low) low = num, low_unit = unit;
else if (num > high) high = num, high_unit = unit;
++count;
}
else if (unit == "cm") {
conv = num * cm_to_m;
input_array.push_back(conv);
if (count == 0) low = conv, low_unit = unit, high = conv, high_unit = unit;
else if (conv < low) low = conv, low_unit = unit;
else if (conv > high) high = conv, high_unit = unit;
++count;
}
else if (unit == "in") {
conv = num * in_to_m;
input_array.push_back(conv);
if (count == 0) low = conv, low_unit = unit, high = conv, high_unit = unit;
else if (conv < low) low = conv, low_unit = unit;
else if (conv > high) high = conv, high_unit = unit;
++count;
}
else if (unit == "ft") {
conv = num * ft_to_m;
input_array.push_back(conv);
if (count == 0) low = conv, low_unit = unit, high = conv, high_unit = unit;
else if (conv < low) low = conv, low_unit = unit;
else if (conv > high) high = conv, high_unit = unit;
++count;
}
}
else
cout << "Incorrect Unit Entered! Please try again..." << endl;
}
// Low/High Unit Re-Conversion
if (low_unit == "cm")
low *= m_to_cm;
else if (low_unit == "in")
low *= m_to_in;
else if (low_unit == "ft")
low *= m_to_ft;
if (high_unit == "cm")
high *= m_to_cm;
else if (high_unit == "in")
high *= m_to_in;
else if (high_unit == "ft")
high *= m_to_ft;
// Sort
sort(input_array.begin(), input_array.end());
// Sum the contents of the array.
for (int i = 0; i < input_array.size(); ++i)
sum += input_array[i];
// Output
cout << endl;
cout << count << " measurements were entered" << endl;
cout << "The sum of measurements entered is " << sum << " m" << endl;
cout << "The lowest measurement entered was " << low << " " << low_unit << endl;
cout << "The highest measurement entered was " << high << " " << high_unit << endl;
// Return
return 0;
}