Question

In: Computer Science

C (1) Create three files to submit: ItemToPurchase.h - Struct definition and related function declarations ItemToPurchase.c...

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 (3 pts)
    • char itemName [ ]
    • int itemPrice
    • int itemQuantity
  • Related functions
    • MakeItemBlank() (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:

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

Solutions

Expert Solution

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
#define ITEM_TO_PURCHASE_H

//Strcuture definition
struct ItemToPurchase{  
   int itemPrice;
   int itemQuantity;
   char itemName[100];
};

//Function declarations
void MakeItemBlank(struct ItemToPurchase*);
void PrintItemCost();

#endif

ItemToPurchase.c

#include "ItemToPurchase.h"
#include<stdio.h>

//Function definitions
//Takes ItemToPurchase pointer as parameter
void MakeItemBlank(struct ItemToPurchase *item){  
   //Set default values
   strcpy(item->itemName,"none");
   item->itemPrice = 0;
   item->itemQuantity = 0;
}
//Print cost
void PrintItemCost(struct ItemToPurchase item){
   printf("%s %d @ $%d = %d\n",item.itemName,item.itemQuantity,item.itemPrice,((item.itemPrice)*(item.itemQuantity)));
}

########################################################

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 :)


Related Solutions

(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp -...
(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp - main() function (2) Build the ContactNode class per the following specifications: Parameterized constructor. Parameters are name followed by phone number. Public member functions InsertAfter() (2 pts) GetName() - Accessor (1 pt) GetPhoneNumber - Accessor (1 pt) GetNext() - Accessor (1 pt) PrintContactNode() Private data members string contactName string contactPhoneNum ContactNode* nextNodePtr Ex. of PrintContactNode() output: Name: Roxanne Hughes Phone number: 443-555-2864 (3) In main(),...
(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp -...
(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp - main() function Build the ItemToPurchase class with the following specifications: Default constructor Public class functions (mutators & accessors) SetName() & GetName() (2 pts) SetPrice() & GetPrice() (2 pts) SetQuantity() & GetQuantity() (2 pts) Private data members string itemName - Initialized in default constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 (2)...
In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main()...
In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main() method Build the ItemToBuy class with the following specifications: Private fields String itemName - Initialized in the nor-arg constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 No-arg Constructor (set the String instance field to "none") Public member methods (mutators & accessors) setName() & getName() setPrice() & getPrice() setQuantity() & getQuantity() toString()...
JAVA (1) Create two files to submit: Payroll.java - Class definition PayrollClient.java - Contains main() method...
JAVA (1) Create two files to submit: Payroll.java - Class definition PayrollClient.java - Contains main() method Build the Payroll class with the following specifications: 4 private fields String name - Initialized in default constructor to "John Doe" int ID - Initialized in default constructor to 9999 doulbe payRate - Initialized in default constructor to 15.0 doulbe hrWorked - Initialized in default constructor to 40 2 constructors (public) Default constructor A constructor that accepts the employee’s name, ID, and pay rate...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class Definition ShoppingCartPrinter.java – Contains main() method Build the ItemToPurchase class with the following specifications: Specifications Description ItemToPurchase(itemName) itemName – The name will be a String datatype and Initialized in default constructor to “none”. ItemToPurchase(itemPrice) itemPrice – The price will be integer datatype and Initialized in default constructor to 0. ItemToPurchase(itemQuantity) itemQuantity – The quantity will be integer datatype Initialized in default constructor to 0....
write a Program in C++ Using a structure (struct) for a timeType, create a program to...
write a Program in C++ Using a structure (struct) for a timeType, create a program to read in 2 times into structures, and call the method addTime, in the format: t3 = addTime(t1, t2); Make sure to use add the code to reset and carry, when adding 2 times. Also, display the resultant time using a function: display(t3);
This project requires the student to create a record (in C++ called a struct). The record...
This project requires the student to create a record (in C++ called a struct). The record should contain several fields of different data types and should allow the user to search the records to find the student with the highest grade. In this project the student should gain an understanding of the nature of records with multiple data types, how to save the records, search the records and retrieve them.  The special problems of maintaining records of multiple data types on...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
C++ Write the definition of a function minMax that has five parameters. The first three parameters...
C++ Write the definition of a function minMax that has five parameters. The first three parameters are integers. The last two are set by the function to the largest and smallest of the values of the first three parameters. The function does not return a value. The function can be used as follows: int a=31, b=5, c=19 big, small; minMax(a,b,c,&big,&small); /* big is now 31 */ /* small is now 5 */ **ONLY THE FUNCTION
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT