Patrick B. answered 04/27/20
Math and computer tutor/teacher
//* ItemToPurchase.h ****//
#ifndef _ITEM2PURCHASE
#define MAX_NAME_LEN (255)
typedef
class ItemToPurchase
{
private:
char itemName[MAX_NAME_LEN];
int itemQuantity;
float itemPrice;
public:
ItemToPurchase();
void GetName( char * nameBuff);
inline float GetPrice() { return(itemPrice); }
inline int GetQuantity() { return(itemQuantity); }
void SetName(char * strName);
void SetPrice(float flPrice);
void SetQuantity(int qty);
} * TItemToPurchase;
#define ITEM2PURCHASE_SIZE (sizeof(ItemToPurchase))
#endif
//* ItemToPurchase.cpp
#ifndef _ITEM2PURCHASE
#include "itemToPurchase.h"
#endif
#include <string.h>
ItemToPurchase::ItemToPurchase()
{
strcpy(this->itemName,"NONE");
this->itemPrice=0.00f;
this->itemQuantity=0;
}
void ItemToPurchase::GetName( char * nameBuff)
{
strcpy(nameBuff,this->itemName);
}
void ItemToPurchase::SetName(char * strName)
{
strcpy(this->itemName,strName);
}
void ItemToPurchase::SetPrice(float flPrice)
{
this->itemPrice = flPrice;
}
void ItemToPurchase::SetQuantity(int qty)
{
this->itemQuantity = qty;
}
// Main.cpp
using namespace std;
#include <iostream>
#include <string>
#include <iomanip>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#ifndef _ITEM2PURCHASE
#include "itemToPurchase.h"
#endif
#include <string.h>
#include <stdio.h>
void InputItem (TItemToPurchase itemToPurchase, char * promptMsg=NULL)
{
string itemName;
char itemNameBuff[MAX_NAME_LEN];
int qty;
float price;
if (promptMsg!=NULL)
{
cout << "********************************************************************" << endl;
cout << promptMsg << endl;
}
cout << "****************************************************************" << endl;
//here's the tricky part of inputting the string containing multiple tokens
cout << " Please input the item name :>";
getline(cin,itemName);
strcpy(itemNameBuff, itemName.c_str());
cout << "Please input the item price :>";
cin >> price;
cout << "Please input the item quantity :>";
cin >> qty;
itemToPurchase->SetName(itemNameBuff);
itemToPurchase->SetPrice(price);
itemToPurchase->SetQuantity(qty);
//flushes the input buffer and eats the newline
cin.ignore(MAX_NAME_LEN,'\n');
return;
}
void PrintReceipt (TItemToPurchase * A, int n)
{
cout << "TOTAL COST" << endl;
float total=0;
int qty;
float price;
char itemName[MAX_NAME_LEN];
float subtotal;
for (int iLoop=0; iLoop<n; iLoop++)
{
price = A[iLoop]->GetPrice();
qty = A[iLoop]->GetQuantity();
A[iLoop]->GetName(itemName);
subtotal = price*qty;
cout << itemName << " : " << qty << " @ $" << std::fixed << setprecision(2) << price
<< " = $" << std::fixed << setprecision(2) << subtotal << endl;
total += subtotal;
}
cout << "Total: $" << std::fixed << setprecision(2) << total << endl;
}
int main(int argc, char** argv)
{
int N=2;
TItemToPurchase A[N]; // there are the 2 items to purchase: pointers to them actually
char promptMsg[255];
// inputs
for (int iLoop=0; iLoop<N; iLoop++)
{
A[iLoop] = new ItemToPurchase;
sprintf(promptMsg," ITEM # %d", (iLoop+1));
InputItem(A[iLoop],(char *) promptMsg);
}
//outputs
PrintReceipt(A,N);
//frees the memory
for (int iLoop=0; iLoop<N; iLoop++)
{
delete A[iLoop];
}
return 0;
}