Question
Having some trouble finshing up a C project. From the looks of it I believe my shopping chart is not updateing correctly. If I could
Having some trouble finshing up a C project. From the looks of it I believe my shopping chart is not updateing correctly.
If I could have some help with this I would appericate it.
My error
How output should look
Code follows below.
ItemToPurchase.c
#include "ItemToPurchase.h" #include
void MakeItemBlank(ItemToPurchase* item){ strcpy((*item).itemName, "none"); strcpy((*item).itemDescription, "none"); (*item).itemPrice=0; (*item).itemQuantity=0; }
void PrintItemCost(ItemToPurchase item){ printf("%s %d @ $%d = $%d ", item.itemName, item.itemQuantity, item.itemPrice, (item.itemPrice*item.itemQuantity)); }
void PrintItemDescription(ItemToPurchase item){ printf("%s: %s. ", item.itemName, item.itemDescription); }
ItemToPurcahse.h
#ifndef ITEM_TO_PURCHASE_H #define ITEM_TO_PURCHASE_H
typedef struct ItemToPurchase_struct { char itemName[50]; char itemDescription[50]; int itemPrice; int itemQuantity; } ItemToPurchase;
void MakeItemBlank(ItemToPurchase *item);
void PrintItemCost(ItemToPurchase item);
void PrintItemDescription(ItemToPurchase item);
#endif
main.c
#include
char PrintMenu(ShoppingCart usrShopping) {
char menuOp = ' ';
printf("MENU "); printf("a - Add item to cart "); printf("r - Remove item from cart "); printf("c - Change item quantity "); printf("i - Output items' descriptions "); printf("o - Output shopping cart "); printf("q - Quit ");
while (menuOp != 'a' && menuOp != 'r' && menuOp != 'c' && menuOp != 'i' && menuOp != 'o' && menuOp != 'q') { printf("Choose an option: "); gets(&menuOp); }
if (menuOp == 'a') { ItemToPurchase item;
printf("ADD ITEM TO CART ");
printf("Enter the item name: "); gets(item.itemName); printf("Enter the item description: "); gets(item.itemDescription); printf("Enter the item price: "); scanf("%d", &item.itemPrice); printf("Enter the item quantity: "); scanf("%d", &item.itemQuantity);
AddItem(item, usrShopping);
printf(" ");
menuOp = ' ';
} else if (menuOp == 'r') { printf("REMOVE ITEM FROM CART ");
/*char name[50];
printf("Enter name of item to remove:"); fgets(name, 50, stdin);
RemoveItem(name, usrShopping);*/
menuOp = ' '; } else if (menuOp == 'c') { printf("CHANGE ITEM QUANTITY "); /*char name[50];
printf("Enter the item name:"); fgets(name, 50, stdin);
int i = 0;
while (strcmp(name, usrShopping.cartItems[i].itemName) != 0) { ++i; }
ModifyItem(usrShopping.cartItems[i], usrShopping);*/
menuOp = ' '; } else if (menuOp == 'i') { printf("OUTPUT ITEM'S DESCRIPTIONS "); PrintDescriptions(usrShopping); menuOp = ' ';
} else if (menuOp == 'o') { printf("OUTPUT SHOPPING CART "); PrintTotal(usrShopping); menuOp = ' '; }
return menuOp; }
int main() {
ShoppingCart usrShopping; usrShopping.cartSize = 0;
printf("Enter Customer's Name: "); gets(usrShopping.customerName); printf("Enter Today's Date: "); gets(usrShopping.currentDate);
printf("Customer Name: %s ", usrShopping.customerName); printf("Today's Date: %s ", usrShopping.currentDate);
char menuChoice = ' ';
while (menuChoice != 'q') { menuChoice = PrintMenu(usrShopping); }
return 0; }
ShoppingCart.c
#include "ShoppingCart.h" #include
ShoppingCart AddItem(ItemToPurchase item, ShoppingCart cart) { cart.cartItems[cart.cartSize] = item; ++cart.cartSize; return cart; }
/*ShoppingCart RemoveItem(char name[], ShoppingCart cart) { int i = 0;
while (strcmp(name, cart.cartItems[i].itemName) != 0) { ++i; }
for(i; i
}*/
ShoppingCart ModifyItem(ItemToPurchase item, ShoppingCart cart) { /*int quantity;
printf("Enter the new quantity:"); scanf("%d", quantity);
int i = 0;
while (strcmp(item.itemName, cart.cartItems[i].itemName) != 0) { ++i; }
cart.cartItems[i].itemQuantity = quantity;*/ }
int GetNumItemsInCart(ShoppingCart cart) { return cart.cartSize; }
int GetCostOfCart(ShoppingCart cart) { int total = 0; int temp = 0;
for (int i = 0; i
void PrintTotal(ShoppingCart cart) { int total = 0; int temp = 0;
printf("%s\'s Shopping Cart - %s ", cart.customerName, cart.currentDate); printf("Number of Items: %d ", cart.cartSize);
if (cart.cartSize == 0) { printf("SHOPPING CART IS EMPTY "); printf("Total: $0 "); } else { for (int i = 0; i
void PrintDescriptions(ShoppingCart cart) {
printf("%s's Shopping Cart - %s ", cart.customerName, cart.currentDate);
printf("Item Descriptions "); for (int i = 0; i
}
#include "ShoppingCart.h" #include
ShoppingCart AddItem(ItemToPurchase item, ShoppingCart cart) { cart.cartItems[cart.cartSize] = item; ++cart.cartSize; return cart; }
/*ShoppingCart RemoveItem(char name[], ShoppingCart cart) { int i = 0;
while (strcmp(name, cart.cartItems[i].itemName) != 0) { ++i; }
for(i; i
}*/
ShoppingCart ModifyItem(ItemToPurchase item, ShoppingCart cart) { /*int quantity;
printf("Enter the new quantity:"); scanf("%d", quantity);
int i = 0;
while (strcmp(item.itemName, cart.cartItems[i].itemName) != 0) { ++i; }
cart.cartItems[i].itemQuantity = quantity;*/ }
int GetNumItemsInCart(ShoppingCart cart) { return cart.cartSize; }
int GetCostOfCart(ShoppingCart cart) { int total = 0; int temp = 0;
for (int i = 0; i
void PrintTotal(ShoppingCart cart) { int total = 0; int temp = 0;
printf("%s\'s Shopping Cart - %s ", cart.customerName, cart.currentDate); printf("Number of Items: %d ", cart.cartSize);
if (cart.cartSize == 0) { printf("SHOPPING CART IS EMPTY "); printf("Total: $0 "); } else { for (int i = 0; i
void PrintDescriptions(ShoppingCart cart) {
printf("%s's Shopping Cart - %s ", cart.customerName, cart.currentDate);
printf("Item Descriptions "); for (int i = 0; i
}
ShopingCart.h
#ifndef SHOPPING_CART_H #define SHOPPING_CART_H
#include "ItemToPurchase.h"
typedef struct ShoppingCart_struct { char customerName[50]; char currentDate[50]; ItemToPurchase cartItems[10]; int cartSize; } ShoppingCart;
ShoppingCart AddItem(ItemToPurchase item, ShoppingCart cart);
ShoppingCart RemoveItem(char name[], ShoppingCart cart);
ShoppingCart ModifyItem(ItemToPurchase item, ShoppingCart cart);
int GetNumItemsInCart(ShoppingCart cart);
int GetCostOfCart(ShoppingCart cart);
void PrintTotal(ShoppingCart cart);
void PrintDescriptions(ShoppingCart cart);
#endif
Choose an option ADD ITEM TO CART Enter the item name: Enter the item description: Enter the item price Enter the item quantity: MENU a Add item to cart r Remove item from cart Change item quantity c output items' descriptions i o Output shopping cart Quit Your output ends with Choose an option Choose an option OUTPUT SHOPPING CART John Doe's Shopping Cart February 1, 2016 Number of Items: 0 SHCP PING CART IS EMPTY Total: $0 MENU a Add item to cart r Remove item from cart c Change item quantity output items' descriptions i o Output shopping cart Quit Choose an optionStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started