Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. #define clearInput getchar() != '\n';
  7.  
  8. typedef struct {
  9. char name[15];
  10. char unit[10];
  11. float ammount;
  12. }item;
  13.  
  14.  
  15.  
  16. void newitem(item *pShopping, int index);
  17. void printList(item *ppShopping, int SizeOfList);
  18. int main(void) {
  19.  
  20.  
  21. item *shopping = NULL, *memoryTemp = NULL;
  22. int menu, flag = 1, listSize = 0;
  23. shopping = (item*)malloc(sizeof(item));
  24.  
  25.  
  26. while (flag == 1) {
  27. printf("menu: ");
  28. scanf_s("%d", &menu);
  29. clearInput;
  30. shopping;
  31. switch (menu) {
  32. case 1: memoryTemp = (item*)realloc(shopping, sizeof(item)*(listSize + 1));
  33. if (memoryTemp != NULL) {
  34. shopping = memoryTemp;
  35. newitem(shopping, listSize);
  36. }
  37. else {
  38. printf("Memory error\n");
  39. }
  40. break;
  41.  
  42. case 2: printList(shopping, listSize);
  43. break;
  44.  
  45. default: printf("Please enter a valid choice");
  46.  
  47. }
  48.  
  49.  
  50. }
  51.  
  52.  
  53. }
  54.  
  55. void newitem(item *pShopping, int index) {
  56. printf("Enter the name of the item: ");
  57. gets_s((pShopping + index)->name);
  58.  
  59. printf("Enter the unit of your item: ");
  60. gets_s((pShopping + index)->unit);
  61.  
  62. printf("Enter the ammount of your item: ");
  63. scanf_s("%f", &(pShopping + index)->ammount);
  64. }
  65. void printList(item *ppShopping, int SizeOfList) {
  66.  
  67. int i;
  68. for (i = 0; i < SizeOfList; i++) {
  69.  
  70. printf("%s\t%.2f\t%s", (ppShopping + i)->name, (ppShopping + i)->ammount, (ppShopping + i)->unit);
  71. }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement