第一次运行后,程序的一部分不会执行

时间:2015-09-19 10:45:38

标签: c++ if-statement while-loop cin

当我在其中进行其中一个练习时,我正在查看一本关于C ++的新书。程序得到一个长度,然后是它的单位。程序将值转换为厘米,然后将该值与先前的输入进行比较,并给出一个输出,说明到目前为止输入的最大和最小值。 问题是,使用“脚”单元时似乎存在问题。 下面是我的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;
}

以下是程序发生的输入/输出:     d:\ C ++ \测试\ subtesting&GT;程序     24厘米     24厘米是迄今为止唯一输入的值。

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

“脚”代码中似乎存在某种错误。有时代码有效,有时却没有。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

代码没有问题。你的期望有些不对劲。

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

输入24英寸和24米后,第二个24英尺的值既不是最大也不是最小,所以正确地不产生输出。