需要帮助对浮点数进行例外处理

Need help in making exceptions to floating numbers

本文关键字:处理 浮点数 帮助      更新时间:2023-10-16

当我在卡车编号中输入浮点数时。。如果它准备好滚动或超过极限,请直接输入重量,然后输入相应的声明。。如何更正此代码,以便只接受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()——有更好的方法可以保持程序运行。