Skip to content

Instantly share code, notes, and snippets.

@NickersF
Last active January 10, 2016 08:20
Show Gist options
  • Save NickersF/1a0eb7c1254452f4ebe4 to your computer and use it in GitHub Desktop.
Save NickersF/1a0eb7c1254452f4ebe4 to your computer and use it in GitHub Desktop.
CS162 Project 1: Full Program Source
// author: Nicholas Fazzolari
// project: Shopping budget tracker
// date: 1/6/2016
#include <iostream>
#include <iomanip>
using namespace std;
// this constant sets the _max_ number of array elements
// for the struct arrays in ShoppingCart 512 seems like a reasonable amount
const int N_OF_ELEMS = 512;
// shopping cart struct
struct ShoppingCart
{
// use two arrays. one for name of item, other for price
int lengthOfList = 0;
double itemPrice[N_OF_ELEMS];
char itemName[N_OF_ELEMS][32];
} newShoppingCart;
// add item to cart function (prototype)
void addItems(double total, double budget);
// print total function (prototype)
void printTotal(double total);
int main() {
// numeric variables
double budget = 0.0;
double total = 0.0;
double getNum = 0.0;
// format the output stream for currency USD
cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);
cout.precision(2);
// welcome message
cout << "************************" << endl;
cout << "Welcome to Tesk0 market " << endl;
cout << "************************" << endl;
cout << endl;
// ask the user how big their shopping budget is
cout << "What is your shopping budget today? $";
cin >> budget;
cin.ignore();
cout << endl;
// call functions that do the work
addItems(total, budget);
printTotal(total);
return 0;
}
void addItems(double total, double budget) {
double difference;
cout << endl;
cout << "****** ADD ITEMS AND PRICES BELOW ******" << endl;
cout << endl;
for (int row = 0; row < N_OF_ELEMS; row++) {
// get the name of the item and store it in the itemName array
cout << "Enter the name of item #" << newShoppingCart.lengthOfList << ": ";
cin.getline(newShoppingCart.itemName[row], 32, '\n');
// get the price of the item and store it in the itemPrice array
cout << "Enter the price of item #" << newShoppingCart.lengthOfList << ": ";
cin >> newShoppingCart.itemPrice[row];
cin.ignore();
cout << endl;
// increment length of item list
newShoppingCart.lengthOfList++;
// update total
total += newShoppingCart.itemPrice[row];
cout << "Your running total is: " << total << endl;
cout << endl;
// calculate difference
difference = budget - total;
// check to terminate with two cases
if (difference < 0) {
cout << "You are $" << difference << " over budget. Please return some items." << endl;
break;
} else if (difference == 0) {
cout << "Great budgeting skills you difference is: $" << difference << endl;
break;
}
}
}
void printTotal(double total) {
cout << endl;
cout << "****** RECEIPT ******";
cout << endl;
// print items from array
for (int row = 0; row < newShoppingCart.lengthOfList; row++) {
cout << newShoppingCart.itemName[row] << setw(12) << right << "$" << newShoppingCart.itemPrice[row] << endl;
total += newShoppingCart.itemPrice[row];
}
cout << endl;
cout << "Your total shopping bill is: $" << total << endl;
cout << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment