This is the problem in the code and please make sure highlight thecode you made
ID: 3611407 • Letter: T
Question
This is the problem in the code and please make sure highlight thecode you made changes.if car was manufactured year for e.g. 2002 and was purchased2004. This make sense in normal lives. In program if i putmanufactured year for e.g. 2004 and purchased year 2002, how canthis make sense. Manufactured year is alway older or equal topurchased year.
#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>50000);
printf("Price out of range(12000-50000) ");
}while(f.price<12000||f.price>50000);
printf(" ");
return f;
}
date getdate()
{int m,d,y;
bool val;
date dd;
do
{
printf("Enter year: ");
scanf("%d",&y);
if(y<1975||y>2010)
printf("Illegal entry ");
}while(y<1980||y>2010);
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("cars.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 I think all the changes are highlighted #include #include 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); bool checkdate(struct date ,struct date); 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(countpur.year) return false; else if(man.year==pur.year) if(man.month>pur.month) return false; elseif(man.month==pur.month) if(man.day>pur.day) return false; return true; } 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(); while(!checkdate(f.man,f.pur)) {printf("can not purchase beforemanufactured "); printf("manufactured%d/%d/%d ",f.man.month,f.man.day,f.man.year); printf("Enter purchase date "); f.pur=getdate(); } {printf("Enter purchase price: "); scanf("%lf",&f.price); if(f.price50000); printf("Price out of range(12000-50000) "); }while(f.price50000); printf(" "); return f; } date getdate() {int m,d,y; bool val; date dd; do { printf("Enter year: "); scanf("%d",&y); if(y2010) printf("Illegal entry "); }while(y2010); do {printf("Enter month: "); scanf("%d",&m); if(m12) printf("Illegal entry "); }while(m12); 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]||dRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.