Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. /* John Falcone
  2. * COP2220 Intro to Programming in C
  3. * user defined struct example
  4. */
  5.  
  6. #define _CRT_SECURE_NO_WARNINGS
  7.  
  8. #include <string.h>
  9. #include <stdio.h>
  10. #define SIZE 20
  11.  
  12. typedef struct{
  13. char make[SIZE];
  14. char model[SIZE];
  15. int year;
  16. double dealerCost;
  17. double price;
  18. }vehicle;
  19.  
  20. //Function Prototypes
  21.  
  22. // fills the data fields of a vehicle instance
  23. //returns the filled vehicle
  24. vehicle FillVehicle();
  25.  
  26. //fills the data fields of a vehicle instance
  27. //by reference using a pointer to a vehicle
  28. void FillVehiclePtr(vehicle *vehicleptr);
  29.  
  30. //fills an array of vehicles
  31. void FillVehicleArray(vehicle arrayC[], int *size);
  32.  
  33.  
  34. //displays one vehicle
  35. void DisplayVehicle(vehicle anyVehicle);
  36.  
  37. int main()
  38. {
  39.  
  40. //Declare variables
  41. vehicle myVehicle, myVehicle1, myVehicle2;
  42. vehicle manyVehicles[SIZE];
  43. int cSize;
  44. int i;
  45.  
  46. //Fill structures with a function
  47. myVehicle = FillVehicle();
  48. myVehicle1 = FillVehicle();
  49.  
  50. //print using display function
  51. printf("\n---------Display myVehicle\n");
  52. DisplayVehicle(myVehicle);
  53. printf("\n---------Display myVehicle1\n");
  54. DisplayVehicle(myVehicle1);
  55.  
  56. //Fill structure using pointers and dispay it
  57. FillVehiclePtr(&myVehicle2);
  58. printf("\n---------Display myVehicle2\n");
  59. DisplayVehicle(myVehicle2);
  60.  
  61. //Fill the array with the function
  62. printf("\n---------Fill array manyVehicles\n");
  63. FillVehicleArray(manyVehicles, &cSize);
  64.  
  65. //display an array of vehicles
  66. printf("\n---------Display array manyVehicles\n");
  67. for (i = 0; i<cSize; i++)
  68. {
  69. DisplayVehicle(manyVehicles[i]);
  70. }
  71.  
  72. return 0;
  73. }
  74.  
  75. //Function Definitions
  76.  
  77.  
  78. // fills the data fields of a vehicle instance
  79. //returns the filled vehicle
  80. vehicle FillVehicle()
  81. {
  82. //Declare local variables
  83. vehicle tempC;
  84. //prompt and get information
  85. printf("\nplease enter the make of your vehicle: ");
  86. scanf("%s", tempC.make);
  87. //print to check
  88. printf("make: %s\n", tempC.make);
  89.  
  90. //prompt and get information
  91. printf("\nplease enter the model of your vehicle: ");
  92. scanf("%s", tempC.model);
  93. //print to check
  94. printf("model: %s\n", tempC.model);
  95.  
  96. //prompt and get information
  97. printf("\nplease enter the year of your vehicle: ");
  98. scanf("%d", &tempC.year);
  99. printf("year: %d\n", tempC.year);
  100. return tempC;
  101. }
  102.  
  103.  
  104.  
  105. //displays one vehicle
  106. void DisplayVehicle(vehicle anyVehicle)
  107. {
  108. printf("\n\nmake: %s\n", anyVehicle.make);
  109. printf("model: %s\n", anyVehicle.model);
  110. printf("year: %d\n", anyVehicle.year);
  111. }
  112.  
  113.  
  114. //fills the data fields of a vehicle instance
  115. //by reference using a pointer to a vehicle
  116. void FillVehiclePtr(vehicle *vehicleptr)
  117. {
  118. //prompt and get information
  119. printf("\nplease enter the make of your vehicle: ");
  120. scanf("%s", (*vehicleptr).make);
  121.  
  122. //prompt and get information
  123. printf("\nplease enter the model of your vehicle: ");
  124. scanf("%s", vehicleptr->model);
  125.  
  126. //prompt and get information
  127. printf("\nplease enter the year of your vehicle: ");
  128. scanf("%d", &(*vehicleptr).year);
  129. }
  130.  
  131.  
  132.  
  133.  
  134. //fills an array of vehicles
  135. void FillVehicleArray(vehicle arrayC[], int *size)
  136. {
  137. int i;
  138. //prompt the user
  139. printf("\nenter the number of vehicles: ");
  140. scanf("%d", size);
  141.  
  142. //print to check
  143. printf("size: %d\n", *size);
  144.  
  145. for (i = 0; i < *size; i++)
  146. {
  147. printf("enter make: ");
  148. scanf("%s", arrayC[i].make);
  149.  
  150. printf("enter model: ");
  151. scanf("%s", arrayC[i].model);
  152.  
  153. printf("enter year: ");
  154. scanf("%d", &arrayC[i].year);
  155. }
  156. }
  157.  
  158.  
  159.  
  160. /*please enter the make of your vehicle: king
  161. make: king
  162.  
  163. please enter the model of your vehicle: spade
  164. model: spade
  165.  
  166. please enter the year of your vehicle: 10
  167. year: 10
  168.  
  169. please enter the make of your vehicle: three
  170. make: three
  171.  
  172. please enter the model of your vehicle: heart
  173. model: heart
  174.  
  175. please enter the year of your vehicle: 3
  176. year: 3
  177.  
  178. ---------Display myVehicle
  179.  
  180.  
  181. make: king
  182. model: spade
  183. year: 10
  184.  
  185. ---------Display myVehicle1
  186.  
  187.  
  188. make: three
  189. model: heart
  190. year: 3
  191.  
  192. please enter the make of your Vehicle: king
  193.  
  194. please enter the model of your vehicle: diamonds
  195.  
  196. please enter the year of your vehicle: 10
  197.  
  198. ---------Display myVehicle2
  199.  
  200.  
  201. make: king
  202. model: diamonds
  203. year: 10
  204.  
  205. ---------Fill array manyVehicles
  206.  
  207. enter the number of vehicles: 3
  208. size: 3
  209. enter make: jack
  210. enter model: clubs
  211. enter year: 10
  212. enter make: four
  213. enter model: hearts
  214. enter year: 4
  215. enter make: ace
  216. enter model: spades
  217. enter year: 11
  218.  
  219. ---------Display array manyVehicles
  220.  
  221.  
  222. make: jack
  223. model: clubs
  224. year: 10
  225.  
  226.  
  227. make: four
  228. model: hearts
  229. year: 4
  230.  
  231.  
  232. make: ace
  233. model: spades
  234. year: 11
  235. Press any key to continue . . .*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement