- #1
dudforreal
- 116
- 0
Got some errors when compiling and not sure what is the problem and how to fix it
Code:
# include<stdio.h>
#define FLEETSIZE 10
typedef struct
{
int day, month, year;
}Date_t;
typedef struct
{
char make[15];
Date_t manufactureDate;
Date_t purchaseDate;
double purchasePrice;
}Car_t;
void initFleet(Car_t fleet[]);
void showDate(Date_t dateToPrint, int whichDate);
double getPurchasePrice();
void addCar(Car_t *carToAdd, int *currentCar);
void clearFleetElement(Car_t *toClear);
void showCar(Car_t toPrint);
void showFleet(Car_t fleet[]);
int getDate(Date_t *toGet, int whichDate);
double getPurchasePrice()
{
double price;
printf("Please enter the purchase price:\n$");
scanf("%lf", &price);
return(price);
}
void addCar(Car_t *carToAdd, int *currentCar)
{
printf("\n\nEnter car make:\n");
scanf("%s", carToAdd->make);
getDate(&(carToAdd->manufactureDate), 1);
getDate(&(carToAdd->purchaseDate), 0);
(carToAdd->purchasePrice) = getPurchasePrice();
currentCar++;
}
void clearFleetElement(Car_t *toClear)
{
toClear->make[0] = 0;
toClear->manufactureDate.day = 0;
toClear->manufactureDate.month = 0;
toClear->manufactureDate.year = 0;
toClear->purchaseDate.day = 0;
toClear->purchaseDate.month = 0;
toClear->purchaseDate.year = 0;
toClear->purchasePrice = 0;
}
void showDate(Date_t dateToPrint, int whichDate)
{
if (whichDate==0)
printf("%d/%d/%d", dateToPrint.day, dateToPrint.month, dateToPrint.year);
if (whichDate==1)
printf("%d/%d/%d", dateToPrint.day, dateToPrint.month, dateToPrint.year);
}
void showCar(Car_t toPrint)
{
printf("Car Make:\t\t\t\t%s", toPrint.make);
printf("\nManufacture Date:\t\t\t");
showDate(toPrint.manufactureDate, 1);
printf("\nPurchase Date:\t\t\t\t");
showDate(toPrint.purchaseDate, 0);
printf("\nPurchase Price:\t\t\t\t$%.2lf", toPrint.purchasePrice);
}
void showFleet(Car_t fleet[])
{
int i;
for(i=0; i<FLEETSIZE; i++)
{
printf("\n\nCar number: %d\n", (i+1));
showCar(fleet[i]);
}
}
int getDate(Date_t *toGet, int whichDate)
{
int dayCheck=0, monthCheck=0, yearCheck=0;
if (whichDate==0)
printf("\n\nPlease enter date of purchase:");
if (whichDate==1)
printf("\n\nPlease enter date of manufacture:");
do
{
printf("\nEnter the date:\nYear:");
scanf("%d", &toGet->year);
if(toGet->year < 2011 && toGet->year > 1900)
yearCheck = 1;
else
printf("Invalid selection, please re-enter");
}while(!yearCheck);
do
{
printf("\nEnter the date:\nMonth:");
scanf("%d", &toGet->month);
if(toGet->month <13 && toGet->month > 0)
monthCheck = 1;
else
printf("Invalid selection, please re-enter");
}while(!monthCheck);
do
{
printf("\nEnter the date:\nDay:");
scanf("%d", &toGet->day);
if(toGet->day <31 && toGet->day >1)
dayCheck = 1;
else
printf("Invalid selection, please re-enter");
}while(!dayCheck);
return 0;
}
void initFleet(Car_t fleet[])
{
int i=0;
for(i=0; i<FLEETSIZE; i++)
{
clearFleetElement(&fleet[i]);
}
}
readFleet(Car_t toRead[])
{
FILE *fFleet;
fFleet = fopen("Fleet_File.bin", "rb");
fread(toRead,10*sizeof(Car_t),1,fFleet);
fclose(fFleet);
}
saveFleet(Car_t toSave[])
{
FILE *fFleet;
int i;
fFleet = fopen("Fleet_File.bin", "wb");
fwrite(toSave, 10*sizeof(Car_t), 1, fFleet);
fclose(fFleet);
}
int main()
{
Car_t fleet[FLEETSIZE];
int currentCar = 0, menuSelectInteger = 0, menuLoopControl = 1;
initFleet(fleet);
do
{
printf("\n\nMain menu:\n\n");
printf("Options:\n\n");
printf("1. Add a car to fleet.\n");
printf("2. Delete last car from the fleet.\n");
printf("3. Display full fleet listing.\n");
printf("4. Save the fleet to a file.\n");
printf("5. Read the fleet from a file.\n");
printf("0. Exit the program\n");
printf("\nPlease enter your choice number:\n\n");
scanf("%d", &menuSelectInteger);
switch(menuSelectInteger)
{
case 1:
addCar(&fleet[currentCar], ¤tCar);
currentCar++;
printf("Current car: %d", currentCar);
break;
case 2:
if (currentCar == 0)
printf("No further cars to delete from fleet(currentCar=%d)\n", currentCar);
else
{
clearFleetElement(&fleet[(currentCar-1)]);
currentCar--;
}
break;
case 3:
printf("Option 3 selected");
showFleet(fleet);
break;
case 4:
saveFleet(fleet);
printf("\n%d\n", menuSelectInteger);
break;
case 5:
readFleet(fleet);
currentCar=FLEETSIZE;
printf("\n%d\n", menuSelectInteger);
break;
case 0:
printf("Thank you for using this program!");
menuLoopControl=0;
break;
default:
printf("Invalid selection. Please make a selection between 1 and 5, or enter 0 (zero) to exit.");
break;
}
}while(menuLoopControl);
system("pause");
return 0;
}
Last edited by a moderator: