Question: Write in C language, please see the direction below Thank you Description: The purpose of this assignment is to provide practice programming using structs and
Write in C language, please see the direction below
Thank you
Description: The purpose of this assignment is to provide practice programming using structs and pointers. Your program will accept user input and store it in a dynamic array of structs. First, you will define a structure to describe a item to be bought. Your program will prompt the user for the initial size of the array. It will then allocate memory from the heap for an array called ShoppingCart. Once this is done, your program will allow the user to enter data for as many items as s/he wishes. If the user wishes to add more items than the initial size of the array will allow, you will use calls to malloc() and memcpy() to increase the size of the array so that the user can continue to add more items. See the instructions below for specific details. Specifications: Define a struct (Item struct) appropriate for holding item information. This should include an item name (a cstring containing 25 characters), quantity (a size_t) and price (an double). You may typedef this struct to a new name if you wish. At the start of the program, ask the user for the number of items, and use this number as the initial size of your ShoppingCart. (Hint: for easier testing, use a small number, such as two or three.) Allocate an appropriate amount of memory from the heap to create a dynamic array (named ShoppingCart) of Item structs, large enough to hold the number of items entered by the user. Provide a menu that allows the user to choose among the following options:
Add an item to ShoppingCart - This will prompt the user to enter information about the item (Name, quantity, and price), and save this in the next uninitialized element in ShoppingCart.
Print the current list of items - This will print all items in the shopping cart
Quit the program
Create a function called "ResizeArray" to be used whenever the number of items to be added to ShoppingCart would exceed the current bounds of the array. This function must return void. The user should not be aware that the size of the array is changing. Rather, s/he should simply be allowed to keep adding items until s/he is done, and ResizeArray should be called (transparently to the user) whenever the number of items to be added would exceed the bounds of the array so that the user may add as many items as s/he likes. Each call to ResizeArray should double the size of the existing ShoppingCart. If by any chance all heap memory is exhausted, an appropriate error message should be issued to the user. Make sure you test your function by adding more items than originally requested at the start of your program. Be sure to include comments within your code that explain in high-level terms what the various parts of your code are doing. Other Specifications:
You may NOT use realloc() for this assignment.
With the exception of those specifically disallowed, use whatever functions, parameters and return statements you wish to organize your code and ensure that it works correctly.
This what I got, please fix it.
It has segment fault.
#include
#include
#include
typedef struct { char name[26]; size_t quantity; double price; }item;
void print_Menu();
item getInput(int count);
item * resizeArray(item *arr, int currentSize );
void printitems(item* arr, int n);
int main()
{
int n , count = 0;
char buffer[25];
printf("Enter the number of Students ");
fgets(buffer, 25, stdin);
sscanf(buffer, "%d", &n);
item *itemArray;
itemArray = (item *) malloc((n) * sizeof(item));
while(1)
{
print_Menu();
char input;
input = getchar();
getchar();//newline
switch(input)
{
case 'A':
case 'a':
if(count == n)
{
itemArray = resizeArray(itemArray, count);
n += 5;
}
itemArray[count] = getInput(count);
count++;
break;
case 'P':
case 'p':
printitems(itemArray, count);
break;
case 'Q':
case 'q':
printf("Good Bye! ");
exit(0);
break;
default:
printf("Invalid Choice, Please Try Again ");
}
}
}
void print_Menu()
{
printf("Add a item to cart ------- A ");
printf("Print the current list of items --- P ");
printf("Quit the program ---------------------- Q ");
}
item getInput(int num)
{
item ite; char buf[100]; printf("Enter item name: ");
fgets(buf, 100, stdin);
sscanf(buf, "%s", ite.name); printf("Enter item quantity: ");
fgets(buf, 100, stdin);
sscanf(buf, "%zu", ite.quantity); printf("Enter item price: ");
fgets(buf, 100, stdin);
sscanf(buf, "%d", ite.price);
getchar(); return ite;
}
void printitems(item* arr, int n)
{
int i ;
for(i = 0; i < n; i++)
{
printf(" Name: %s", arr[i].name); printf(" Quantity: %d", arr[i].quantity); printf(" Price: %d", arr[i].price);
}
}
item* resizeArray(item* arr, int currentSize)
{
item* newArr = (item *) malloc((currentSize + 5) * sizeof(item));
memcpy(newArr, arr, currentSize * sizeof(item));
free(arr);
return newArr;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
