Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is C. Create three files to submit: ItemToPurchase.h - Struct definition an

ID: 3798638 • Letter: T

Question

This is C.

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 (3 pts)

char itemName [ ]

int itemPrice

int itemQuantity

Related functionsMakeItemBlank() (2 pts)

Has a pointer to an ItemToPurchase parameter.

Sets item's name = "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, call fflush(stdin); to allow the user to input a new string. (2 pts)

Ex:


(3) Add the costs of the two items together and output the total cost. (2 pts)

Ex:

Explanation / Answer

Solution ::

/* /////// ItemToPurchase.h ////// */ FILE 1
// header files
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<conio.h>
#define MAX-NAME-SIZE 256
static int len =2;
// Structre declarations
struct ItemToPurchase

{
char item-name[128];
int Item-Price;
int Item-Quantity;
};
// functions declarations and object of structure
struct ItemToPurchase Item[2];
void MakeItemBlank();
void PrintItemCost();
/* /////// ItemToPurchase.c //// * / FILE 2
#include "ItemToPurchase.h" //it includes the .h file
// Initially initialising with the default values
void MakeItemBlank()

{
int i;
char tmp[10]="none";
for(i=0;i<len;i++)

{
strcpy(Item[i].item-name,tmp);
Item[i].Item-Price=0;
Item[i].Item-Quantity=0;
}
  
}
// Print the ItemCost method will calculate the cost of all Item
// printing the cost and the name and the total cost of a particular Item
// printing the Total cost of all the Items
void PrintItemCost()

{
// declaration of variables
int Total-cost=0;
int cost=0;
int i;
printf("TOTAL COST ");
// for loop for calculating the cost
for(i=0;i<len;i++)

{
cost=(Item[i].Item-Price*Item[i].Item-Quantity);
printf("%s %d @ $%d=$%d ",Item[i].item-name,Item[i].Item-Quantity,Item[i].Item-Price,cost);
Total-cost+=cost;
cost=0;
}
printf(" Total:$%d ",Total-cost);
  
}

/* ///////// main.c ////////// */ FILE 3
#include "ItemToPurchase.c"
/ main function starts here
int main()

{
int i;
char name[128];
// Prompt for the user input for the number of Items
printf(" Enter the number of Items ");
scanf("%d",&len);
// prompt for the user inputs
for(i=0;i<len;i++)

{
  
printf("Item %d ",(i+1));
printf("Enter Item name:");
scanf(" %[^ ]s", name);
strcpy(Item[i].item-name,name);
printf("Enter Item price:");
scanf("%d",&Item[i].Item-Price);
printf("Enter Item quantity:");
scanf("%d",&Item[i].Item-Quantity);
}
PrintItemCost(); //calling the function for printing the total cost
return 0;
}

/// *** Thank You *** ///

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote