Design a structure called car that holds the following information about an auto
ID: 3764499 • Letter: D
Question
Design a structure called car that holds the following information about an automobile: it’s make, as a string in a character array or string object, and the year it was built, as an integer. Write a pro- gram that asks the user how many cars to catalog. The program should use new to create a dynamic array of that many car struc- tures. Next, it should prompt the user to input the make (which may consist of more than one word) and year information for each structure. Note: this requires some care becuase it alternates reading strings with numeric data 1. Finally, the program should display the contents of each structure. Sample output should look like figure ??.
Explanation / Answer
#include <stdio.h>
#include <conio.h>
void main()
{
struct date
{
int day;
int month;
int year;
};
struct details
{
char name[20];
int price;
int code;
int qty;
struct date mfg;
};
struct details car[50];
int n,i;
clrscr();
printf("Enter number of cars:");
scanf("%d",&n);
fflush(stdin);
for(i=0;i<n;i++)
{
fflush(stdin);
printf("Car name:");
scanf("%[^ ]",car[i].name);
fflush(stdin);
printf("Car code:");
scanf("%d",&car[i].code);
fflush(stdin);
printf("Quantity:");
scanf("%d",&car[i].qty);
fflush(stdin);
printf("price:");
scanf("%d",&car[i].price);
fflush(stdin);
printf("Manufacturing date(dd-mm-yyyy):");
scanf("%d-%d-%d",&car[i].mfg.day,&car[i].mfg.month,&car[i].mfg.year);
}
printf(" ***** INVENTORY ***** ");
printf("------------------------------------------------------------------ ");
printf("S.N.| NAME | CODE | QUANTITY | PRICE |MFG.DATE ");
printf("------------------------------------------------------------------ ");
for(i=0;i<n;i++)
printf("%d %-15s %-d %-5d %-5d %d/%d/%d ",i+1,car[i].name,car[i].code,car[i].qty,car[i].price,
car[i].mfg.day,car[i].mfg.month,car[i].mfg.year);
printf("------------------------------------------------------------------ ");
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.