Please, C code 7.6 Ch 7 Warm up: Online shopping cart (C) (1) Create three files
ID: 3857375 • Letter: P
Question
Please, C code
7.6 Ch 7 Warm up: Online shopping cart (C)
(1) Create three files to submit: Item ToPurchase.h Struct definition and related function declarations Item ToPurchase.c - Related function definitions main.c main() function Build the Item ToPurchase struct with the following specifications: the temToPurchase struct Data members (3 pts) o char itemName [] o int itemPrice o int itemQuantity Related functions o MakeltemBlank0 (2 pts) Has a pointer to an Item ToPurchase parameter. · Sets item's name-"none", item's price = 0, item's quantity = 0 o PrintltemCost() Has an Item ToPurchase parameter. Ex. of PrintltemCost) output: Bottled Water 10 @ $1 = $10 (2) In main), prompt the user for two items and create two objects of the Item ToPurchase struct. Before prompting for the second item, call flush(stdin); to allow the user to input a new string. (2 pts)Explanation / Answer
/******** ItemToPurchase.h ************/
#ifndef ITEMTOPURCHASE_H
#define ITEMTOPURCHASE_H
typedef struct IToP {
char *itemName;
int itemPrice;
int itemQuantity;
}ItemToPurchase;
void MakeItemBlank(ItemToPurchase*);
int PrintItemCost(ItemToPurchase*);
#endif
/******* ItemToPurchase.c *******/
#ifndef ITEMTOPURCHASE_C
#define ITEMTOPURCHASE_C
#include<stdio.h>
#include<string.h>
#include "ItemToPyrchase.h"
void MakeItemBlank(ItemToPurchase* item) {
item->itemName = malloc(sizeof(char) * 20);
strcpy(item->itemName,"None");
item->itemPrice = 0;
item->itemQuantity = 0;
}
int PrintItemCost(ItemToPurchase* item){
int total = item->itemQuantity * item->itemPrice;
printf("%s %d @ $%d = %d ", item->itemName, item->itemQuantity, item->itemPrice, total);
return total;
}
#endif
/******** main.c ****/
#include<stdio.h>
#include <ItemToPurchase.h>
int main(){
int i, total, length = 20;
ItemToPurchase* items = malloc(sizeof(ItemToPurchase) * 2);
for(i = 0; i < 2; i++) {
printf("Item %d ",i + 1);
MakeItemBlank(items + i);
printf("Enter the item name: ");
fflush(stdin);
fflush(stdout);
gets(items[i].itemName);
fflush(stdin);
printf("Enter the item price: ");
scanf("%d", &(items[i].itemPrice));
fflush(stdin);
printf("Enter the item quantity: ");
scanf("%d", &(items[i].itemQuantity));
fflush(stdin);
printf(" ");
}
for (i = 0; i < 2; i++)
total += PrintItemCost(items + i);
printf(" Total: $%d ",total);
}