In: Computer Science
C
(1) Create three files to submit:
Build the ItemToPurchase struct with the following specifications:
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 code to allow the user to input a new string. c is declared as a char. (2 pts)
c = getchar(); while(c != '\n' && c != EOF) { c = getchar(); }
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. (2 pts)
Ex:
TOTAL COST Chocolate Chips 1 @ $3 = $3 Bottled Water 10 @ $1 = $10 Total: $13
Current file for main.c
Current file for ItemToPurchase.c
Current file for ItemToPurchase.h
Here is the answer for your question in C Programming Language.
Kindly upvote if you find the answer helpful.
###################################################################
CODE :
ItemToPurchase.h
#ifndef ITEM_TO_PURCHASE_H //Strcuture definition //Function declarations #endif |
ItemToPurchase.c
#include "ItemToPurchase.h" //Function definitions |
########################################################
main.c
#include "ItemToPurchase.c" #include<stdlib.h> void main(){ //Required variables char name[100],c; int price,quantity,total = 0; //Read item 1 values printf("Item 1 \n"); printf("Enter the item name: \n"); gets(name); printf("Enter the item price: \n"); scanf("%d",&price); printf("Enter the item quantity: \n"); scanf("%d",&quantity); //Create item1 object and assign values read struct ItemToPurchase item1; strcpy(item1.itemName,name); item1.itemPrice = price; item1.itemQuantity = quantity; c = getchar(); while(c != '\n' && c != EOF) { c = getchar(); } //Read item 2 values printf("\nItem 2 \n"); printf("Enter the item name: \n"); gets(name); printf("Enter the item price: \n"); scanf("%d",&price); printf("Enter the item quantity: \n"); scanf("%d",&quantity); //Create item2 object and assign values read struct ItemToPurchase item2; strcpy(item2.itemName,name); item2.itemPrice = price; item2.itemQuantity = quantity; //Calculate total total += (item1.itemPrice*item1.itemQuantity) + (item2.itemPrice*item2.itemQuantity); //Print output printf("\nTOTAL COST\n"); PrintItemCost(item1); PrintItemCost(item2); printf("\nTotal : $%d",total); } |
#################################################################
SCREENSHOTS :
Please see the screenshots of the code below for the indentations of the code.
ItemToPurchase.h
###########################################################################
ItemToPurchase.c
##################################################################
main.c
########################################################################
OUTPUT :
Any doubts regarding this can be explained with pleasure :)