Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define clearInput getchar() != '\n';
- typedef struct {
- char name[15];
- char unit[10];
- float ammount;
- }item;
- void newitem(item *pShopping, int index);
- void printList(item *ppShopping, int SizeOfList);
- int main(void) {
- item *shopping = NULL, *memoryTemp = NULL;
- int menu, flag = 1, listSize = 0;
- shopping = (item*)malloc(sizeof(item));
- while (flag == 1) {
- printf("menu: ");
- scanf_s("%d", &menu);
- clearInput;
- shopping;
- switch (menu) {
- case 1: memoryTemp = (item*)realloc(shopping, sizeof(item)*(listSize + 1));
- if (memoryTemp != NULL) {
- shopping = memoryTemp;
- newitem(shopping, listSize);
- }
- else {
- printf("Memory error\n");
- }
- break;
- case 2: printList(shopping, listSize);
- break;
- default: printf("Please enter a valid choice");
- }
- }
- }
- void newitem(item *pShopping, int index) {
- printf("Enter the name of the item: ");
- gets_s((pShopping + index)->name);
- printf("Enter the unit of your item: ");
- gets_s((pShopping + index)->unit);
- printf("Enter the ammount of your item: ");
- scanf_s("%f", &(pShopping + index)->ammount);
- }
- void printList(item *ppShopping, int SizeOfList) {
- int i;
- for (i = 0; i < SizeOfList; i++) {
- printf("%s\t%.2f\t%s", (ppShopping + i)->name, (ppShopping + i)->ammount, (ppShopping + i)->unit);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement