使用 if-else 控制结构的意外输出

Unexpected output using if-else control structure

本文关键字:意外 输出 控制结构 if-else 使用      更新时间:2023-10-16

当为低值和高值分别提供输入 -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<int>(lowGallon);

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

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

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