***** C language******* In main.c Build the ItemToPurchase strict with the follo
ID: 3807241 • Letter: #
Question
***** C language*******
In main.c Build the ItemToPurchase strict with the following specifications: Data members char itemName [] int item Price int itemQuantity and create two functions - MakeItemBlank()- Has a pass by reference ItemToPurchase parameter. Reference Figure 7.4.1. - Sets item's name = "none", item's price = 0. item's quantity = 0 - PrintltemCost() - Has a pass by value ItemToPurchase parameter. Ex. of PrintItemCost() output: Bottled Water 10 e $1 = $10 In main(), prompt the user for two items and create two objects of the ItemToPurchase struct. Before prompting for the second item, enter fflush(stdin): to allow the user to input a new string. 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 Add the costs of the two items together and output the total cost. Ex: TOTAL COST Chocolate Chips 1 e $3 = §3 Bottled Water 10 e $1 = $10 Total: $13Explanation / Answer
here is the program in c language
#include <stdio.h>
struct ItemToPurchase
{
char itemName[50];
int itemPrice;
int itemQuantity;
};
void display(struct ItemToPurchase item1, struct ItemToPurchase item2);
int main()
{
struct ItemToPurchase item1;
struct ItemToPurchase item2;
printf("Item1 ");
printf("Enter the item name: ");
scanf("%[^ ]s", item1.itemName);
printf(" Enter the item price: ");
scanf("%d", &item1.itemPrice);
printf(" Enter the item quantity: ");
scanf("%d", &item1.itemQuantity);
fflush(stdin);
printf(" Item2 ");
printf("Enter the item name: ");
scanf("%[^ ]s", item2.itemName);
printf(" Enter the item price: ");
scanf("%d", &item2.itemPrice);
printf(" Enter the item quantity: ");
scanf("%d", &item2.itemQuantity);
display(item1,item2);
return 0;
}
void display(struct ItemToPurchase item1, struct ItemToPurchase item2)
{
printf("TOTAL COST ");
printf("%s",item1.itemName);
printf(" %d",item1.itemQuantity);
printf(" @ $");
printf("%d",item1.itemPrice);
printf(" = $%d",item1.itemPrice*item1.itemQuantity);
printf(" ");
printf("%s",item2.itemName);
printf(" %d",item2.itemQuantity);
printf(" @ $");
printf("%d",item2.itemPrice);
printf(" = $%d",item2.itemPrice*item2.itemQuantity);
printf(" Total: $%d",(item1.itemPrice*item1.itemQuantity)+(item2.itemPrice*item2.itemQuantity));
}
output:-
Item1
Enter the item name: choclate chips
Enter the item price: 2
Enter the item quantity: 1
Item2
Enter the item name: bottled chips
Enter the item price: 3
Enter the item quantity: 10
TOTAL COST
choclate chips 1 @ $2 = $2
bottled chips 10 @ $3 = $30
Total: $32
here in this program i have create a structure named as ItemToPurchase which has 2 datatype and 3 variables as itemName, itemPrice, and itemQuantity.
char itemName[50];
int itemPrice;
int itemQuantity;
then i have decalred a function named as display to display final results like:-
void display(struct ItemToPurchase item1, struct ItemToPurchase item2);
in the main() method i have make the 2 objects of structure and by using those taking the input from user
struct ItemToPurchase item1;
struct ItemToPurchase item2;
printf("Enter the item name: ");
scanf("%[^ ]s", item1.itemName);
printf(" Enter the item price: ");
scanf("%d", &item1.itemPrice);
printf(" Enter the item quantity: ");
scanf("%d", &item1.itemQuantity);
fflush(stdin);
printf(" Item2 ");
printf("Enter the item name: ");
scanf("%[^ ]s", item2.itemName);
printf(" Enter the item price: ");
scanf("%d", &item2.itemPrice);
printf(" Enter the item quantity: ");
scanf("%d", &item2.itemQuantity);
and finally calling the function display to display the results like
void display(struct ItemToPurchase item1, struct ItemToPurchase item2)
{
printf("TOTAL COST ");
printf("%s",item1.itemName);
printf(" %d",item1.itemQuantity);
printf(" @ $");
printf("%d",item1.itemPrice);
printf(" = $%d",item1.itemPrice*item1.itemQuantity);
printf(" ");
printf("%s",item2.itemName);
printf(" %d",item2.itemQuantity);
printf(" @ $");
printf("%d",item2.itemPrice);
printf(" = $%d",item2.itemPrice*item2.itemQuantity);
printf(" Total: $%d",(item1.itemPrice*item1.itemQuantity)+(item2.itemPrice*item2.itemQuantity));
}
finally its adding both the results value and displayig final output result.
Item1
Enter the item name: choclate chips
Enter the item price: 2
Enter the item quantity: 1
Item2
Enter the item name: bottled chips
Enter the item price: 3
Enter the item quantity: 10
TOTAL COST
choclate chips 1 @ $2 = $2
bottled chips 10 @ $3 = $30
Total: $32
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.