Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* John Falcone
- * COP2220 Intro to Programming in C
- * user defined struct example
- */
- #define _CRT_SECURE_NO_WARNINGS
- #include <string.h>
- #include <stdio.h>
- #define SIZE 20
- typedef struct{
- char make[SIZE];
- char model[SIZE];
- int year;
- double dealerCost;
- double price;
- }vehicle;
- //Function Prototypes
- // fills the data fields of a vehicle instance
- //returns the filled vehicle
- vehicle FillVehicle();
- //fills the data fields of a vehicle instance
- //by reference using a pointer to a vehicle
- void FillVehiclePtr(vehicle *vehicleptr);
- //fills an array of vehicles
- void FillVehicleArray(vehicle arrayC[], int *size);
- //displays one vehicle
- void DisplayVehicle(vehicle anyVehicle);
- int main()
- {
- //Declare variables
- vehicle myVehicle, myVehicle1, myVehicle2;
- vehicle manyVehicles[SIZE];
- int cSize;
- int i;
- //Fill structures with a function
- myVehicle = FillVehicle();
- myVehicle1 = FillVehicle();
- //print using display function
- printf("\n---------Display myVehicle\n");
- DisplayVehicle(myVehicle);
- printf("\n---------Display myVehicle1\n");
- DisplayVehicle(myVehicle1);
- //Fill structure using pointers and dispay it
- FillVehiclePtr(&myVehicle2);
- printf("\n---------Display myVehicle2\n");
- DisplayVehicle(myVehicle2);
- //Fill the array with the function
- printf("\n---------Fill array manyVehicles\n");
- FillVehicleArray(manyVehicles, &cSize);
- //display an array of vehicles
- printf("\n---------Display array manyVehicles\n");
- for (i = 0; i<cSize; i++)
- {
- DisplayVehicle(manyVehicles[i]);
- }
- return 0;
- }
- //Function Definitions
- // fills the data fields of a vehicle instance
- //returns the filled vehicle
- vehicle FillVehicle()
- {
- //Declare local variables
- vehicle tempC;
- //prompt and get information
- printf("\nplease enter the make of your vehicle: ");
- scanf("%s", tempC.make);
- //print to check
- printf("make: %s\n", tempC.make);
- //prompt and get information
- printf("\nplease enter the model of your vehicle: ");
- scanf("%s", tempC.model);
- //print to check
- printf("model: %s\n", tempC.model);
- //prompt and get information
- printf("\nplease enter the year of your vehicle: ");
- scanf("%d", &tempC.year);
- printf("year: %d\n", tempC.year);
- return tempC;
- }
- //displays one vehicle
- void DisplayVehicle(vehicle anyVehicle)
- {
- printf("\n\nmake: %s\n", anyVehicle.make);
- printf("model: %s\n", anyVehicle.model);
- printf("year: %d\n", anyVehicle.year);
- }
- //fills the data fields of a vehicle instance
- //by reference using a pointer to a vehicle
- void FillVehiclePtr(vehicle *vehicleptr)
- {
- //prompt and get information
- printf("\nplease enter the make of your vehicle: ");
- scanf("%s", (*vehicleptr).make);
- //prompt and get information
- printf("\nplease enter the model of your vehicle: ");
- scanf("%s", vehicleptr->model);
- //prompt and get information
- printf("\nplease enter the year of your vehicle: ");
- scanf("%d", &(*vehicleptr).year);
- }
- //fills an array of vehicles
- void FillVehicleArray(vehicle arrayC[], int *size)
- {
- int i;
- //prompt the user
- printf("\nenter the number of vehicles: ");
- scanf("%d", size);
- //print to check
- printf("size: %d\n", *size);
- for (i = 0; i < *size; i++)
- {
- printf("enter make: ");
- scanf("%s", arrayC[i].make);
- printf("enter model: ");
- scanf("%s", arrayC[i].model);
- printf("enter year: ");
- scanf("%d", &arrayC[i].year);
- }
- }
- /*please enter the make of your vehicle: king
- make: king
- please enter the model of your vehicle: spade
- model: spade
- please enter the year of your vehicle: 10
- year: 10
- please enter the make of your vehicle: three
- make: three
- please enter the model of your vehicle: heart
- model: heart
- please enter the year of your vehicle: 3
- year: 3
- ---------Display myVehicle
- make: king
- model: spade
- year: 10
- ---------Display myVehicle1
- make: three
- model: heart
- year: 3
- please enter the make of your Vehicle: king
- please enter the model of your vehicle: diamonds
- please enter the year of your vehicle: 10
- ---------Display myVehicle2
- make: king
- model: diamonds
- year: 10
- ---------Fill array manyVehicles
- enter the number of vehicles: 3
- size: 3
- enter make: jack
- enter model: clubs
- enter year: 10
- enter make: four
- enter model: hearts
- enter year: 4
- enter make: ace
- enter model: spades
- enter year: 11
- ---------Display array manyVehicles
- make: jack
- model: clubs
- year: 10
- make: four
- model: hearts
- year: 4
- make: ace
- model: spades
- year: 11
- Press any key to continue . . .*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement