Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. struct inventory_item
  7. {
  8. char productName[50];
  9. int quantity;
  10. float price;
  11. char id[9];
  12. char date[15];
  13. };
  14.  
  15.  
  16. int readFile(char filename[], struct inventory_item inventory_list[], int count);
  17. int getTotalItems(struct inventory_item inventory_list[], int count);
  18. double getTotalVal(struct inventory_item inventory_list[], int count);
  19.  
  20. void printItem(struct inventory_item item)
  21. {
  22. // This is an optional function
  23. }
  24.  
  25. int main(int argc, char *argv[])
  26. {
  27. if (argc == 2)
  28. {
  29. struct inventory_item inventory_list[100];
  30. int count = readFile(argv[1], inventory_list);
  31. int totalNumItem = getTotalItems(inventory_list, count);
  32. float totalVal = getTotalVal(inventory_list, count);
  33. printf("Total inventory value: $%.2f\n", totalVal);
  34. printf("Total inventory quantity: %d\n", totalNumItem);
  35. }
  36. else
  37. {
  38. printf("Error: Unable to open file %s/n", argv[1]);
  39. }
  40. return 0;
  41. }
  42.  
  43. int readFile(char filename[], struct inventory_item inventory_list[], int count)
  44. {
  45. int i = 0;
  46. int numberItems = 0;
  47. FILE * ifp = fopen(filename, "r");
  48.  
  49. fscanf(ifp, "%d", &numberItems);
  50. printf("d\n", numberItems);
  51.  
  52. for(i = 0; i < numberItems; i++)
  53. {
  54. fscanf(ifp, "%s", &inventory_list[i].productName);
  55. printf("%s\n", inventory_list[i].productName);
  56.  
  57. fscanf(ifp, "%d", &inventory_list[i].quantity);
  58. printf("%d\n", inventory_list[i].quantity);
  59.  
  60. fscanf(ifp, "%.2f", &inventory_list[i].price);
  61. printf("%.2f\n", inventory_list[i].price);
  62.  
  63. fscanf(ifp, "%s", &inventory_list[i].date);
  64. printf("%s\n", inventory_list[i].date);
  65. }
  66.  
  67. fclose(ifp);
  68. return numberItems;
  69. }
  70.  
  71. int getTotalItems(struct inventory_item inventory_list[], int count)
  72. {
  73. int totprod = 0;
  74. for(int i = 0; i < count; i++)
  75. {
  76. printf("%d\n", inventory_list[i].quantity);
  77. totprod += inventory_list[i].quantity;
  78. }
  79. printf("%d", totprod);
  80. }
  81.  
  82. double getTotalVal(struct inventory_item inventory_list[], int count)
  83. {
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement