需要帮助对浮点数进行例外处理
Need help in making exceptions to floating numbers
当我在卡车编号中输入浮点数时。。如果它准备好滚动或超过极限,请直接输入重量,然后输入相应的声明。。如何更正此代码,以便只接受1,2,3,4,5,6,7,并且浮点数将无效。谢谢
#include<iostream>
using namespace std;
int main()
{
const int ARRAY_SIZE=8;
bool isInputTruckInvalid(int inputTruck);
bool isInputWeightNeg(float weight);
int maxWeight[ARRAY_SIZE]={0,50000,25000,20000,35000,40000,25000,30000};
int truckNum[ARRAY_SIZE]={0,1,2,3,4,5,6,7};
int inputTruck;
float weight;
int choice();
do
{
cout<<"Please enter the Truck Number: ";
cin>>inputTruck;
}
while(isInputTruckInvalid(inputTruck));
do
{
cout<<"Please enter the Weight: ";
cin>>weight;
}while(isInputWeightNeg(weight));
switch(truckNum[inputTruck])
{
case 1:
if(weight<=maxWeight[1])
{
cout<<"Truck 1 is ready to roll - weight limit passed"<<endl;
choice();
}
else
{
cout<<"Truck 1 has exceeded the maximum allowable weight limit of 50000"<<endl;
choice();
}
break;
case 2:
{
if(weight<=maxWeight[2])
{
cout<<"Truck 2 is ready to roll - weight limit passed"<<endl;
choice();
}
else
{
cout<<"Truck 2 has exceeded the maximum allowable weight limit of 25000"<<endl;
choice();
}
break;
case 3:
if(weight<=maxWeight[3])
{
cout<<"Truck 3 is ready to roll - weight limit passed"<<endl;
choice();
}
else
{
cout<<"Truck 3 has exceeded the maximum allowable weight limit of 20000"<<endl;
choice();
}
break;
case 4:
if(weight<=maxWeight[4])
{
cout<<"Truck 4 is ready to roll - weight limit passed"<<endl;
choice();
}
else
{
cout<<"Truck 4 has exceeded the maximum allowable weight limit of 35000"<<endl;
choice();
}
break;
case 5:
if(weight<=maxWeight[5])
{
cout<<"Truck 5 is ready to roll - weight limit passed"<<endl;
choice();
}
else
{
cout<<"Truck 5 has exceeded the maximum allowable weight limit of 40000"<<endl;
choice();
}
break;
case 6:
if(weight<=maxWeight[6])
{
cout<<"Truck 6 is ready to roll - weight limit passed"<<endl;
choice();
}
else
{
cout<<"Truck 6 has exceeded the maximum allowable weight limit of 25000"<<endl;
choice();
}
break;
case 7:
if(weight<=maxWeight[7])
{
cout<<"Truck 7 is ready to roll - weight limit passed"<<endl;
choice();
}
else
{
cout<<"Truck 7 has exceeded the maximum allowable weight limit of 30000"<<endl;
choice();
}
break;
}
}
return 0;
}
bool isInputWeightNeg(float weight)
{
if(weight<0)
{
cout<<"Weight is negative.Please input valid number."<<endl;
return true;
}
else
{
return false;
}
}
bool isInputTruckInvalid(int inputTruck)
{
if(inputTruck!=1 && inputTruck!=2 && inputTruck!=3 && inputTruck!=4 && inputTruck!=5 && inputTruck!=6 && inputTruck!=7 )
{
cout<<"You entered an invalid truck number."<<endl;
return true;
}
else
{
return false;
}
}
int choice()
{
int retry;
cout<<"nDo you want to continue [Y/N]? 1 as yes/2 as No?:";
cin>>retry;
cout<<endl;
switch (retry){
case 1:
main();
break;
case 2:
cout<<"Program terminatedn";
break;
default:
cout<<"Invalid input";
choice();
break;
}
return retry;
}
这可能不是你的问题的答案,因为即使在阅读了你的评论之后,你的需求仍然不明确。但是,请检查main
函数:case 2:
语句后的"{
"是什么意思?
case 2:
{
和return 0;
之前的"}
"
应该是
// --------------
case 2:
if(weight<=maxWeight[2])
{
cout<<"Truck 2 is ready to roll - weight limit passed"<<endl;
choice();
}
else
{
cout<<"Truck 2 has exceeded the maximum allowable weight limit of 25000"<<endl;
choice();
}
break;
/// ----------------
case 7:
if(weight<=maxWeight[7])
{
cout<<"Truck 7 is ready to roll - weight limit passed"<<endl;
choice();
}
else
{
cout<<"Truck 7 has exceeded the maximum allowable weight limit of 30000"<<endl;
choice();
}
break;
}
return 0;
}
为什么函数isInputTruckInvalid
如此复杂?它可以是1到7之间的简单范围检查,而不是这么多&&
和!=
第三,不要从choice()
调用main()
——有更好的方法可以保持程序运行。
相关文章:
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 使用提升将数据从 PyObject 复制到浮点数 *
- 使用浮点数和双精度数的非常小数字的数学
- 像union_这样的 Boost.Geometry 操作如何处理浮点类型的基本不精确性?
- 使用英特尔内联函数将打包的 8 位整数乘以浮点数向量
- 如何在 c++ 中将小数点后两位数的浮点数分配给另一个浮点数
- 返回浮点数的小数位数
- txt 文件中浮点数的最大和最小值
- 为什么 std::cout 打印浮点数、双精度和长双精度到相同的小数精度?
- 将浮点数转换为无符号字符数组并打印出来
- 如何将时间字符串 (M:SS) 转换为浮点数
- 如何将浮点数(*)[6]转换为浮点**类型?
- 当浮点数文本直接分配给不同的类型时,编译器如何处理浮点文字?
- 需要帮助对浮点数进行例外处理
- 在处理浮点数时避免不稳定的小数字
- 浮点数-在c++中处理极小的数字
- 浮点数 - 如何在 c++ 中处理非常小的数字
- 在c++中处理浮点数或双精度数.表示错误.丢失十进制值