Sunday, 18 August 2013

Find Possible Run-Time Error in code

Find Possible Run-Time Error in code

Can some one point out the possible runtime error in following code. It
works fine with normal input and extream input on my computer. But when in
a coding competition I upload this code, they show runtime errors in this
code. I tries fixing all possible errors and added try and catch block to
catch exception. Stil not able to figure out the runtime error. Please
help me out.
#include<iostream>
#include<iomanip>
#define SERVICETAX 10.36
#define STT 0.025
#define STAMP_AND_REGULATORY_CHARGES 0.006
typedef double rupees;
inline rupees roundOffRupees(rupees a){
long temp=(a*100);//roundf(a*100)/100;
//std::cout<<a<<std::endl;
a=((rupees)temp)/100;
return a;
}
int main(){
try{
rupees brokerageRate,buyingAmt,sellingAmt;
int qty;
std::cin>>brokerageRate>>buyingAmt>>sellingAmt>>qty;
if(!std::cin){
std::cerr<<"Invalid Input"<<std::endl;
//getch();
return 0;
}
rupees brokerageCharge1=buyingAmt*qty*brokerageRate/100;
brokerageCharge1=roundOffRupees(brokerageCharge1);
rupees serviceTaxAmt1=brokerageCharge1*SERVICETAX/100;
serviceTaxAmt1=roundOffRupees(serviceTaxAmt1);
rupees totalBuying=brokerageCharge1+serviceTaxAmt1;
totalBuying=roundOffRupees(totalBuying);
rupees brokerageCharge2=sellingAmt*qty*brokerageRate/100;
brokerageCharge2=roundOffRupees(brokerageCharge2);
rupees serviceTaxAmt2=brokerageCharge2*SERVICETAX/100;
serviceTaxAmt2=roundOffRupees(serviceTaxAmt2);
rupees sttAmt=sellingAmt*qty*STT/100;
sttAmt=roundOffRupees(sttAmt);
rupees totalSelling=brokerageCharge2+serviceTaxAmt2+sttAmt;
totalSelling=roundOffRupees(totalSelling);
rupees totalTurnover=(buyingAmt+sellingAmt)*qty;
totalTurnover=roundOffRupees(totalTurnover);
rupees
stampAndRegulatoryCharges=totalTurnover*STAMP_AND_REGULATORY_CHARGES/100;
stampandregulatorycharges=roundOffRupees(stampAndRegulatoryCharges);
rupees totalCharges=totalBuying+totalSelling+stampAndRegulatoryCharges;
rupees profit=(sellingAmt-buyingAmt)*qty-totalCharges;
profit=roundOffRupees(profit);
if(profit>=0){
std::cout<<"Profit"<<std::fixed<<std::setprecision(2)<<std::endl;
std::cout<<profit;//<<std::endl;
}
else if(profit<0){
std::cout<<"Loss" <<std::fixed<<std::setprecision(2)<<std::endl;
std::cout<<-profit;//<<std::endl;
}
// getch();
return 0;
}
catch(...){
std::cout<<"RunTimeError"<<std::endl;
return 0;
}
}

No comments:

Post a Comment