使用 if-else 控制结构的意外输出
Unexpected output using if-else control structure
当为低值和高值分别提供输入 -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
有什么意义?
相关文章:
- 字符串比较中的意外输出
- 来自 decltype 的意外输出类型
- 字符数组到十六进制字符串的转换 - 意外输出
- 使用 boost::regex 从目录中获取带有一些正则表达式的文件名称时出现意外输出
- 使用后序遍历递归的深度优先搜索会产生意外输出
- C++的意外输出
- 贝金纳C++练习解决方案的意外输出
- 无效* 转换获得意外输出
- 以下程序的意外输出
- 意外输出:矢量矢量(功率集)
- 基本 int 数组提供意外输出
- 线程的意外输出
- 减法中的意外输出
- 带有左移操作员C 的意外输出
- C strncpy意外输出
- 意外输出..函数绑定在虚拟表中的发生方式
- 星号的意外输出
- 已编译的 protobuf 文件的意外输出路径
- 打印对象矢量的意外输出
- 递归函数用于计算 n 个数字之和的意外输出