C program: (1) Create three files to submit: ItemToPurchase.h - Struct definitio
ID: 3920710 • Letter: C
Question
C program:
(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:
(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)
Ex:
(3) Add the costs of the two items together and output the total cost. (2 pts)
Ex:
Submission Instructions
Deliverables
ItemToPurchase.c
,
ItemToPurchase.h
and
main.c
You must submit these file(s)
Compile command
gcc ItemToPurchase.c main.c -Wall -o a.out -lm
We will use this command to compile your code
Submit your files below by dragging and dropping into the area or choosing a file on your hard drive.
ItemTo...ase.c
Drag file hereorChoose on hard drive.
ItemTo...ase.h
Drag file hereorChoose on hard drive.
main.c
Drag file hereorChoose on hard drive.
Explanation / Answer
main.cpp
#include<iostream>
#include<string>
#include "ItemToPurchase.h"
using namespace std;
int main()
{
ItemToPurchase item1Cart, item2Cart;
string itemName;
int itemPrice;
int itemQuantity;
int totalCost = 0;
cout << "Item 1:" << endl;
cout << "Enter the item name : " << endl;
getline(cin, itemName);
cout << "Enter the item price : " << endl;
cin >> itemPrice;
cout << "Enter the item quantity : " << endl;
cin >> itemQuantity;
item1Cart.SetName(itemName);
item1Cart.SetPrice(itemPrice);
item1Cart.SetQuantity(itemQuantity);
cin.ignore();
cout << "Item 2:" << endl;
cout << "Enter the item name : " << endl;
getline(cin, itemName);
cout << "Enter the item price : " << endl;
cin >> itemPrice;
cout << "Enter the item quantity : " << endl;
cin >> itemQuantity;
item2Cart.SetName(itemName);
item2Cart.SetPrice(itemPrice);
item2Cart.SetQuantity(itemQuantity);
cout << "TOTAL COST : " << endl;
cout << item1Cart.GetName() << " " << item1Cart.GetQuantity() << " @ $" << item1Cart.GetPrice() << " = " << (item1Cart.GetQuantity()*item1Cart.GetPrice()) << endl;
cout << item2Cart.GetName() << " " << item2Cart.GetQuantity() << " @ $" << item2Cart.GetPrice() << " = " << (item2Cart.GetQuantity()*item2Cart.GetPrice()) << endl;
totalCost = (item1Cart.GetQuantity()*item1Cart.GetPrice()) + (item2Cart.GetQuantity()*item2Cart.GetPrice());
cout << "Total : $" << totalCost << endl;
return 0;
}
------------------------------------------------------------
ItemToPurchase.h
#ifndef ITEMTOPURCHASE_H_INCLUDED
#define ITEMTOPURCHASE_H_INCLUDED
#include<string>
#include <iostream>
using namespace std;
class ItemToPurchase
{
public:
ItemToPurchase();
void SetName(string ItemName);
void SetPrice(int itemPrice);
void SetQuantity(int itemQuantity);
string GetName();
int GetPrice();
int GetQuantity();
private:
string itemName;
int itemPrice;
int itemQuantity;
};
#endif
------------------------------------------------------------------------------
ItemToPurchase.cpp:
#include <iostream>
#include <string>
#include "ItemToPurchase.h"
using namespace std;
ItemToPurchase::ItemToPurchase()
{
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
}
void ItemToPurchase::SetName(string name)
{
itemName = name;
}
void ItemToPurchase::SetPrice(int itemPrice)
{
this->itemPrice = itemPrice;
}
void ItemToPurchase::SetQuantity(int itemQuantity)
{
this->itemQuantity = itemQuantity;
}
string ItemToPurchase::GetName()
{
return itemName;
}
int ItemToPurchase::GetPrice()
{
return itemPrice;
}
int ItemToPurchase::GetQuantity()
{
return itemQuantity;
}
------------------------------------------------------------------------
Please try executing these files.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.