Ok, I'm posting my *.cpp program here, I also have a *.h file with my function prototypes and struct definition included.
Code:
// Josh Stevanus Prog 5
// last program
using namespace std;
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype>
#include "cs116prog5.h"
int main()
{
ifstream inFile;
SalesItem itemList[NUM_ITEMS]; // the table for the whole sale items o
int itemsPurchased[NUM_ITEMS]; // the quantity of each item per each e
char cont;
char upc;
float subTotal;
float itemTotal;
float total;
float dailyTotal = 0.00;
if (!inFile)
{
cout << "Cannot open the input file!" << endl;
return 1;
}
initialize_item_list (itemList); // initialize the item table
cout << endl << "Welcome to the POST system!" << endl << endl
<< "Beginning a new sale? (Y/N) ";
cin >> cont;
cont = toupper(cont);
while (cont == 'Y')
{
initialize_items_purchase_list (itemsPurchased);
accept_item_code(upc); // get a valid item code
cout << endl << endl << endl << "item code done";
while (upc != 'X')
{
get_item_name (itemList, upc); // prints the item name for upc
int quantity = get_quantity (itemsPurchased);
cout << " item total: $ ";
itemTotal = get_item_total(itemList, upc, quantity);
cout << fixed << showpoint << setprecision(2) << itemTotal
<< endl << endl;
accept_item_code(upc); // SELF-ADDED FUNCTION CALL
}
subTotal = list_sales_items (itemList,itemsPurchased);
total = get_total(subTotal, TAX_RATE);
// Computes the total sales amount with tax
dailyTotal += total;
cout << "Beginning a new sale? (Y/N) ";
cin >> cont;
cont = toupper(cont);
}
cout << endl << "===================" << endl
<< "The total sale for the day is $" << setw(7) << fixed
<< setprecision(2) << dailyTotal << endl << endl
<< "Thanks for using POST system. Goodbye." << endl;
return 0;
}
// function definitions
void initialize_item_list (SalesItem itemList[])
{
ifstream inFile;
int i = 0;
inFile.open("items.txt"); // read items data from an input file
inFile >> itemList[0].upc; // priming read
while (inFile)
{
inFile >> itemList[i].name;
inFile >> itemList[i].price;
i++;
inFile >> itemList[i].upc;
}
cout << "INITIALIZE_ITEM_LIST FUNCTION COMPLETE" << endl; // DEBUG
}
void initialize_items_purchase_list (int itemsPurchased[])
{
cout << "INITIALIZE_ITEMS_PURCHASE_LIST STARTED"; // DEBUGGING
for (int i=0; i < NUM_ITEMS; i++)
{
itemsPurchased[i] = 0;
}
}
void accept_item_code (char& upc)
{
bool valid = false;
cout << endl << "--------------------" << endl;
cout << "Enter product code: ";
cin >> upc;
upc = toupper(upc);
cout << upc;
cout << endl;
while (!valid)
{
if (upc == 'A' || upc == 'B' || upc == 'C' || upc == 'D' || upc == 'E')
{
valid = true;
}
else if (upc == 'F' || upc == 'G' || upc == 'H' || upc == 'I' || upc
== 'X')
{
valid = true;
}
else
{
cout << "!!! Invalid product code" << endl << endl
<< "Enter product code: ";
cin >> upc;
upc = toupper(upc);
cout << endl;
}
}
return;
}
void get_item_name (const SalesItem itemList[], char upc)
{
int i=0;
while (i < NUM_ITEMS)
{
if (itemList[i].upc == upc)
{
cout << setw(18) << "item name: " << itemList[i].name;
}
}
}
int get_quantity (int itemsPurchased[])
{
static int i=0;
int item;
cout << endl << "Enter quantity: ";
cin >> itemsPurchased[i];
item = itemsPurchased[i];
cout << endl;
i++;
return item;
}
float get_item_total (const SalesItem itemList[], char upc, int quantity)
{
float itemTotal;
for (int i=0; i < NUM_ITEMS; i++)
{
if (itemList[i].upc == upc)
{
itemTotal = itemList[i].price * quantity;
}
}
return itemTotal;
}
float list_sales_items (const SalesItem itemList[],const int itemsPurchased[])
// Lists the quantity, name, and price for all sale items, and
// returns the subtotal of all items purchased
{
float subTotal = 0;
float itemTotal = 0;
cout << "----------------------------" << endl << "Items list:" << endl;
for (int i=0; i < NUM_ITEMS; i++)
{
if (itemsPurchased[i] != 0)
{
cout << setw(5) << itemsPurchased[i] << " " << itemList[i].name
<< setw(10) << "$";
itemTotal = itemsPurchased[i] * itemList[i].price;
cout << setw(7) << itemTotal << endl;
subTotal = subTotal + itemTotal;
}
}
return subTotal;
}
float get_total(float subTotal, float TAX_RATE)
{ // Computes the total sales amount with tax
float tendered;
float total;
cout << "Subtotal" << setw(13) << "$" << setw(7) << subTotal << endl
<< "Total with Tax (6%) $" << setw(7) << fixed << showpoint
<< setprecision(2);
total = subTotal * TAX_RATE;
cout << total << endl << "Tendered amount $ ";
cin >> tendered;
cout << endl << "Change $" << setw(7)
<< tendered - total << endl << "----------------------------"
<< endl << endl;
return total;
}
The user inputs an item code, then the program prints the corresponding name. A text file contains a list of each product code, name, and price and my array and struct stores that information. The user also then enters the quantity of the item, and then the total cost is printed. The user continues entering code, quantity, price, until they are finished they type "X" as their product code. Then, a list is printed including the quantity, name, and price of each item, then the subtotal, then total with tax, then the user enters the amount "tendered", then the change is printed. The program then asks if they want to "begin a new sale" and if they enter "N" the "total sale for the day" is printed and the program exits.