Thursday, July 9, 2009

C++..program runs fine but with errors in the output. What is my mistake?

//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{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;





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





while(inFile)








{


if(code=='m')


typeCode=MOTORCYCLE;


else if(code=='c')


typeCode=CAR;


else if(code=='b')


typeCode=BUS;


else if(code=='t')


typeCode=TRUCK;


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





}





switch(typeCode)


{


case MOTORCYCLE:cout%26lt;%26lt;"MOTORCYCLE";


break;


case CAR:cout%26lt;%26lt;"CAR";


break;


case BUS:cout%26lt;%26lt;"BUS";


break;


case TRUCK:cout%26lt;%26lt;"TRUCK";


break;


default:cout%26lt;%26lt;"Error: Invalid Vehicle Type";





}





{


if(code=='m')


cout%26lt;%26lt;fixed%26lt;%26lt;showpoint%26lt;%26lt;setprecision(2)%26lt;...


else if (code=='c')


cout%26lt;%26lt;fixed%26lt;%26lt;showpoint%26lt;%26lt;setprecision(2)%26lt;...


else if(code=='b')


cout%26lt;%26lt;fixed%26lt;%26lt;showpoint%26lt;%26lt;setprecision(2)%26lt;...


else if(code=='t')


cout%26lt;%26lt;fixed%26lt;%26lt;showpoint%26lt;%26lt;setprecision(2)%26lt;...


else


cout%26lt;%26lt;fixed%26lt;%26lt;showpoint%26lt;%26lt;setprecision(2)%26lt;...


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;





}








}





}

C++..program runs fine but with errors in the output. What is my mistake?
Would help if you showed the input, the output and the errors you are experiencing!
Reply:try to post code that doesnt have ... after things like setpre, so we can read it easier, try adding some spaces between things if it was yahoo that did that
Reply:here/..(I named the project x.cpp)


Compiling...


x.cpp


.\x.cpp(9) : error C2871: 'std' : a namespace with this name does not exist


.\x.cpp(26) : error C2065: 'ifstream' : undeclared identifier


.\x.cpp(26) : error C2146: syntax error : missing ';' before identifier 'inFile'


.\x.cpp(26) : error C2065: 'inFile' : undeclared identifier


.\x.cpp(31) : error C2228: left of '.open' must have class/struct/union


type is ''unknown-type''


.\x.cpp(34) : error C2065: 'cout' : undeclared identifier


.\x.cpp(34) : error C2065: 'endl' : undeclared identifier


.\x.cpp(39) : error C3861: 'setw': identifier not found


.\x.cpp(40) : error C3861: 'setw': identifier not found


.\x.cpp(40) : error C3861: 'setw': identifier not found


.\x.cpp(40) : error C3861: 'setw': identifier not found


.\x.cpp(40) : error C3861: 'setw': identifier not found


.\x.cpp(76) : error C2065: 'fixed' : undeclared identifier


.\x.cpp(76) : error C2065: 'showpoint' : undeclared identifier


.\x.cpp(76) : error C2065: 'setpre' : undeclared identifier


.\x.cpp(76) : error C2143: syntax error : missing ';' before '...'


.\x.cpp(76) : error C2143: syntax error : missing ';' before '...'


.\x.cpp(106) : error C2065: 'cin' : undeclared identifier


.\x.cpp(110) : error C2065: 'moto' : undeclared identifier


.\x.cpp(110) : error C2143: syntax error : missing ')' before '...'


.\x.cpp(110) : error C2143: syntax error : missing ';' before '...'


.\x.cpp(110) : warning C4390: ';' : empty controlled statement found; is this the intent?


.\x.cpp(113) : error C2181: illegal else without matching if


.\x.cpp(126) : error C2059: syntax error : '...'


.\x.cpp(129) : error C2181: illegal else without matching if


.\x.cpp(133) : error C2143: syntax error : missing ')' before '}'


.\x.cpp(133) : error C2143: syntax error : missing ';' before ')'


.\x.cpp(142) : error C2143: syntax error : missing ')' before '...'


.\x.cpp(142) : error C2143: syntax error : missing ';' before '...'


.\x.cpp(142) : warning C4390: ';' : empty controlled statement found; is this the intent?


.\x.cpp(145) : error C2181: illegal else without matching if


.\x.cpp(159) : error C2065: 'trucksRa' : undeclared identifier


.\x.cpp(159) : error C2143: syntax error : missing ')' before '...'


.\x.cpp(159) : error C2143: syntax error : missing ';' before '...'


.\x.cpp(159) : warning C4390: ';' : empty controlled statement found; is this the intent?


.\x.cpp(162) : error C2181: illegal else without matching if


also, did you forget to add %26lt;include%26gt; "stdafx.h" to yuo headers?

wildflower

No comments:

Post a Comment