Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*Language C please* we will focus on adding, removing and editing grocery items, and we will limit the number of items to a maximum of

*Language C please*

we will focus on adding, removing and editing grocery items, and we will limit the number of items to a maximum of 5. In lab 7 we will extend this to have an unlimited number of items as well as saving and loading shopping lists.

Below are a couple of example runs so that you can get an impression of how the finished program is supposed to work.

Example run (user input in bold):

Welcome to the shopping list manager!

=====================================

1.Add an item

2.Display the shopping list

3.Remove an item

4.Change an item

5.Save list

6.Load list

7.Exit

What do you want to do? 1

Enter name: Potatoes

Enter amount: 1.5

Enter unit: kg

Potatoes was added to the shopping list.

Welcome to the shopping list manager!

=====================================

1.Add an item

2.Display the shopping list

3.Remove an item

4.Change an item

5.Save list

6.Load list

7.Exit

What do you want to do? 1

Enter name: Milk

Enter amount: 2

Enter unit: litre

Milk was added to the shopping list.

Welcome to the shopping list manager!

=====================================

1.Add an item

2.Display the shopping list

3.Remove an item

4.Change an item

5.Save list

6.Load list

7.Exit

What do you want to do? 2

Your list contains 2 items:

Potatoes 1.5 kg

Milk 2 litre

Welcome to the shopping list manager!

=====================================

1.Add an item

2.Display the shopping list

3.Remove an item

4.Change an item

5.Save list

6.Load list

7.Exit

What do you want to do? 7

Goodbye!

Set of .c and .h files that contains the menu and empty function definitions. Your task will be to provide implementation for the empty functions.

ShoppingList.h

This file consists of struct definitions and function declarations for the functions that you will implement. The functions should be somewhat self-explanatory, but you will find detailed instructions of what each function should do in lab 6 and 7.

It also contains two data types: one for a grocery item (struct GroceryItem) and one for a shopping list (struct ShoppingList). The GroceryItem struct contains three members: the name of the item (as a string), the amount of the item (as a float) and finally a string representing which unit the amount is measured in (e.g., kg, litre etc). The unit is represented as a string.

The ShoppingList struct has two members: an array of grocery items and the current length of the shopping list. As you can see, the array of items is currently of length 5. The length member keeps track of how long the shopping list currently is, and it is initially zero (as you can see in the main()-function). In lab 6, the list cannot be longer than 5 items:

#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

This file consists of a bunch of functions with empty function bodies. This is where your main work will be. Your task is to fill these functions with appropriate code:

#define _CRT_SECURE_NO_WARNINGS

#include"ShoppingList.h"

#include

#include // For malloc() and free()

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)

{

}

main.c

This file contains the main()-function and the menu that operates the program:

#define _CRT_SECURE_NO_WARNINGS

#include

#include "ShoppingList.h"

int main(void)

{

struct ShoppingList shoppingList;

shoppingList.length = 0; // The shopping list is empty at the start

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);

return 0;

}

Your task is to fill their empty function bodies (in ShoppingList.c). It is allowed, and encouraged, to write additional functions to support the functionality of the program.

you should not have to do any changes to the files ShoppingList.h and main.c.

Task 3.1 - addItem()

This function should add an item to your grocery list. As you can see, this function (and all other functions) receives a pointer to a shopping list. This is a pointer to the shopping list defined in main(), and your functions should make changes directly to the shopping list that the argument list is pointing to. Check shoppingList.h to see exactly how the types struct ShoppingList and struct GroceryItem are defined.

The function should ask the user for a name, amount and unit of an item (see the example run above). It should then place it last in the shopping list. The length field should always be containing the index to the last position of the array, so this is where the item should be placed. For instance, length is zero when the list is empty, so the first time this function is called, the item should be placed at index zero in list->itemArray.

This function is also responsible for increasing the length of the list. Since we just added one item, the list is now one item longer. Just remember that it should not be possible to add more than 5 items to the list.

If the user enters a non-positive number for amount, the program should tell the user that the input is invalid and repeat the question until a correct answer has been given.

Hint: Remember, the argument list is a pointer to the Shopping list-struct which in turn contains an array of grocery items.

So, for example, to access the amount for the first item in the list (which is a float variable), we can write:

list->itemList[0].amount

If we wanted to access the name of the second item in the list (which is a string), we can write:

list->itemList[1].name

We use the -> operator because list is a pointer to a struct.

Task 3.2 - printList()

This function should print the entire shopping list vertically aligned to the screen. Each item should also be numbered, starting from 1 (note however, that the first element will still be stored at index 0 in the array, as usual).

For example:

Bananas 12 pieces

Milk 1.5 litre

The easiest way to make the text aligned is to use tabs to separate the text (tabs are made with the special character '\t'). If you want, you can also try to align the numbers nicely by specifying the number of decimals and left/right alignment there. Check the reference for printf() for more information or scroll down a bit on this page for an example: http://alvinalexander.com/programming/printf-format-cheat-sheet Links to an external site..

If the list is empty (i.e., if the length member is zero), the text the list is empty should be printed instead of a list.

Task 3.3 - editItem()

The user should be able to change the amount of a specific item by first prompting the user to enter a number indicating which item to change, and then asking for a new amount. See the example below:

Example:

Your list contains 2 items:

1:Potatoes 1.5 kg

2.Milk 2 litre

3.Add an item

4.Display the shopping list

5.Remove an item

6.Change an item

7.Save list

8.Load list

9.Exit

What do you want to do? 4 Which item do you want to change? 10

The list contains only 2 items!

What do you want to do? 4

Which item do you want to change? 2

Current amount: 2 litre

Enter new amount: 5

Add an item

Display the shopping list

Remove an item

Change an item

Save list

Load list

Exit

What do you want to do? 2

Your list contains 2 items:

Potatoes 1.5 kg

Milk 5 litre (note how the amount have changed to 5)

As seen in the example, the program should check whether the user enters a valid item.

Task 3.4 - removeItem()

The user should be able to choose an item from the list and remove it. Keep in mind that it is not possible by physically remove elements from an array, every index in the array must contain something.

Instead, remove an item by shifting all the content above the removed element one step to the left, overwriting the previous item. For example, if it is the item on index 2 that should be removed, the item on index 3 should be moved to index 2, the item on index 4 should be moved to index 3, etc.

Also, remember that the list should now contain one item less, so update the length member of the list struct.

*Language C please*

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning VB 2008 Databases

Authors: Vidya Vrat Agarwal, James Huddleston

1st Edition

1590599470, 978-1590599471

More Books

Students also viewed these Databases questions