A program that accepts numbers and computes their sum, and displays smallest and largest number I am self learning programming by reading "Programming: Principles, and Practice" by Bjarne Stroustrup. I have solved a drill. Please review my code and give suggestions.

Drill


  Write a program that:
  
  
  Accepts numbers with units m, cm, ft, and in and rejects other units.
  Keeps a record of all the numbers in a single unit and computes the      sum of the numbers.
  Prints the smallest and the largest numbers.
  


My solution:

#include &lt;iostream&gt;
#include &lt;algorithm&gt;

using namespace std;

int main()
{
    double temp{0};
    string temp_unit;
    vector&lt;double&gt; numbers;
    double sum{0};

    constexpr double cm_to_m = 0.01;
    constexpr double ft_to_m = 0.3048;
    constexpr double in_to_m = 0.0254;

    cout&lt;&lt;"Enter an integer with its unit. E.g 2m or 2 m \n";
    while(cin&gt;&gt;temp&gt;&gt;temp_unit){
        if(temp_unit=="m"){
            numbers.push_back(temp);
        }
        else if(temp_unit=="cm"){
            numbers.push_back(temp*cm_to_m);
        }
        else if(temp_unit=="ft"){
            numbers.push_back(temp*ft_to_m);
        }
        else if(temp_unit=="in"){
            numbers.push_back(temp*in_to_m);
        }
        else{
            cout&lt;&lt;"Incorrect Unit\n";
        }
    }
    sort(numbers.begin(),numbers.end());
    for(auto&amp; i:numbers){
        cout&lt;&lt;i&lt;&lt;" ";
        sum+=i;
    }
    cout&lt;&lt;"\nNumber of elements "&lt;&lt;numbers.size()&lt;&lt;endl;
    cout&lt;&lt;"Sum of numbers: "&lt;&lt;sum&lt;&lt;endl;
    cout&lt;&lt;"Smallest number: "&lt;&lt;numbers[0]&lt;&lt;endl;
    cout&lt;&lt;"Largest number: "&lt;&lt;numbers[numbers.size()-1]&lt;&lt;endl;
    return 0;
}
Use maps to handle mappings

instead of having constants for each individual ratio, you might as well have a single constant that holds both the ratios and their respective strings, as well as being able to map one to the other.

Consider this:

const std::unordered_map&lt;std::string, double&gt; unit_ratios = {
  {"m", 1.0},
  {"cm", 0.01},
  {"ft", 0.3048},
  {"in", 0.0254}
};

int main() {
  std::vector&lt;double&gt; numbers;

  double temp;
  std::string temp_unit;
  while(cin&gt;&gt;temp&gt;&gt;temp_unit){
    try {
      numbers.push_back(temp * unit_ratios.at(temp_unit));
    }
    catch(const std::out_of_range&amp;) {
      cout&lt;&lt;"Incorrect Unit\n";
    }
  }
...
}


Doesn't that look nicer? all the names and ratios nicely bundled together in one spot. 

That's also why I've set it as a global constant in my example: The ratios and their name have nothing to do with the main function, they just are, so they should exist out of context.

Declare variables as late as possible

double sum{0}; should be declared as close as possible to the first use.

Use algorithms when possible.

You can use std::accumulate to calculate the sum:

double sum = std::accumulate(numbers.begin(), numbers.end(), 0.0);


Use front() and back() to access the first and last elements of a vector

You could make an argument that numbers[0] is as good as numbers.front(), but not for the last element. 

cout&lt;&lt;"Smallest number: "&lt;&lt; numbers.front() &lt;&lt; endl;
cout&lt;&lt;"Largest number: "&lt;&lt; numbers.back() &lt;&lt; endl;


Do you really need to sort?

Sorting is a O(NlogN) operation, whereas std::max_element and std::min_element are both O(N).

Since you are only doing these two lookups through the vector, you might be better off just using these instead. 

If you can use c++11, then std::minmax_element is even better, since you only have to do one pass.

don't use using namespace std;

Just... just don't. Please see every other C++ question on this site as to why.