当我在卡车号码中输入浮动数字时..直接输入重量然后输入相应的声明,如果它已准备好滚动或超过限制..我是否更正此代码以便只有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 terminated\n";
break;
default:
cout<<"Invalid input";
choice();
break;
}
return retry;
}
答案 0 :(得分:0)
这可能无法回答您的问题,因为即使在阅读您的评论后您的要求仍然不明确。但是,请检查main
功能:
你是什么意思&#34; {
&#34;在case 2:
陈述之后?
case 2:
{
和&#34; }
&#34;就在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之间的简单范围检查,而不是那么多&&
和!=
第三,不要从main()
致电choice()
- 有更好的方法可以让程序继续运行。