//Sample program to read input file and perform calculations with interactive values.
//Print out a table with input and calculated values.
#include
#include
#include
#include
using namespace std;
void GetRates(float&, float&, float&, float&);
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<<"Unable to open input file, program abnormally ended"<
return 1;
}
GetRates(cycleRate, carRate, busRate, truckRate);
cout<
cout<
inFile>>code>>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>>code>>weight;
}
switch(typeCode)
{
case MOTORCYCLE:cout<<"MOTORCYCLE";
break;
case CAR:cout<<"CAR";
break;
case BUS:cout<<"BUS";
break;
case TRUCK:cout<<"TRUCK";
break;
default:cout<<"Error: Invalid Vehicle Type";
}
{
if(code=='m')
cout<
else if (code=='c')
cout<
else if(code=='b')
cout<
else if(code=='t')
cout<
else
cout<
return 0;
}
}
void GetRates(/*out*/ float& motorcycleRate,
/*out*/ float& carsRate,
/*out*/ float& busesRate,
/*out*/ float& trucksRate)
{
bool invalidData;
invalidData=true;
while(invalidData)
{
cout<<"Enter the tax rate for MOTORCYCLES(0.01-0.99)"<
cin>>motorcycleRate;
{
if(0.009<=motorcycleRate&&motorcycleRate<=0.999)
invalidData=false;
else
cout<<"Warning: entry invalid, rate must be between 0.01 and 0.99"<
}
}
invalidData=true;
while(invalidData)
{
cout<<"Enter the tax rate for CARS(0.01-0.99)"<
cin>>carsRate;
{
if(0.009<=carsRate&&carsRate<=0.999)
invalidData=false;
else
cout<<"Warning: entry invalid, rate must be between 0.01 and 0.99"<
}
}
invalidData=true;
while(invalidData)
{
cout<<"Enter the tax rate for BUSES(0.01-0.99)"<
cin>>busesRate;
{
if(0.009<=busesRate&&busesRate<=0.999)
invalidData=false;
else
cout<<"Warning: entry invalid, rate must be between 0.01 and 0.99"<
}
}
invalidData=true;
while(invalidData)
{
cout<<"Enter the tax rate for TRUCKS(0.01-0.99)"<
cin>>trucksRate;
{
if(0.009<=trucksRate&&trucksRate<=0.999)
invalidData=false;
else
cout<<"Warning: entry invalid, rate must be between 0.01 and 0.99"<
}
}
}
2006-07-11
09:07:55
·
4 answers
·
asked by
jdegbor
1
in
Computers & Internet
➔ Programming & Design
This program is supposed to read from an input file and convert 'm' to MOTORCYCLES, 'c' to CARS, 'b' to BUS and 't' to TRUCKS. an example of an input file would be
b 3500
c 400
5 9000
m 50000
t 4200.5
It is supposed to prompt the user for 4 tax rates(in succession) for the four vehicle types. For e.g the first line from the input file has b for BUS and 3500 as the weight. The program should read the two values, multiply the weight value for bus(in this example) by the approriate tax rate for buses(gathered from user's input) and print out a trable with four columns:
Vehicle type(converted), weight, rate and total tax due(weight * rate).
I am required to use enum data type for MOTORCYCLE, CAR, BUS and TRUCK.
The program is supposed to print error messages to indicate invalid vehicle type or an invalid weight(non-integer) but go on printing and processing the rest of the values.
2006-07-11
09:25:06 ·
update #1
This is the line that specifies my output(not the heading)
{
if(code=='m')
cout<
else if (code=='c')
cout<
else if(code=='b')
cout<
else if(code=='t')
cout<
else
cout<
return 0;
2006-07-11
09:29:16 ·
update #2
this is for when the code is 'm'. All the others follow the same format except for the variables BUS, TRUCK and CAR.
if(code=='m')
cout<
2006-07-11
09:31:09 ·
update #3