For this assignment, you will implement a simple grocery list program. The program will use a dynamic array to keep track of items on the list, which include both a name and a count. Review all of the sections below for specific requirements.
When you start the program, it should prompt the user to enter a command like this:
$ ./grocery-list
What do you want to do? Type one of the following commands:
add: add an item
lookup: look up an item
print: print the list
exit: exit the program
Command:
The user can then enter one of the four operations.
add CommandWhen the user types the command add, the program should ask the user to enter an item name and count like this:
What item would you like to add?
carrots
How many would you like?
3
The program should add the item to the list and then return to the main menu and ask for a new command.
If the user gives an invalid count (a negative number, or an input that isn’t a number), the program should print an error and ask the user to try again:
What item would you like to add?
avocados
How many would you like?
x
Invalid number. Try again.
How many would you like?
If the user enters an item that is already on the list you can handle this however you like. You can return an error, ask for a new count and add to the existing count, or add the item again with a new count. Just make sure your program handles this case without an error.
lookup CommandWhen the user types the command lookup, the program should ask the user to enter an item name.
If the list contains a matching item it should work like this:
What item are you looking for?
carrots
You need 3 carrots
But, if the list does not contain a matching item the output should be:
What item are you looking for?
avocados
I didn't find avocados on the list.
In either case, return to the main menu and ask for a new command.
print CommandWhen the user types the command print, print the entire list.
If the list is empty, the output should be:
Grocery List:
The list is empty
But, if the list contains items it should print them in the order they were added:
Grocery List:
carrots: 3
avocados: 9
cheese: 14
Return to the main menu and ask for a new command after printing the list.
exit CommandWhen the user types exit, the program should free all allocated memory and exit without printing any additional output.
If the user enters an invalid command, print an error and try again:
What do you want to do? Type one of the following commands:
add: add an item
lookup: look up an item
print: print the list
exit: exit the program
Command: remove
Unrecognized command.
What do you want to do? Type one of the following commands:
...
As you complete your implementation, you may assume that no line of input from the user will be longer than 128 characters. That includes command inputs, item names, and text the user types to specify a number.
Your implementation must meet the following requirements:
You may find it useful to create a struct to represent one grocery item. An item has a name and a count. Your list would be an array of these items.
You should break your program up into functions to make the code readable.
One good way to do this is to create a function for each command (except exit, perhaps).
That separates the code you write to deal with command input from the behavior of each command.
You’ll need to think carefully about how the add command can modify the grocery list array, though.
Your assignment will be graded on a scale of 0–-100 points based on the following criteria:
Makefile with correctly-defined targets all, clean, and grocery-list (10 points)tests.txt (15 points)
tests.txt format from homework 4.There will be potential deductions for any code quality violations. There are eight requirements for quality code on this assignment:
if–else block that explains what the purpose of that code is. The comment should not simply restate the code; it should add information for a human who reads the code and is confused.