Question: I have created a shopping list which allows the user to add, change, remove,display,load and save the shopping list, thereafter I've modified the code (see
I have created a shopping list which allows the user to add, change, remove,display,load and save the shopping list, thereafter I've modified the code (see below) in order to meet the following requirements:
*Remove the restriction of a maximum of five elements. Next, you have to resize the dynamic array (list->itemList) to fit another item (remember that the dynamic array starts as empty, so this has to be done even when you add just one item). To resize a dynamic array you should use the function realloc(). Make sure that you allocate memory for the correct thing; list->itemList is an array of struct GroceryItems, so this is the type you should allocate memory for. If you allocate memory for the incorrect type, the program may still seem to work at first for a while, until it suddenly crashes.
*if the same item is added twice or more, update the value of the item (instead of having the item displayed several times in the shopping list).
My question is: I can't get the if-statement (containing malloc() and realloc()) in addItem() to work and I also need help with loadList(), as of right know it doesn't work for items with several word such as:
1. milk chocolate 1 bar
2. orange juice 2 liters
returns:
1. milk 1 bar
2. chocolate 1 bar
3. orange 2 liters
4. juice 2 liters
-> How can I change it so it doesn't look like that?
The given main.c file:
#define _CRT_SECURE_NO_WARNINGS #include
int main(void) { struct ShoppingList shoppingList; shoppingList.length = 0; // The shopping list is empty at the start shoppingList.itemList = 0;
int option;
do { printf(" Welcome to the shopping list manager! "); printf("===================================== ");
printf("1. Add an item "); printf("2. Display the shopping list "); printf("3. Remove an item "); printf("4. Change an item "); printf("5. Save list "); printf("6. Load list "); printf("7. Exit ");
printf("What do you want to do? "); scanf("%d", &option);
switch (option) { case 1: addItem(&shoppingList); break; case 2: printList(&shoppingList); break; case 3: removeItem(&shoppingList); break; case 4: editItem(&shoppingList); break; case 5: saveList(&shoppingList); break; case 6: loadList(&shoppingList); break; case 7: break; default: printf("Please enter a number between 1 and 7"); } } while (option != 7);
free(shoppingList.itemList);
return 0; }
The given header file (ShoppingList.h):
#ifndef SHOPPING_LIST_H #define SHOPPING_LIST_H
// Struct definitions
struct GroceryItem { char productName[20]; float amount; char unit[10]; };
struct ShoppingList { int length; struct GroceryItem itemList[5]; };
// Function declarations
void addItem(struct ShoppingList *list); void printList(struct ShoppingList *list); void editItem(struct ShoppingList *list); void removeItem(struct ShoppingList *list); void saveList(struct ShoppingList *list); void loadList(struct ShoppingList* list);
#endif
ShoppingList.c
#define _CRT_SECURE_NO_WARNINGS #include"ShoppingList.h" #include
void addItem(struct ShoppingList* list) { printf("===================================== "); printf("Your list contains %d items ", list->length);
while (getchar() != ' '); printf("Enter name: "); gets(list->itemList[list->length].productName);
if (list->length == 0) { list->itemList = (struct GroceryItem*)malloc(sizeof(struct GroceryItem)); } else { list->itemList = ((struct GroceryItem*)realloc(list->itemList, (list->length + 1) * sizeof(struct GroceryItem*))); printf("Memory allocated"); }
printf("Enter amount: "); scanf_s("%f", &list->itemList[list->length].amount);
while (list->itemList[list->length].amount <= 0.0) { printf("Invalid number. "); printf("Enter amount: "); scanf_s("%f", &list->itemList[list->length].amount); }
while (getchar() != ' '); printf("Enter unit: "); gets(list->itemList[list->length].unit);
list->length++;
list->itemList = (struct GroceryItem*)realloc(list->itemList, list->length * sizeof(struct GroceryItem));
strcpy(list->itemList[list->length - 1].productName, list->itemList[list->length].productName); list->itemList[list->length - 1].amount = list->itemList[list->length].amount; strcpy(list->itemList[list->length - 1].unit, list->itemList[list->length].unit);
return 0; }
void printList(struct ShoppingList *list) { printf("===================================== "); printf("Your list contains %d items: ", list->length);
if (list->length == 0) { printf("The list is empty! "); } else { for (int i = 0; i < list->length; i++) { printf("%d.%-13s \t%.1f \t%s ", i + 1, list->itemList[i].productName, list->itemList[i].amount, list->itemList[i].unit); } } }
void editItem(struct ShoppingList *list) { { if (list->length == 0) { printf("The list is empty! "); return; } printList(list); int index;
printf("Which item do you want to change? "); scanf_s("%d", &index);
while (index < 1 || index > list->length) { printf("The list contains only %d items! ", list->length); printf("Which item do you want to change? "); scanf_s("%d", &index); } index--; // Since the list is numbered from 1, but the array is numbered from 0
printf("Current amount: %.1f %s ", list->itemList[index].amount, list->itemList[index].unit); printf("Enter new amount: "); scanf_s("%f", &list->itemList[index].amount); } }
void removeItem(struct ShoppingList *list) { if (list->length == 0) { printf("The list is empty! "); return; } printf("Your list contains %d items ", list->length);
printList(list);
int index;
printf("Which item do you want to remove? "); scanf_s("%d", &index);
while (index < 1 || index > list->length) { printf("The list contains only %d items! ", list->length); printf("Which item do you want to remove? "); scanf_s("%d", &index); }
index--; // Since the list is numbered from 1, but the array is numbered from 0
printf("The item %s %.1f %s was removed from the list. ", list->itemList[index].productName, list->itemList[index].amount, list->itemList[index].unit);
// Shift all items one step to the left for (int i = index; i < list->length - 1; i++) { list->itemList[i] = list->itemList[i + 1]; }
list->length--; }
void saveList(struct ShoppingList *list)//shopping_list.txt { FILE* file; //create a pointer to file,which is the variable that will handle the file. file = fopen("shoppingList.txt", "w"); //We now open the file and "w" makes it possible to write to the file
if (file == NULL) { printf("The file could not be opened!"); return -1; }
for (int i = 0; i < list->length; i++) { fprintf(file, "%s %.1f %s ", list->itemList[i].productName, list->itemList[i].amount, list->itemList[i].unit); } fclose(file); return 0; }
void loadList(struct ShoppingList* list) { FILE* file; file = fopen("shoppingList.txt", "r"); //"r" makes it possible to read from the file
list->length = 0;
if (file == NULL) { printf("The file could not be opened!"); return -1; }
char line[100]; while (fgets(line, 100, file)) { struct GroceryItem item; fscanf(line, "%s %f %s", item.productName, &item.amount, item.unit); addItem(list, item.productName, item.amount, item.unit); }
fclose(file); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
