#include<stdio.h> #include<conio.h> using namespace std; struct date{ int day; i
ID: 3611038 • Letter: #
Question
#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 ");
else
{
fleet[count]= addcar();
count++;
}
break;
case 2:if(count<=0)
printf("Sorry fleet already empty ");
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 ");
f.man=getdate();
printf("Enter purchase date ");
f.pur=getdate();
do
{printf("Enter purchase price: ");
scanf("%lf",&f.price);
if(f.price<12000||f.price>100000);
printf("Price out of range(12000-100000) ");
}while(f.price<12000||f.price>100000);
printf(" ");
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 ");
}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("Car's specification.txt","r");
if(in==NULL)
{printf("Error opening file for input! ");
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(" ");
return;
}
void showfleet(struct car f[],int count)
{int i;
printf(" manufactured purchased purchase ");
printf(" make day monthyear day month year price ");
for(i=0;i<count;i++)
showcar(fleet[i]);
printf(" ");
return;
}
void showcar(car f)
{printf("%7s ",f.make);
showdate(f.man);
showdate(f.pur);
printf("$%8.2lf ", 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! ");
return;
}
for(i=0;i<count;i++)
fprintf(out,"%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);
fclose(out);
printf(" ");
return;
}
int menu()
{int choice;
do
{printf("1. add a car ");
printf("2. delete last car ");
printf("3. display the fleet on the screen ");
printf("4. save the fleet to a file ");
printf("5. read the fleet from a file ");
printf("6. exit program ");
scanf("%d",&choice);
if(choice<1||choice>6)
printf("Illegal entry - tryagain ");
}while(choice<1||choice>6);
return choice;
}
Explanation / Answer
please rate -thanks all the functions except validate are call by reference, theychange the values directly in the memory location, as opposed toreturning the value in a variableRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.