BWMustang13 BWMustang13 - 24 days ago 6
C++ Question

How do I create an accumulator in C++ using a user defined function?

I am trying to keep a running total of cups of coffee sold and I have to use a user defined function to do it. I have tried numerous variations of the attached code but nothing seems to work. What am I doing wrong? Also I am a newb to C++ so that is why it looks amateurish!

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

const int SM_OZ = 8;
const int MD_OZ = 12;
const int LG_OZ = 16;

const double SM_PRICE = 1.19;
const double MD_PRICE = 1.49;
const double LG_PRICE = 1.89;
const double TAX = .0825;

void amtSold(int &smtCup, int &mdtCup, int &lgtCup);

int main()
{
int selection;
int smCup;
int mdCup;
int lgCup;

int smtCup;
int mdtCup;
int lgtCup;

smCup = 0;
mdCup = 0;
lgCup = 0;


do
{
cout << "COFFEE SHOP" << endl;
cout << "1. Sell Coffee" << endl;
cout << "2. Total Number of Cups Sold" << endl;
cout << "3. Total Amount of Coffee Sold" << endl;
cout << "4. Total Amount of Money made" << endl;
cout << "0. Exit" << endl;
cout << "Type a number to continue: ";
cin >> selection;
cout << endl;


//loop through the solutions based on the user's selection
switch (selection)
{
case 1:
cout << "How many small cups of coffee: ";
cin >> smCup;
cout << "How many medium cups of coffee: ";
cin >> mdCup;
cout << "How many large cups of coffee: ";
cin >> lgCup;

system("cls");

cout << fixed << setprecision(2) << endl;

//Sale Coffee Receipt Page
cout << "COFFEE SHOP" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "Price" << setw(18) << "Total" << endl;
cout << "Small: " << setw(18) << smCup << setw(18) << SM_PRICE << setw(18) << smCup*SM_PRICE << endl;
cout << "Medium: " << setw(17) << mdCup << setw(18) << MD_PRICE << setw(18) << mdCup*MD_PRICE << endl;
cout << "Large: " << setw(18) << lgCup << setw(18) << LG_PRICE << setw(18) << lgCup*LG_PRICE << endl;
cout << "Subtotal: " << setw(51) << (smCup*SM_PRICE)+(mdCup*MD_PRICE)+(lgCup*LG_PRICE) << endl;
cout << "Tax: (8.25%)" << setw(49) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX << endl;
cout << "Total: " << setw(54) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))+(((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX) << endl;
cout << endl;
cout << endl;

break;

case 2:
//Total Number of Cups Sold
cout << "REPORT - NUMBER OF COFFEE CUPS SOLD" << endl;

amtSold(smtCup, mdtCup, lgtCup);
cout << "SIZE" << setw(21) << "Number" << endl;
cout << "Small: " << setw(18) << smCup << endl;
cout << "Medium: " << setw(17) << mdCup << endl;
cout << "Large: " << setw(18) << lgCup << endl;
cout << endl;
cout << endl;

break;

case 3:
//Total Amount of Coffee Sold
cout << "REPORT - AMOUNT OF COFFEE SOLD" << endl;

cout << "SIZE" << setw(21) << "Number" << setw(18) << "OZ" << endl;
cout << "Small: " << setw(18) << smCup << setw(18) << smCup*SM_OZ << endl;
cout << "Medium: " << setw(17) << mdCup << setw(18) << mdCup*MD_OZ << endl;
cout << "Large: " << setw(18) << lgCup << setw(18) << lgCup*LG_OZ << endl;
cout << "Total: " << setw(36) << (smCup*SM_OZ) + (mdCup*MD_OZ) + (lgCup*LG_OZ) << endl;
cout << endl;
cout << endl;

break;

case 4:
//Total Amount of Money made
cout << "COFFEE SHOP - REPORT MONEY MADE" << endl;

cout << "SIZE" << setw(21) << "Number" << setw(18) << "Price" << setw(18) << "Total" << endl;
cout << "Small: " << setw(18) << smCup << setw(18) << SM_PRICE << setw(18) << smCup*SM_PRICE << endl;
cout << "Medium: " << setw(17) << mdCup << setw(18) << MD_PRICE << setw(18) << mdCup*MD_PRICE << endl;
cout << "Large: " << setw(18) << lgCup << setw(18) << LG_PRICE << setw(18) << lgCup*LG_PRICE << endl;
cout << "Subtotal: " << setw(51) << (smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE) << endl;
cout << "Tax: (8.25%)" << setw(49) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX << endl;
cout << "Total: " << setw(54) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE)) + (((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX) << endl;
cout << endl;
cout << endl;

break;

case 0:

system("cls");

break;

default:
//notify the user that an invalid selection has been inputted
cout << "You have made an invalid selection. Please choose a number from the list." << endl;
cout << endl;

}

} while (selection != 0);


system("pause");
return 0;

}

void amtSold(int &smtCup, int &mdtCup, int &lgtCup)
{
int smCup;
int mdCup;
int lgCup;

smCup = 0;
mdCup = 0;
lgCup = 0;

smtCup += smCup;
mdtCup += mdCup;
lgtCup += lgCup;

}

Answer

So as you probably know, you're not keeping track of the number of coffee cups of each size that you're selling (i.e. smtCup, mdtCup, and lgtCup).

I'm assuming that these variables mean the total number of cups for each size, you might want to put some comments during the variable declaration step. You'll want to initialise the variables to 0:

int smtCup = 0;
int mdtCup = 0;
int lgtCup = 0;

As this is a fairly simple program, you can perform accumulation without using your amtSold function, so you can delete that.

Then, in case 1 of your switch statement, you'll want to update smtCup, mdtCup, and lgtCup every time you update the values. Please be aware that smCup, mdCup, and lgCup are used only for input in this program.

cout << "How many small cups of coffee: ";
cin >> smCup;
cout << "How many medium cups of coffee: ";
cin >> mdCup;
cout << "How many large cups of coffee: ";
cin >> lgCup;

smtCup += smCup;
mdtCup += mdCup;
lgtCup += lgCup;

From here on out, you can print out the total number of small, medium and large cups by calling smtCup, mdtCup, and lgtCup in the other cases! Change smCup, mdCup, and lgCup to smtCup, mdtCup, and lgtCup in cases 2-4. Hope this helps!