第一次后部分程序没有执行运行

A part of the program doesn't execute after the first time run

当我在其中做一个练习时,我正在查看一本关于 C++ 的新书。该程序获得一个长度,后跟它的单位。该程序将该值转换为厘米,然后将该值与之前的输入进行比较,并给出一个输出,说明到目前为止输入的最大和最小值。 问题是,使用 'feet' 单位时似乎有问题。 下面是我的 main.cpp:

#include "std_lib_facilities.h"
int main()
{
    double value{0};
    double largest{0};
    double smallest{0};
    string unit;
    constexpr double cm_per_in {2.54};
    constexpr double cm_per_m {100};
    constexpr double cm_per_ft {cm_per_in*12};
    int sum;

    while(cin >> value >> unit)
    {
        if(unit=="cm")
        {
            if(largest==0 && smallest == 0){
                smallest = value;
                largest = value;
                cout << value << "cm is the only value entered till now.\n";
            }   
            else if(value<smallest)
            {
                smallest = value;
                cout << "\n\nThe smallest so far is "<<smallest<<"cm.\n";
                cout << "\nThe largest so far is "<<largest<<"cm.\n";
            }
            else if(value>largest){
                largest = value;
                cout  << "The smallest so far is "<<smallest<<"cm.\n";
                cout << "\nThe largest so far is "<<largest<<"cm.\n";
            }
            sum += value;

        }
        else if(unit=="m")
        {
            if(largest==0 && smallest == 0){
                smallest = value*cm_per_m;
                largest = value*cm_per_m;
                cout << value*cm_per_m << "cm is the only value entered till now.\n";
            }   
            else if(value*cm_per_m<smallest)
            {
                smallest = value*cm_per_m;
                cout << "The smallest so far is "<<smallest<<"cm.\n";
                cout << "\nThe largest so far is "<<largest<<"cm.\n";
            }
            else if(value*cm_per_m>largest){
                largest = value*cm_per_m;
                cout << "\nThe smalles so far is "<<smallest<<"cm.\n";
                cout  << "The largest so far is "<<largest<<"cm.\n";
            }
            sum += value*cm_per_m;

        }
        else if(unit=="ft")
        {
            if(largest==0 && smallest == 0){
                smallest = value*cm_per_ft;
                largest = value*cm_per_ft;
                cout << value*cm_per_ft << "cm is the only value entered till now.\n";
            }   
            else if(value*cm_per_ft<smallest)
            {
                smallest = value*cm_per_ft;
                cout << "The smallest so far is "<<smallest<<"cm.\n";
                cout << "\nThe largest so far is "<<largest<<"cm.\n";
            }
            else if(value*cm_per_ft>largest){
                largest = value*cm_per_ft;
                cout << "\nThe smallest so far is "<<smallest<<"cm.\n";
                cout  << "The largest so far is "<<largest<<"cm.\n";
            }
            sum += value * cm_per_ft;

        }
        else if(unit=="in")
        {
            if(largest==0 && smallest == 0){
                smallest = value*cm_per_in;
                largest = value*cm_per_in;
                cout << value*cm_per_in << "cm is the only value entered till now.\n";
            }   
            else if(value*cm_per_in<smallest)
            {
                smallest = value*cm_per_in;
                cout << "The smallest so far is "<<smallest<<"cm.\n";
                cout << "The largest so far is "<<largest<<"cm.\n";
            }
            else if(value*cm_per_in>largest){
                largest = value*cm_per_in;
                cout << "The smallest so far is "<<smallest<<"cm.\n";
                cout  << "The largest so far is "<<largest<<"cm.\n";
            }
            sum += value * cm_per_in;

        }
    }
    return 0;
}

下面是程序中发生的 input/output: D:\C++\testing\subtesting>程序 24 厘米 24cm是目前唯一输入的数值

24 in
The smallest so far is 24cm.
The largest so far is 60.96cm.

24 m

The smallest so far is 24cm.
The largest so far is 2400cm.

24 ft

在上面的输出中可以看到,使用'feet'单位时没有输出。另一个测试的输出:

D:\C++\testing\subtesting>program
24 ft
731.52cm is the only value entered till now.
24 ft

The smallest so far is 731.52cm.
The largest so far is 731.52cm.
200 ft

The smallest so far is 731.52cm.
The largest so far is 6096cm.
30 ft

'feet' 代码中似乎存在某种错误。有时代码有效,有时无效。有人可以帮助我吗?

代码没有问题。您的期望有问题。

只有其中一个变化时才显示最大和最小,这是一个明确的设计意图,而不是错误。

输入 24 in 和 24 m 后,第三个值 24 ft 既不是最大也不是最小值,因此正确地没有输出。