I was curious as to how I'd be able to make this much clearer to read because from my perspective I understand what does what but how does it look to a third party? Also how are my object / method names doing?
//Program that stores units into a vector and compares / prints them back to the user.
#include "../../std_lib_facilities.h"
double unit_conversion(double input_value, string input_unit, string conversion);
double vector_sum(vector<double> stored_values);
void input_prompt(bool newline);
void vector_print(vector<double> stored_values);
int main()
{
//Variable and Vector Declartion
double input_value = 0, smallest_value = 0, largest_value = 0;
vector<double> stored_values;
vector<string> accepted_unit{"cm", "m", "in", "ft"};
string input_unit;
input_prompt(0);
//First variable assignment and unit checking
while(cin >> input_value >> input_unit)
{
if (find(accepted_unit.begin(), accepted_unit.end(), input_unit) != accepted_unit.end())
{
break;
}
else
{
cout << "You have entered an invalid unit type!\n";
input_prompt(1);
}
}
//Assign smallest/largest value to first valid input
stored_values.push_back(unit_conversion(input_value, input_unit, "convert_to_m"));
string smallest_value_unit = input_unit, largest_value_unit = input_unit;
input_prompt(1);
while (cin >> input_value >> input_unit)
{
if (find(accepted_unit.begin(), accepted_unit.end(), input_unit) != accepted_unit.end())
{
//Smaller or Larger
if (unit_conversion(input_value, input_unit, "convert_to_m") > stored_values[0])
{
//New value is Larger
cout << "The new value " << input_value << input_unit << " is the largest so far!\nThe last largest number being: " << unit_conversion(stored_values[stored_values.size() - 1], largest_value_unit, "convert_from_m") << largest_value_unit << '\n';
largest_value_unit = input_unit;
}
else if (unit_conversion(input_value, input_unit, "convert_to_m") < stored_values[stored_values.size() - 1])
{
//New value is Smaller
cout << "The new value " << input_value << input_unit << " is the smallest so far!\nThe last smallest number being: " << unit_conversion(stored_values[0], smallest_value_unit, "convert_from_m") << smallest_value_unit << '\n';
smallest_value_unit = input_unit;
}
cout << "Your last input was: " << input_value << input_unit << '\n';
//Add input value into array and sort by size
stored_values.push_back(unit_conversion(input_value, input_unit, "convert_to_m"));
sort(stored_values);
input_prompt(1);
}
else
{
//Bad unit type error
cout << "You have entered an invalid unit type!\n";
input_prompt(1);
}
}
//Output for sum, total inputs, last input and range of inputs in cm
cout << "The final smallest value was: " << stored_values[0] << smallest_value_unit << '\n';
cout << "The last largest number being : " << stored_values[stored_values.size() - 1] << largest_value_unit << '\n';
cout << "Your total sum is now: (" << vector_sum(stored_values) * 100 << " cm) (" << vector_sum(stored_values) << " m) (" << vector_sum(stored_values) * 39.37 << " in) (" << vector_sum(stored_values) * 3.28 << " ft)\n";
cout << "You have entered a total of: " << stored_values.size() << " values\n";
vector_print(stored_values);
return 0;
}
void input_prompt(bool newline)
{
if (newline)
cout << "\nInput a new number and a unit (cm, m, in, ft) ";
else
cout << "Input a new number and a unit (cm, m, in, ft) ";
}
//Converts units using the specified operator needed for conversion
double unit_conversion(double input_value, string input_unit, string conversion)
{
//Converts all numbers to metres
if (conversion == "convert_to_m")
{
if (input_unit == "cm")
return input_value / 100;
else if (input_unit == "m")
return input_value;
else if (input_unit == "in")
return input_value / 39.37;
else if (input_unit == "ft")
return input_value / 3.28;
}
else if (conversion == "convert_from_m")
{
if (input_unit == "cm")
return input_value * 100;
else if (input_unit == "m")
return input_value;
else if (input_unit == "in")
return input_value * 39.37;
else if (input_unit == "ft")
return input_value * 3.28;
}
else
{
cout << "Type error, please revise!\n";
}
}
//Adds all of vector range and returns sum
double vector_sum(vector<double> stored_values)
{
double temp_value = 0;
for (int i = 0; i < stored_values.size(); ++i)
{
temp_value += stored_values[i];
}
return temp_value;
}
//Prints all vector values in cm
void vector_print(vector<double> stored_values)
{
cout << "Your range of values are (in m): ";
for (int i = 0; i < stored_values.size(); ++i)
{
cout << stored_values[i] << "m ";
}
cout << '\n';
}