gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:25:23 26 4
gpt4 key购买 nike

当提供下面显示的程序时,分别为低值和高值输入 -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 的意义何在?

关于c++ - 使用 if-else 控制结构的意外输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28622618/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com