Warm Up in C Programming Shopping Cart 1) Create ItemToPurchase.h - Struct defin
ID: 3573949 • Letter: W
Question
Warm Up in C Programming Shopping Cart
1) Create
ItemToPurchase.h - Struct definition and related function declarations
ItemToPurchase.c - Related function definitions
main.c - main() function
Build the ItemToPurchase struct with the following specifications:
Data members:
-char itemName [ ]
-int itemPrice
-int itemQuantity
Related functionsMakeItemBlank()
-Has a pointer to an ItemToPurchase parameter. Reference Figure 7.4.1.
-Sets item's name = "none", item's description = "none", item's price = 0, item's quantity = 0
PrintItemCost()
-Has an ItemToPurchase parameter.
Ex. of PrintItemCost() output:
(2) In main(), prompt the user for two items and create two objects of the ItemToPurchase struct. Before prompting for the second item, enter the following line of code to allow the user to input a new string.
(3) Add the costs of the two items together and output the total cost.
EX:
Explanation / Answer
// ItemToPurchase.h
#ifndef ITEMTOPURCHASE_H
#define ITEMTOPURCHASE_H
struct ItemToPurchase{
char itemName[200];
int itemPrice;
int itemQuantity;
};
#endif
// ItemToPurchase.c
#include "ItemToPurchase.h"
#include <stdio.h>
#include <string.h>
void MakeItemBlank(struct ItemToPurchase *i){
strcpy(i->itemName, "none");
i->itemPrice = 0;
i->itemQuantity = 0;
}
void PrintItemCost(struct ItemToPurchase i){
printf("%s %d @ $%d = %d ", i.itemName, i.itemQuantity, i.itemPrice, i.itemPrice * i.itemQuantity);
}
// main.c
#include <stdio.h>
#include <stdio.h>
#include "ItemToPurchase.c"
int main(){
struct ItemToPurchase item1, item2;
printf("Item 1 ");
printf("Enter the item name: ");
fgets (item1.itemName, 200, stdin);
item1.itemName[strlen(item1.itemName) - 1] = '';
printf("Enter the item price: ");
scanf("%d", &item1.itemPrice);
printf("Enter the item quantity: ");
scanf("%d", &item1.itemQuantity);
printf(" Item 2 ");
printf("Enter the item name: ");
scanf(" %s", item2.itemName);
fgets (item2.itemName, 200, stdin);
item2.itemName[strlen(item2.itemName) - 1] = '';
printf("Enter the item price: ");
scanf("%d", &item2.itemPrice);
printf("Enter the item quantity: ");
scanf("%d", &item2.itemQuantity);
printf("TOTAL COST ");
int totalCost = 0;
PrintItemCost(item1);
PrintItemCost(item2);
totalCost += item1.itemPrice * item1.itemQuantity + item2.itemPrice * item2.itemQuantity;
printf("Total: $%d ", totalCost);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.