#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAXCARS 5
#define MAXSTRING 80 //80 being the number of characters for one whole line
//structure for a car data type
typedef struct
{
char make[MAXSTRING];
char colour[MAXSTRING];
int age;
}CAR;
//function prototypes
void ShowMenu(CAR cars[MAXCARS]);
void AddData(CAR cars[MAXCARS]);
void DisplayCars(CAR cars[MAXCARS]);
void DisplayOne(CAR cars[MAXCARS]);
void main(void)
{
CAR cars[MAXCARS];
clrscr();
printf("size of car data structure is %d bytes\n", sizeof(CAR));
printf("size of *cars is %d bytes\n", sizeof(CAR *));
printf("size of array cars is %d bytes\n ", sizeof(cars));
getch();
ShowMenu(cars);
}
void ShowMenu(CAR cars[MAXCARS])
{
int choice;
clrscr();
do
{
printf("Car Program\n");
printf("1. Add Data\n");
printf("2. Display All\n");
printf("3. Display one\n");
printf("4. Quit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: AddData(cars);
break;
case 2: DisplayCars(cars);
break;
case 3: DisplayOne(cars);
break;
case 4: printf("Quitting Program\n");
break;
default: printf("Enter 1 to 3\n");
}
}
while(choice != 4);
}
void AddData(CAR cars[MAXCARS])
{
int i; //looping variable
for(i=0; i<MAXCARS; i++)
{
printf("Enter make ");
scanf("%s", cars[i].make);
printf("Enter colour ");
scanf("%s", cars[i].colour);
printf("Enter age ");
scanf("%d", &cars[i].age);
}
}
void DisplayCars(CAR cars[MAXCARS])
{
int i; //looping variable
for(i=0; i<MAXCARS; i++)
{
printf("Make: %s\n", cars[i].make);
printf("Colour: %s\n", cars[i].colour);
printf("Age: %d\n", cars[i].age);
}
}
void DisplayOne(CAR cars[MAXCARS])
{
char cartofind[MAXSTRING];
int i;
printf("Enter car to find ");
scanf("%s", cartofind);
for(i=0; i<MAXCARS; i++)
{
if(strcmp(cars[i].make, cartofind) == 0)
{
printf("Make: %s\n", cars[i].make);
printf("Colour: %s\n", cars[i].colour);
printf("Age: %d\n", cars[i].age);
}
}
}
#include <stdio.h>
#include <conio.h>
#define MAXCARS 5
#define MAXSTRING 80 //80 being the number of characters for one whole line
//structure for a car data type
typedef struct
{
char make[MAXSTRING];
char colour[MAXSTRING];
int age;
}CAR;
//function prototypes
void ShowMenu(CAR *cars[MAXCARS]);
void AddData(CAR *cars[MAXCARS]);
void DisplayCars(CAR *cars[MAXCARS]);
void DisplayOne(CAR *cars[MAXCARS]);
void main(void)
{
CAR *cars[MAXCARS];
clrscr();
printf("size of car data structure is %d bytes\n", sizeof(CAR));
printf("size of *cars is %d bytes\n", sizeof(*cars));
printf("size of array cars is %d bytes\n ", sizeof(cars));
getch();
ShowMenu(cars);
}
void ShowMenu(CAR *cars[MAXCARS])
{
int choice;
clrscr();
do
{
printf("Car Program\n");
printf("1. Add Data\n");
printf("2. Display All\n");
printf("3. Display one\n");
printf("4. Quit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: AddData(cars);
break;
case 2: DisplayCars(cars);
break;
case 3: DisplayOne(cars);
break;
case 4: printf("Quitting Program\n");
break;
default: printf("Enter 1 to 3\n");
}
}
while(choice != 4);
}
void AddData(CAR *cars[MAXCARS])
{
int i; //looping variable
for(i=0; i<MAXCARS; i++)
{
printf("Enter make ");
scanf("%s", cars[i]->make);
printf("Enter colour ");
scanf("%s", cars[i]->colour);
printf("Enter age ");
scanf("%d", &cars[i]->age);
}
}
void DisplayCars(CAR *cars[MAXCARS])
{
int i; //looping variable
for(i=0; i<MAXCARS; i++)
{
printf("Make: %s\n", cars[i]->make);
printf("Colour: %s\n", cars[i]->colour);
printf("Age: %d\n", cars[i]->age);
}
}
void DisplayOne(CAR *cars[MAXCARS])
{
char cartofind[MAXSTRING];
int i;
printf("Enter car to find ");
scanf("%s", cartofind);
for(i=0; i<MAXCARS; i++)
{
if(strcmp(cars[i]->make, cartofind) == 0)
{
printf("Make: %s\n", cars[i]->make);
printf("Colour: %s\n", cars[i]->colour);
printf("Age: %d\n", cars[i]->age);
}
}
}