Setting the smallest number so far from user input I'm trying to teach myself C++ from Stroustrup's Principles and Practice Using C++ (second edition) and I've gotten to the drill in chapter 4. The problem tells me to make a program that takes in a number with a unit of either feet, inches, meters, and centimeters, as well as log the largest and smallest numbers the user has input. I got the code to work by initializing my smallest number as 0 and making an if statement check if the input was smaller or 0 (because you can't really have 0 length).

Is there a way to either start with an infinitely larger "smallest number" variable so it always changes it in the if statement, or a way to where it has to do as few if statements as possible? Also, I'd rather not ask for an initial starting number before the while loop.

#include "std_lib_facilities.h"

using namespace std;


int main()
{
    cout &lt;&lt; "Please input a number followed by a unit (cm, m, in, ft). Results are read in meters. Input a '|' to terminate the program." &lt;&lt; endl;

    double x;
    string unit = " ";;
    constexpr double cm = .01;
    constexpr double in = .0254;
    constexpr double ft = .3048;
    double smallest = 0;
    double largest;
    double sum = 0;


    while(cin &gt;&gt; x &gt;&gt; unit)
    {
        if((char)x == '|')
            break;

        cout &lt;&lt; "Value entered: " &lt;&lt; x &lt;&lt; unit &lt;&lt; endl;
        if(unit=="m" || unit=="cm" || unit=="in" || unit=="ft") //converts units to meters
        {
            if(unit == "m")
            {
                x=x;                //
            }
            else if(unit == "cm")
            {
                x*=cm;
            }
            else if(unit == "in")
            {
                x*=in;
            }
            else if(unit == "ft")
            {
                x*=ft;
            }
            else
                cout &lt;&lt; "Something went wrong." &lt;&lt; endl;


            if(x &gt; largest)
            {
                largest = x;
            }
            if(x &lt; smallest || smallest == 0)
            {
                smallest = x;
            }
            cout &lt;&lt; "The smallest number so far is: " &lt;&lt;smallest&lt;&lt; " meters\n";
            cout &lt;&lt; "The largest number so far is: " &lt;&lt; largest &lt;&lt; " meters\n";

            sum+=x;
        }
        else
            cout &lt;&lt; "Invalid unit! Try again." &lt;&lt; endl;
    }
    cout &lt;&lt; "The sum of all lengths is: " &lt;&lt; sum &lt;&lt; " meters\n";
}
Overall, this looks pretty good. It's straightforward and readable, which is great.
Naming
One of the most important things in programming is naming things so that anyone reading the code can figure out the meaning. You've done a pretty good job, but I think you could improve it. Calling a variable x is kind of useless. You could name it something like userValue, or even input so someone reading the code at least knows from the name that it's something that's input by the user.
Your constant names are OK, but could be better. (It's great that you're using named constants at all, though!) It would be nice to add that your converting from cm or in to meters. And there are some common conventions for constants, too. Some people prefer constants to be all capital letters, and others prefer to prefix them with the letter &quot;k&quot; (like it's a mathematical constant). So you could name them CM_TO_METERS or kInToMeters, etc. That way if you add other types of conversions in the future (like converting to Imperial), then you might add kCmToInches or whatever.
Finding the Min and Max
You ask if there's a good constant to initialize the minimum to. I usually use the constant DBL_MAX in this case. (It's defined in &lt;cfloat&gt;.) If you're looking for only positive numbers, then you can use 0 as the start value for the maximum value. Then any number entered will automatically be greater than that.
Potential Problem
I see one thing that could be a problem. You declare x as a double, but the first thing you do with it is cast it to a char and check if it equals |. That seems really odd, error prone, and hard to understand. If it were me, I would probably refactor this to prompt the user to tell me what they want to do (convert another value, or quit), then after they've told me they want to enter a number, get the numeric input into x.
A Better Loop
You say you don't like having the initial prompt outside of the loop, and I agree. It's good to remind the user what input you're expecting each time through the loop. I'd probably rewrite the loop like this:
bool done = false;
do {
    cout &lt;&lt; &quot;What would you like to do?\n&quot;;
    cout &lt;&lt; &quot;1) Convert a value\n&quot;;
    cout &lt;&lt; &quot;2) Quit\n&quot;;
    char action;
    cin &gt;&gt; action;
    if (action == '2')
    {
        done = true;
    }
    else
    {
        // Your code for getting and converting a number
    }
} while (!done);

Now it will prompt the user each time through the loop, and when they decide to quit, they can simply enter &quot;2&quot; at the prompt.