12.12 Warm up: Online shopping cart (C not C++) (1) Create three files to submit
ID: 3780024 • Letter: 1
Question
12.12 Warm up: Online shopping cart (C not C++)
(1) Create three files to submit:
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:
Bottled Water 10 @ $1 = $10
(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.
Ex:
Item 1
Enter the item name: Chocolate Chips
Enter the item price: 3
Enter the item quantity: 1
Item 2
Enter the item name: Bottled Water
Enter the item price: 1
Enter the item quantity: 10
(3) Add the costs of the two items together and output the total cost.
Ex:
TOTAL COST
Chocolate Chips 1 @ $3 = $3
Bottled Water 10 @ $1 = $10
Total: $13
Construct 7.4.1: Dereferencing a pointer to a struct variableName member NameExplanation / Answer
Tested on Ubuntu,Linux
/************ItemToPurchase.h***************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static int len=2;
/*Structre declarations*/
typedef struct ItemToPurchase{
char itemName[128];
int itemPrice;
int itemQuantity;
}ItemPurchase;
/*functions declarations and object of structure*/
void MakeItemBlank(ItemPurchase*);
void PrintItemCost(ItemPurchase*);
/***************ItemToPurchase.c***************/
#include "ItemToPurchase.h"//including .h file
/*Initially initialising with default values*/
void MakeItemBlank(ItemPurchase *item) {
int i;
char temp[10]="none";
for(i=0;i<len;i++) {
strcpy(item[i].itemName,temp);
item[i].itemPrice=0;
item[i].itemQuantity=0;
}
}
/*PrintItemCost method will calculate cost of all item
* printing cost and name and total cost of particular item
* printing Total cost of all items */
void PrintItemCost(ItemPurchase *item) {
/*Variable declarations*/
int totalCost=0;
int cost=0;
int i=0;
printf("TOTAL COST ");
/*for loop for calculating cost*/
for(i=0;i<len;i++) {
cost=item[i].itemPrice*item[i].itemQuantity;
printf("%s %d @ $%d=$%d ",item[i].itemName,item[i].itemQuantity,item[i].itemPrice,cost);
totalCost+=cost;
cost=0;
}
printf("Total:$%d ",totalCost);
}
/**********************main.c*******************/
#include "ItemToPurchase.h"
/*Main function start*/
int main(){
int i;
char name[128];
ItemPurchase item[2];
/*prompt for user inputs*/
for(i=0;i<len;i++) {
printf("Item %d ",(i+1));
printf("Enter the item name:");
scanf(" %[^ ]s", name);
strcpy(item[i].itemName,name);
printf("Enter the item price:");
scanf("%d",&item[i].itemPrice);
printf("Enter the item quantity:");
scanf("%d",&item[i].itemQuantity);
}
PrintItemCost(item);
return 0;
}
/**************output*************/
raj@raj:~/Desktop/chegg$ gcc -Wall main1.c ItemToPurchase.c -o a.out -lm
raj@raj:~/Desktop/chegg$ ./a.out
Item 1
Enter the item name:Chocolate Chips
Enter the item price:3
Enter the item quantity:1
Item 2
Enter the item name:Bottled Water
Enter the item price:1
Enter the item quantity:10
TOTAL COST
Chocolate Chips 1 @ $3=$3
Bottled Water 10 @ $1=$10
Total:$13
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.