Thursday, July 9, 2009

C++ Error message?

Thanks for your previous answers. They've been eye-openers. I have a quick question. The program is to read from a file containing codes for vehicle types(perform the necessary conversions) and use the weights to perform calculations(multiplying by the user-entered tax values). Each line of the file contains the code and the weight. The weights should be integer values between 100 and 99000. Here's my input file








c 4000


t 20500


5 9000


m 500


b 10550


c 2500.8








I have a slight problem displaying the error message when a non-int value is encountered. I have tried several things with little success....Here is my output..











Enter the tax rate for MOTORCYCLES(0.01-0.99)


.25


Enter the tax rate for CARS(0.01-0.99)


45


Warning: entry invalid, rate must be between 0.01 and 0.99


Enter the tax rate for CARS(0.01-0.99)


.14


Enter the tax rate for BUSES(0.01-0.99)


89


Warning: entry invalid, rate must be between 0.01 and 0.99


Enter the tax rate for BUSES(0.01-0.99)


.28


Enter the tax rate for TRUCKS(0.01-0.99)


.26


ROAD TAX REPORT


Vehicle Type: Weight: Rate: Tax Due:





CAR 4000 0.14 560.00


TRUCK 20500 0.26 5330.00


5 9000 Error:Invalid Vehicle Code


MOTORCYCLE 500 0.25 125.00


BUS 10550 0.28 2954.00


CAR 2500 0.14 350.00


. 8 Error:Invalid Vehicle Code





It's made up of four columns(neatly arranged). As you can see, everything runs fine except for the last line. The desired output on the last row would be:


CAR 2500.8 Error: Invalid weight(non-integer)











Here is my program:














//Sample program to read input file and perform calculations with interactive values.


//Print out a table with input and calculated values.


#include%26lt;iostream%26gt;


#include%26lt;iomanip%26gt;


#include%26lt;fstream%26gt;


#include%26lt;cstring%26gt;


using namespace std;








void GetRates(float%26amp;, float%26amp;, float%26amp;, float%26amp;);





enum Vehicles{ERROR,MOTORCYCLE, CAR, BUS, TRUCK};











int main()





{


float cycleRate;


float carRate;


float busRate;


float truckRate;


char code;


int weight;


ifstream inFile;


Vehicles typeCode;














inFile.open("file1.dat");


if(!inFile)


{


cout%26lt;%26lt;"Unable to open input file, program abnormally ended"%26lt;%26lt;endl;


return 1;


}


GetRates(cycleRate, carRate, busRate, truckRate);


cout%26lt;%26lt;setw(40)%26lt;%26lt;" ROAD TAX REPORT"%26lt;%26lt;endl;


cout%26lt;%26lt;setw(2)%26lt;%26lt;"Vehicle Type:"%26lt;%26lt;setw(15)%26lt;%26lt;" Weight:"%26lt;%26lt;setw(15)%26lt;%26lt;" Rate:"%26lt;%26lt;setw(20)%26lt;%26lt;" Tax Due:"%26lt;%26lt;endl%26lt;%26lt;endl;





inFile%26gt;%26gt;code%26gt;%26gt;weight;


while(inFile)


{


{


if(!inFile)


cout%26lt;%26lt;fixed%26lt;%26lt;showpoint%26lt;%26lt;setprecision(2)%26lt;... Error:Invalid weight(non-integer)"%26lt;%26lt;endl; "Error: invalid weight(non-integer)";


}


{


if(code=='m')


typeCode=MOTORCYCLE;


else if(code=='c')


typeCode=CAR;


else if(code=='b')


typeCode=BUS;


else if(code=='t')


typeCode=TRUCK;


else


typeCode=ERROR;





}








switch(typeCode)


{


case MOTORCYCLE:cout%26lt;%26lt;fixed%26lt;%26lt;showpoint%26lt;%26lt;setpr...


break;


case CAR:cout%26lt;%26lt;fixed%26lt;%26lt;showpoint%26lt;%26lt;setprecision...


break;


case BUS:cout%26lt;%26lt;fixed%26lt;%26lt;showpoint%26lt;%26lt;setprecision...


break;


case TRUCK:cout%26lt;%26lt;fixed%26lt;%26lt;showpoint%26lt;%26lt;setprecisi...


break;


case ERROR:cout%26lt;%26lt;fixed%26lt;%26lt;showpoint%26lt;%26lt;setprecisi... Error:Invalid Vehicle Code"%26lt;%26lt;endl;





}








inFile%26gt;%26gt;code%26gt;%26gt;weight;





}





cout%26lt;%26lt;endl;


cout%26lt;%26lt;"End of Report"%26lt;%26lt;endl;


return 0;


}























void GetRates(/*out*/ float%26amp; motorcycleRate,


/*out*/ float%26amp; carsRate,


/*out*/ float%26amp; busesRate,


/*out*/ float%26amp; trucksRate)


{








bool invalidData;


invalidData=true;


while(invalidData)


{


cout%26lt;%26lt;"Enter the tax rate for MOTORCYCLES(0.01-0.99)"%26lt;%26lt;endl;


cin%26gt;%26gt;motorcycleRate;





{





if(0.009%26lt;=motorcycleRate%26amp;%26amp;motorcycleRate...


invalidData=false;





else





cout%26lt;%26lt;"Warning: entry invalid, rate must be between 0.01 and 0.99"%26lt;%26lt;endl;





}


}


invalidData=true;


while(invalidData)


{





cout%26lt;%26lt;"Enter the tax rate for CARS(0.01-0.99)"%26lt;%26lt;endl;


cin%26gt;%26gt;carsRate;


{


if(0.009%26lt;=carsRate%26amp;%26amp;carsRate%26lt;=0.999)


invalidData=false;





else





cout%26lt;%26lt;"Warning: entry invalid, rate must be between 0.01 and 0.99"%26lt;%26lt;endl;





}


}


invalidData=true;


while(invalidData)


{


cout%26lt;%26lt;"Enter the tax rate for BUSES(0.01-0.99)"%26lt;%26lt;endl;


cin%26gt;%26gt;busesRate;


{





if(0.009%26lt;=busesRate%26amp;%26amp;busesRate%26lt;=0.999)


invalidData=false;





else





cout%26lt;%26lt;"Warning: entry invalid, rate must be between 0.01 and 0.99"%26lt;%26lt;endl;





}


}


invalidData=true;


while(invalidData)


{





cout%26lt;%26lt;"Enter the tax rate for TRUCKS(0.01-0.99)"%26lt;%26lt;endl;


cin%26gt;%26gt;trucksRate;


{





if(0.009%26lt;=trucksRate%26amp;%26amp;trucksRate%26lt;=0.999)


invalidData=false;





else





cout%26lt;%26lt;"Warning: entry invalid, rate must be between 0.01 and 0.99"%26lt;%26lt;endl;





}








}





}














THANKS for your time!!

C++ Error message?
Your program is working fine, it is doing exactly what you want it to do.





The problem is that you are asking it to do the wrong thing.





The line with the problem is your input line.


inFile%26gt;%26gt;code%26gt;%26gt;weight





inFile knows that weight is a declared integer, so it is only reading an integer - that is, up to the decimal place. It is then processing that input.





When it returns again, it reads the ".8" as the code variable and returning the TypeCode Error.





You need to rethink the input statement in two ways: (1) you don't want to tell the input stream that it "weight" an integer, because that is all it will get; and (2) you want to check how code gets input so it only reads in one character.





I am not going to solve it for you - but that is where the problem is.
Reply:too long still thanks for the two points
Reply:I can't tell you what isn't working, but I can point out a prolem that may help you find it. When programming a structured or procedural (same thing, different names) program in any language one of the main goals is to make the code clear, readable, compact and well documented. I'm not able to see what delineations you've made between functions, count the functions or even trace the code effectively at this point. Do yourself a favor, take a break, relax and then look at the code you've written again and clean it up. You'll likely find your own problem in the clean up effort. It'll likely be a forehead smacker.


No comments:

Post a Comment