Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 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[]);
  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 (argv[1] == NULL)
  28. {
  29. printf("Error: file not specified\n Format: ./a.out <input-file\n");
  30. return 0;
  31. }
  32. if (argc == 2)
  33. {
  34. struct inventory_item inventory_list[100];
  35. int count = readFile(argv[1], inventory_list);
  36. int totalNumItem = getTotalItems(inventory_list, count);
  37. float totalVal = getTotalVal(inventory_list, count);
  38. printf("Total inventory value: $%.2f\n", totalVal);
  39. printf("Total inventory quantity: %d\n", totalNumItem);
  40. }
  41. if (argc != 2)
  42. {
  43. printf("Error: Unable to open file %s/n", argv[1]);
  44. }
  45. return 0;
  46. }
  47.  
  48. int readFile(char filename[], struct inventory_item inventory_list[])
  49. {
  50. int i = 0;
  51. int numberItems = 0;
  52. FILE * ifp = fopen(filename, "r");
  53.  
  54. fscanf(ifp, "%d", &numberItems);
  55. printf("%d\n", numberItems);
  56.  
  57. for(i = 0; i < numberItems; i++)
  58. {
  59. fscanf(ifp, "%s", &inventory_list[i].productName);
  60. printf("%s\n", inventory_list[i].productName);
  61.  
  62. fscanf(ifp, "%d", &inventory_list[i].quantity);
  63. printf("%d\n", inventory_list[i].quantity);
  64.  
  65. fscanf(ifp, "%.2f", &inventory_list[i].price);
  66. printf("%.2f\n", inventory_list[i].price);
  67.  
  68. fscanf(ifp, "%s", &inventory_list[i].date);
  69. printf("%s\n", inventory_list[i].date);
  70. }
  71.  
  72. fclose(ifp);
  73. return numberItems;
  74. }
  75.  
  76. int getTotalItems(struct inventory_item inventory_list[], int count)
  77. {
  78. int totprod = 0;
  79. for(int i = 0; i < count; i++)
  80. {
  81. printf("%d\n", inventory_list[i].quantity);
  82. totprod += inventory_list[i].quantity;
  83. }
  84. return totprod;
  85. }
  86.  
  87. double getTotalVal(struct inventory_item inventory_list[], int count)
  88. {
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement