【问题标题】:Unexpected output using if-else control structure使用 if-else 控制结构的意外输出
【发布时间】:2025-12-24 00:10:06
【问题描述】:

当提供如下所示的程序时,分别为低值和高值输入 -1.3 和 -1.1,它会打印错误消息“错误:高加仑值必须大于或等于低加仑值。”。然而,这个错误的测试是if(lowGallon > highGallon),在这个给定的情况下它显然不是。这个输出错误的解释是什么?

这个输入验证所在的具体部分在注释//checking for numerical input errors的部分下面。

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    double lowGallon,
           highGallon,
           literConvert;
    const int INCREMENTER = 1;
    char charVal;
    bool quitting = false,
         lowIsNeg = false,
         highIsNeg = false,
         highIsLessThanLow = false;

    cout << "This program creates a gallons to liters conversion table." << endl << endl;

    do {
        cout << "Enter the lowest gallon value to display (q to quit): ";
        cin >> lowGallon;
        cout << endl;

        do {
            //checking for data type input errors
            if (cin.fail()) {
                cin.clear();
                cin >> charVal;
                if (charVal == 'q') {
                    quitting = true;
                    cout << endl << "Aborting; no conversion performed." << endl;
                } else {
                    cout << "You entered an illegal character: (" << charVal << ")" << endl << endl;
                    cout << "Enter the lowest gallon value to display (q to quit): ";
                    cin >> lowGallon;
                    cout << endl;
                }
            }
        } while (cin.fail() && quitting == false);

        if (quitting == false) {
            lowGallon = static_cast<int>(lowGallon);

            cout << "Enter the highest gallon value to display (q to quit): ";
            cin >> highGallon;
            cout << endl;

            do {
                //checking for data type input errors
                if (cin.fail()) {
                    cin.clear();
                    cin >> charVal;
                    if (charVal == 'q') {
                        quitting = true;
                        cout << endl << "Aborting; no conversion performed." << endl;
                    } else {
                        cout << "You entered an illegal character: (" << charVal << ")" << endl << endl;
                        cout << "Enter the highest gallon value to display (q to quit): ";
                        cin >> highGallon;
                        cout << endl;
                    }
                }
            } while (cin.fail() && quitting == false);

            //checking for numerical input errors
            if (quitting == false) {
                cout << endl;
                if(lowGallon < 0) {
                    cout << "Error: low gallon value must not be negative." << endl;
                    lowIsNeg = true;
                } else {
                    lowIsNeg = false;
                }
                if(highGallon < 0) {
                    cout << "Error: high gallon value must not be negative." << endl;
                    highIsNeg = true;
                } else {
                    highIsNeg = false;
                }
                if(lowGallon > highGallon) {
                    cout << "Error: high gallon value must be larger than or equal to the low gallon value." << endl;
                    highIsLessThanLow = true;
                } else {
                    highIsLessThanLow = false;
                }
            }

            if (quitting == false && lowIsNeg == false && highIsNeg == false && highIsLessThanLow == false) {
                if (highGallon - static_cast<int>(highGallon) > 0) {
                    highGallon = static_cast<int>(highGallon) + 1;
                }

                cout << fixed << setprecision(1) << "The conversion table will be created for the gallon range" << endl;
                cout << "of " << lowGallon << " to " << highGallon << " in increments of " << static_cast<double>(INCREMENTER) << endl << endl;

                cout << "   GALLONS TO LITERS" << endl;
                cout << "   CONVERSION TABLE" << endl;
                cout << "   Gallons    " << "Liters" << endl;
                cout << "   =======   " << "=======" << endl;

                for(int counter = lowGallon; counter <= highGallon; counter += INCREMENTER) {
                    cout << setw(9) << setprecision(1) << static_cast<double>(counter);
                    literConvert = counter * 3.785;
                    cout << setw(11) << setprecision(3) << literConvert << endl;
                }
            } else if (quitting == false) {
                cout << "Please re-enter low and high gallon values correctly." << endl << endl << endl;
            }
        }
    } while(quitting == false && (lowIsNeg == true || highIsNeg == true || highIsLessThanLow == true));

    return 0;
}

【问题讨论】:

  • 当我滚动浏览你的长长的代码行时,我发现了这个lowGallon = static_cast&lt;int&gt;(lowGallon); 这显然是你的问题,因为如果 lowGallon 是 -1.3,它现在是 -1。还想补充一下,如果是布尔变量,则不需要:if (boolean == true) {....},只需if (boolean)if (!boolean),就足够了

标签: c++ validation if-statement input output


【解决方案1】:

在你的代码中

 lowGallon = static_cast<int>(lowGallon);

lowGallon 值从-1.3 截断为-1.0。但你永远不会截断 highGallon 值。

其余的如下。 -1.0 确实大于 -1.1,因此出现错误消息。

你为什么要这样做?到int 的中间转换有什么意义?

【讨论】: