c++ - Need help in making exceptions to floating numbers -
c++ - Need help in making exceptions to floating numbers -
when come in floating numbers in truck number..it goes straight please come in weight corresponding statement if either ready roll or on limit..how right code 1,2,3,4,5,6,7 accepted , floating numbers invalid. thanks.
#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(); { cout<<"please come in truck number: "; cin>>inputtruck; } while(isinputtruckinvalid(inputtruck)); { cout<<"please come in weight: "; cin>>weight; }while(isinputweightneg(weight)); switch(trucknum[inputtruck]) { case 1: if(weight<=maxweight[1]) { cout<<"truck 1 ready roll - weight limit passed"<<endl; choice(); } else { cout<<"truck 1 has exceeded maximum allowable weight limit of 50000"<<endl; choice(); } break; case 2: { if(weight<=maxweight[2]) { cout<<"truck 2 ready roll - weight limit passed"<<endl; choice(); } else { cout<<"truck 2 has exceeded maximum allowable weight limit of 25000"<<endl; choice(); } break; case 3: if(weight<=maxweight[3]) { cout<<"truck 3 ready roll - weight limit passed"<<endl; choice(); } else { cout<<"truck 3 has exceeded maximum allowable weight limit of 20000"<<endl; choice(); } break; case 4: if(weight<=maxweight[4]) { cout<<"truck 4 ready roll - weight limit passed"<<endl; choice(); } else { cout<<"truck 4 has exceeded maximum allowable weight limit of 35000"<<endl; choice(); } break; case 5: if(weight<=maxweight[5]) { cout<<"truck 5 ready roll - weight limit passed"<<endl; choice(); } else { cout<<"truck 5 has exceeded maximum allowable weight limit of 40000"<<endl; choice(); } break; case 6: if(weight<=maxweight[6]) { cout<<"truck 6 ready roll - weight limit passed"<<endl; choice(); } else { cout<<"truck 6 has exceeded maximum allowable weight limit of 25000"<<endl; choice(); } break; case 7: if(weight<=maxweight[7]) { cout<<"truck 7 ready roll - weight limit passed"<<endl; choice(); } else { cout<<"truck 7 has exceeded maximum allowable weight limit of 30000"<<endl; choice(); } break; } } homecoming 0; } bool isinputweightneg(float weight) { if(weight<0) { cout<<"weight negative.please input valid number."<<endl; homecoming true; } else { homecoming false; } } bool isinputtruckinvalid(int inputtruck) { if(inputtruck!=1 && inputtruck!=2 && inputtruck!=3 && inputtruck!=4 && inputtruck!=5 && inputtruck!=6 && inputtruck!=7 ) { cout<<"you entered invalid truck number."<<endl; homecoming true; } else { homecoming false; } } int choice() { int retry; cout<<"\ndo want go on [y/n]? 1 yes/2 no?:"; cin>>retry; cout<<endl; switch (retry){ case 1: main(); break; case 2: cout<<"program terminated\n"; break; default: cout<<"invalid input"; choice(); break; } homecoming retry; }
this may not reply question, requirement still not clear, after reading comment. but, please check main
function: mean "{
" after case 2:
statement?
case 2: {
and "}
" before return 0;
it should
// -------------- case 2: if(weight<=maxweight[2]) { cout<<"truck 2 ready roll - weight limit passed"<<endl; choice(); } else { cout<<"truck 2 has exceeded maximum allowable weight limit of 25000"<<endl; choice(); } break; /// ---------------- case 7: if(weight<=maxweight[7]) { cout<<"truck 7 ready roll - weight limit passed"<<endl; choice(); } else { cout<<"truck 7 has exceeded maximum allowable weight limit of 30000"<<endl; choice(); } break; } homecoming 0; }
why function isinputtruckinvalid
complicated? can simple range check between 1 , 7 instead of many &&
, !=
thirdly, don't phone call main()
choice()
- there improve ways maintain programme running.
c++
Comments
Post a Comment