Code:
#include<stdio.h>
#include<conio.h>
using namespace std;
struct date{
int day;
int month;
int year;
};
struct car{
char make[10];
struct date man;
struct date pur;
double price;
}fleet[10];
car addcar();
date getdate();
void readfleet(struct car[],int*);
void showfleet(struct car[],int);
void showcar(struct car);
void showdate(struct date);
void savefleet(struct car[],int);
bool validate(int,int,int);
int menu();
int main()
{bool done=false;
int choice,count=0,max=10;
do
{
choice=menu();
switch(choice)
{case 1:if(count==10)
printf("Sorry fleet full\n\n");
else
{
fleet[count]= addcar();
count++;
}
break;
case 2:if(count<=0)
printf("Sorry fleet already empty\n\n");
else
count--;
break;
case 3:showfleet(fleet,count);
break;
case 4:savefleet(fleet,count);
break;
case 5: readfleet(fleet,&count);
break;
case 6:done=true;
break;
}
}while(!done);
return 0;
}
car addcar( )
{car f;
printf("Enter car make: ");
scanf("%s",&f.make);
printf("Enter manufacture date\n");
f.man=getdate();
printf("Enter purchase date\n");
f.pur=getdate();
do
{printf("Enter purchase price: ");
scanf("%lf",&f.price);
if(f.price<12000||f.price>50000);
printf("Price out of range (12000-50000)\n");
}while(f.price<12000||f.price>50000);
printf("\n\n");
return f;
}
date getdate()
{int m,d,y;
bool val;
date dd;
printf("Enter year: ");
scanf("%d",&y);
do
{printf("Enter month: ");
scanf("%d",&m);
if(m<1||m>12)
printf("Illegal entry ");
}while(m<1||m>12);
do
{printf("Enter day: ");
scanf("%d",&d);
val=validate(d,m,y);
if(!val)
printf("Illegal entry \n");
}while(!val);
dd.month=m;
dd.day=d;
dd.year=y;
return dd;
}
bool validate(int d,int m, int y)
{int leap=0;
int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(y%400==0)
leap=1;
else if(y%4==0)
if(y%100!=0)
leap=1;
if(leap==1)
days[1]++;
if(d>days[m-1]||d<1)
return false;
return true;
}
void readfleet(struct car f[],int*count)
{FILE *in;
int i;
in = fopen("cars.txt","r");
if(in==NULL)
{printf("Error opening file for input!\n");
return;
}
for(i=0;i<10;i++)
{fscanf(in,"%s %d %d %d %d %d %d %lf",
&f[i].make,&f[i].man.day,&f[i].man.month,&f[i].man.year,&f[i].pur.day,&f[i].pur.month,&f[i].pur.year,&f[i].price);
if(feof(in))
break;
}
fclose(in);
*count=i;
printf("\n\n");
return;
}
void showfleet(struct car f[],int count)
{int i;
printf(" manufactured purchased purchase\n");
printf(" make day month year day month year price\n");
for(i=0;i<count;i++)
showcar(fleet[i]);
printf("\n\n");
return;
}
void showcar(car f)
{printf("%7s ",f.make);
showdate(f.man);
showdate(f.pur);
printf("$%8.2lf\n", f.price);
return;
}
void showdate(date d)
{printf("%2d %2d %2d ",d.day,d.month,d.year);
return;
}
void savefleet(struct car f[],int count)
{int i;
FILE *out;
out = fopen("cars.txt","w");
if(out==NULL)
{printf("Error opening file for output!\n");
return;
}
for(i=0;i<count;i++)
fprintf(out,"%s %d %d %d %d %d %d %lf\n",
f[i].make,f[i].man.day,f[i].man.month,f[i].man.year,f[i].pur.day,f[i].pur.month,f[i].pur.year,f[i].price);
fclose(out);
printf("\n\n");
return;
}
void add(int);
int main(int argc, char **argv)
{
int i = 2;
add(i);
return 0;
}
void add(int a)
{
a += 5;
}
int menu()
{int choice;
do
{printf("1. add a car\n");
printf("2. delete last car\n");
printf("3. display the fleet on the screen\n");
printf("4. save the fleet to a file\n");
printf("5. read the fleet from a file\n");
printf("6. exit program\n");
scanf("%d",&choice);
if(choice<1||choice>6)
printf("Illegal entry - try again\n\n");
}while(choice<1||choice>6);
return choice;
}