Passing array to Boolean Function - c++

I am working on a project for class and I have pretty much completed all my code I just have one issue (THE CODE IS IN C++). I'm not too great at using boolean functions especially in the case for this program. If you guys could help me write or push me in the right direction for this code I'd appreciate it. This program is supposed to be a Soda Machine program made of structs. My question is how do I pass the array to the boolean function in order to check if there are any drinks left for the one that the customer wants to purchase. If there are no drinks left, the program will print out "Sorry we are sold out. Please make another selection." If there are still drinks available then just do nothing and continue with the program. It will pretty much validate the amount of drinks left. I attempted to write the function but I'm not sure if it is correct, I will post it for you guys to take a look at it. If you guys need any other info please let me know. Thanks for the help before guys.
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
struct Machine
{
string name;
double cost;
int drinks;
};
int displayMenu(int choice);
double inputValidation(double);
bool drinksRemaining(); // this is the function that will check if there are any drinks of the choice left
int displayMenu(int choice)
{
cout << "SOFT DRINK MACHINE\n";
cout << "------------------\n";
cout << "1) Cola ($0.65)\n";
cout << "2) Root Beer ($0.70)\n";
cout << "3) Lemon-Lime ($0.75)\n";
cout << "4) Grape Soda ($0.85)\n";
cout << "5) Water ($0.90)\n";
cout << "6) Quit Program\n";
cout << endl;
cout << "Please make a selection: ";
cin >> choice;
return choice;
}
double inputValidation(double amount)
{
while (amount < 0.00 || amount > 1.00)
{
cout << "Please enter an amount between $0.00 and $1.00.\n";
cout << "It should also be equal to or greater than the drink price : \n";
cin >> amount;
}
return amount;
}
bool drinksRemaining() // this is the function that I am having trouble with
{
if (drinks <= 0)
{
cout << "Sorry we are out of this drink. Please choose another one.";
cin >> drinkChoice;
return true;
}
else
{
return false;
}
}
int main()
{
const int DRINK_NUMS = 5;
int selection = 0;
double amountInserted = 0.00;
double changeReturned = 0.00;
double profit = 0.00;
Machine drink[DRINK_NUMS] = { { "Cola", .65, 20 }, { "Root Beer", .70, 20 }, { "Lemon-Lime", .75, 20 }, { "Grape Soda", .85, 20 }, { "Water", .90, 20 } };
do
{
profit += amountInserted - changeReturned;
selection = displayMenu(selection);
if (selection == 1)
{
cout << "Please enter the amount you want to insert:\n";
cin >> amountInserted;
inputValidation(amountInserted);
changeReturned = amountInserted - drink[0].cost;
cout << setprecision(2) << fixed << "CHANGE : $" << changeReturned << endl;
drink[0].drinks--;
}
else if (selection == 2)
{
cout << "Please enter the amount you want to insert:\n";
cin >> amountInserted;
inputValidation(amountInserted);
changeReturned = amountInserted - drink[1].cost;
cout << setprecision(2) << fixed << "CHANGE : $" << changeReturned << endl;
drink[1].drinks--;
}
else if (selection == 3)
{
cout << "Please enter the amount you want to insert.\n";
cin >> amountInserted;
changeReturned = amountInserted - drink[2].cost;
cout << setprecision(2) << fixed << "CHANGE : $" << changeReturned << endl;
drink[2].drinks--;
}
else if (selection == 4)
{
cout << "Please enter the amount you want to insert.\n";
cin >> amountInserted;
changeReturned = amountInserted - drink[3].cost;
cout << setprecision(2) << fixed << "CHANGE : $" << changeReturned << endl;
drink[3].drinks--;
}
else if (selection == 5)
{
cout << "Please enter the amount you want to insert.\n";
cin >> amountInserted;
changeReturned = amountInserted - drink[4].cost;
cout << setprecision(2) << fixed << "CHANGE : $" << changeReturned << endl;
drink[4].drinks--;
}
else if (selection == 6)
{
cout << endl;
cout << "SOFT DRINK MACHINE REPORT\n";
cout << "--------------------------\n";
cout << "Total amount earned: $" << profit << endl;
cout << endl;
}
else
{
cout << "Invalid selection. Please try again.";
displayMenu(selection);
}
}while (selection != 6);
system("PAUSE");
return 0;
}

The function needs an object or a reference to an object of type Machine.
bool drinksRemaining(Machine const& m);
and can be implemented very simply:
bool drinksRemaining(Machine const& m)
{
return (m.drinks > 0);
}
You can use it as:
if (selection == 1)
{
if ( drinksRemaining(drink[0]) )
{
cout << "Please enter the amount you want to insert:\n";
cin >> amountInserted;
inputValidation(amountInserted);
changeReturned = amountInserted - drink[0].cost;
cout << setprecision(2) << fixed << "CHANGE : $" << changeReturned << endl;
drink[0].drinks--;
}
else
{
cout << "Sorry we are out of this drink. Please choose another one.";
}
}

Related

how to add all in all the total in loop?

It is me again. I want to add the prices in the purchase if I want to buy more items. But I do not know how to do that. For example if, I confirm my purchase and want to buy more items, I want it to add the prices that is confirmed to purchase, and if I finally do not want to buy more items and not to look for more items, that total price would be computed.
int main()
{
int choice;
int purchase;
int quantity;
double totalChoice1;
double totalChoice2;
char view;
char confirm;
char buyMore;
char look;
double alloy, apex, kraken, aorus;
double oppo, alpha, rog, huawei;
double ps4, nintendo, xbox, wii;
alloy = 69.99;
apex = 199;
kraken = 90;
aorus = 60;
do {
cout << "What type of items would you like to view?" << endl;
cout << " [1] Peripherals" << endl;
cout << " [2] Mobile Phones" << endl;
cout << " [3] Consoles" << endl;
cout << " [4] Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
cout << "--------------------" << endl;
cout << "What peripherals would you like to purchase?" << endl;
cout << "[1] HyperX Alloy FPS PRO - $69.99" << endl;
cout << "[2] SteelSeries APEX PRO - $199" << endl;
cout << "[3] Razer Kraken X - $90" << endl;
cout << "[4] AORUS K7 - $60" << endl;
cout << "[5] BACK TO MENU" << endl;
cout << "Enter your choice: ";
cin >> purchase;
cout << "--------------------" << endl;
if (purchase == 1) {
cout << "How many would you like to purchase? ";
cin >> quantity;
totalChoice1 = quantity * alloy;
cout << "The total price for that is " << totalChoice1 << endl;
cout << "Confirm the Purchase? [Y]/[N]: ";
cin >> confirm;
if (confirm == 'Y') {
totalChoice1; // This is just a trial code.
cout << "Would you like to buy more items? [Y]/[N]: ";
cin >> buyMore;
}
else if (confirm == 'N') {
cout << "Do you still want to look for items? [Y]/[N]: ";
cin >> look;
if (look == 'N') {
break;
}
}
else {
break;
}
}
}
while (purchase == 5 || buyMore == 'Y' || look == 'Y');
cout << "The total price for your items is: " << totalChoice1; // This is also a trial code (totalChoice1)
}
You are simply missing a variable to keep track of that total. Don't forget to give it an initial value of 0!
There are plenty of minor other issues with your code, so you'll have some more learning ahead. For instance, we find it easier to do bookkeeping in cents, because you can treat them as integers.

Arrays Lose Value Upon Return (Inventory/Menu Program) C++

So for the most part I understand what I did wrong, the issue is I don't know how to fix it.
Goal: This is a store management system that must include a menu and inventory management functions that can be manipulated. To do this I used arrays to add the store's items, their descriptions, and their quantities. All the arrays are partially filled to the third element and have a max value of ten elements.
Issue: When the program is run the first time it works and the user can see their inventory, description and quantity. However when they exit to the menu and come BACK to inventory, everything past the third element is cleared. This is due to the declarations initializing the arrays past the third element to 0. How do I fix this to guarantee the user has their inventory saved and can view it upon return?
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;
//declarations
int mainMenu(); //done
void inventoryMgmt();
void addInv(); //working on
void invView(string x[], int sizeofArray, string y[], int secondArray, int z[], int thirdArray);
void editor(string x[], int sizeofArray, string y[], int secondArray, int z[], int thirdArray);
void customerReciept(); //done
void calculatePrice(); //done
void exit(); //done
const double massTax = 0.0625;
//main
int main() {
int choice;
bool repeat = true;
while (repeat = true) {
choice = mainMenu();
switch (choice) {
case 1:
customerReciept();
break;
case 2:
inventoryMgmt();
break;
//case 3:
//itemSearcher();
//break;
case 4:
exit();
repeat = false;
break;
}
}
return 0;
}
//main menu function ***done
int mainMenu() {
cout << "\n\n\t\t\t\t Welcome to the Massasoit Store Management System " << endl;
cout << "\t\t\t\t ________________________________________________ \n\n";
cout << "\t\t\t\t\t Main Menu: " << endl;
cout << "\t\t\t\t\t\t 1. Customer Reciept" << endl;
cout << "\t\t\t\t\t\t 2. Inventory Management" << endl;
cout << "\t\t\t\t\t\t 3. Item Search" << endl;
cout << "\t\t\t\t\t\t 4. Exit" << endl << endl;
int choice;
cout << "\t\t\t\t\t\t Where do you need to go?: ";
cin >> choice;
while (choice < 1 || choice > 4) {
cout << "\t\t\t\t\t Incorrect Selection Please Select Again: ";
cin >> choice;
}
return choice;
}
//customer reciept function **done
void customerReciept() {
cout << "\n\n\t\t\t\t Welcome to the Massasoit Store: Customer Reciept " << endl;
cout << "\t\t\t\t ________________________________________________ \n\n";
cout << "\t\t\t\t\t Receipt Menu: " << endl;
cout << "\t\t\t\t\t\t 1. Calculate Receipt" << endl;
cout << "\t\t\t\t\t\t 2. Return to Main" << endl;
int recieptChoice;
cout << "\t\t\t\t\t\t Where do you need to go?: ";
cin >> recieptChoice;
while (recieptChoice < 1 || recieptChoice > 2) {
cout << "Invalid Selection Please Choose Again: ";
cin >> recieptChoice;
}
if (recieptChoice == 1) {
calculatePrice();
}
}
void calculatePrice() {
double cost;
double taxAmount;
int numOfItems;
double finalCost = 0;
char tax;
int i;
cout << "\n\n\t\t\t\t Welcome to the Massasoit Store: Customer Reciept " << endl;
cout << "\t\t\t\t ________________________________________________ \n\n";
cout << "How many items were purchased?: ";
cin >> numOfItems;
for (i = 0; i < numOfItems; i++) {
cout << "What did item " << i + 1 << " cost? $";
cin >> cost;
cout << "Is this item taxable? (y/n):";
cin >> tax;
if (tax == 'y' || tax == 'Y') {
taxAmount = (cost * massTax);
cost = taxAmount + cost;
finalCost = cost + finalCost;
}
else {
finalCost = cost + finalCost;
}
}
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "This customer's total is $" << finalCost << endl;
}
void inventoryMgmt() {
int invChoice;
cout << "\n\n\t\t\t\t\t Welcome to Inventory Management " << endl;
cout << "\t\t\t\t ______________________________________________ \n\n";
cout << "\t\t\t\t\t Inventory Settings: " << endl;
cout << "\t\t\t\t\t\t 1. Add/View & Edit/Delete Inventory" << endl;
cout << "\t\t\t\t\t\t 2. Return to Main" << endl;
cout << "\t\t\t\t\t\t Where do you need to go?: ";
cin >> invChoice;
cout << endl << endl;
while (invChoice < 1 || invChoice > 2) {
cout << "Invalid Selection Please Choose Again: ";
cin >> invChoice;
}
if (invChoice == 1) {
addInv();
}
if (invChoice == 2) {
//edit/delete();
}
}
void addInv() {
//so when this is called, everything is initialized to 0 which makes it wipe the memory
//when the user comes back to it from the menu.
const int description = 20; //this allows a short description for each item
const int counter = 10; //slots of inventory allowed
int quantity[10] = {10, 15, 45};
string itemArray[counter] = { "Hot Drinks", "Cold Drinks", "Books"};
string descriptionArray[description] = { "Coffee, Tea etc", "Water, juice, etc", "Texts, notebook, etc"};
char addChoice;
int destination;
cout << "\t\t\t\t\t\t 1. Add/View" << endl;
cout << "\t\t\t\t\t\t 2. Edit/Delete" << endl;
cout << "\t\t\t\t\t\t 3. Exit to Main" << endl;
cout << "\t\t\t\t\t\t Where would you like to go?: " << endl;
cin >> destination;
while (destination < 1 || destination > 3) {
cout << "Invalid Selection, Please Select Again: ";
cin >> destination;
}
if (destination == 1) {
cout << "Would you like to add an item? (y/n): ";
cin >> addChoice;
int i = 3; //these two are initialized to three to ensure that the store
int ii = 3; //will always have hot drinks, cold drinks and books as options
while (addChoice == 'y' && i < counter) {
cout << "What would you like to add?: ";
cin >> itemArray[i];
cout << "You have added \"" << itemArray[i] << "\" to the inventory\n";
cout << "Please Provide a Brief Description: ";
cin.ignore();
getline(cin, descriptionArray[ii]);
cout << "You've described \"" << itemArray[i] << "\" as \"" << descriptionArray[i] << "\"" << endl;
cout << "What is the quantity of " << itemArray[i] << " in stock?";
cin >> quantity[i];
cout << "Would you like to add another item?";
cin >> addChoice;
i++;
ii++;
if (i > counter) {
cout << "**INVENTORY LIMIT REACHED*** ";
}
}
invView(itemArray, 10, descriptionArray, 10, quantity, 10); //working on this. //so use this for view, edit, and delete.
}
}
void invView(string x[], int sizeofArray, string y[], int secondArray, int z[], int thirdArray) { //add quantity
int i = 0;
int ii = 0;
int iii = 0;
cout << "Your inventory consists of: ";
while (i < sizeofArray && x[i] != "" && ii < secondArray && y[i] != "" && iii < thirdArray) {
cout << x[i] << " - " << y[i] << " | Quantity: " << z[i] << endl;
i++;
ii++;
}
}
void editor(string x[], int sizeofArray, string y[], int secondArray, int z[], int thirdArray) {
cout << "Which item would you like to edit?";
}
void exit() {
cout << "\n\n\t\t\t\t Welcome to the Massasoit Store Management System " << endl;
cout << "\t\t\t\t ________________________________________________ \n\n";
cout << "\t\t\t\t Thank you for using the Massasoit Store Management System" << endl;
exit(1);
}
I included the entire code in case it was needed. The issues are centered around the inventoryMgmt() and addInv() functions.
Thanks in advance for any help!
In theaddInv() all the arrays are local. Once that function is done executing, all local variables are freed up thus resulting in your deletion. Try place them outside your addInv() function:
int main(){
int quantity[10] = {10, 15, 45};
string itemArray[counter] = { "..."};
string descriptionArray[description] = { "..."};
void addInv() {...}
}

Why is failed input validation returning me to the start of my program?

Really sorry if this is a dumb question. I know it must have a super easy solution but I've been staring at this for so long I can't see it. It doesn't help that I'm really new at this either.
Long story short for some reason entering an invalid input past the first time returns me back to my menu, and sometimes also asks me to enter weight immediately after instead of allowing me to enter a menu choice. It's just all around broken and I don't know why. Thanks.
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
bool loopFlag = true;
bool loopFlagTwo = true;
int choice = 0;
int time = 0;
float weightPounds = 0;
float weight = 0;
const int BIKING = 8;
const int RUNNING = 10;
const int LIFTING = 3;
const float YOGA = 2.5;
int main()
{
cout << "Welcome to my Fitness Center" << endl;
do
{
cout << "\n\t____________________________________________________________" << endl;
cout << "\n\t\t\tMy Fitness Center" << endl;
cout << "\t\t\tActivity System" << endl;
cout << "\t____________________________________________________________" << endl;
cout << "\t\t\t Main Menu\n" << endl;
cout << "\t\t\t1) Stationary Bike" << endl;
cout << "\t\t\t2) Treadmill" << endl;
cout << "\t\t\t3) Weight Lifting" << endl;
cout << "\t\t\t4) Hatha Yoga" << endl;
cout << "\t\t\t5) End" << endl;
cout << "\t____________________________________________________________" << endl;
cout << "\n\nEnter the workout that you wish to track, or end to exit:" << endl;
do
{
cin >> choice;
if (cin.fail() || choice > 5 || choice < 1)
{
cout << "Invalid choice. Please choose from option 1 through 5." << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else if (choice == 5)
{
return 0;
}
else
{
loopFlag = false;
}
}
while (loopFlag);
do
{
cout << "\nPlease enter your weight in pounds: " << endl;
cin >> weightPounds;
if (cin.fail() || weightPounds <= 0)
{
cout << "Invalid weight entry!" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else
{
loopFlag = false;
}
}
while (loopFlag);
weight = weightPounds / 2.2;
cout << "\nYour weight is: \n" << fixed << setprecision(1) << weight << " kilograms." << endl;
if (choice == 1)
{
do
{
cout << "For how many minutes did you do this activity? " << endl;
cin >> time;
if (cin.fail() || time <= 0)
{
cout << "Invalid time entry!" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else
{
loopFlag = false;
}
}
while (loopFlag);
}
}
while (choice != 5);
return 0;
}
You need to set loopFlag to true before every do...while() you have, or use another flag, because after the first do...while(), loopFlag is always false.

How can I use arrays to ask my customers the quantity they want and print a receipt from the arrays?

I need to create arrays that save the quantity of the item the user selects and also prints out a receipt with the product, quantity and the total price. Please help me understand how to do this. I've got a basic understanding of what an array is. I just couldn't figure out how to save the users input.
#include <iostream>
#include <Windows.h>
#include <cstdlib>
#include <string>
#include "customerclass.h"
using namespace std;
//***** Functions to calculate the price of multiple items *****
void finalPrice1(int itemQuantity) {
float price;
price = itemQuantity * 3.00;
cout << "Your total is $" << price << endl;
cout << "Thank you for using my shop" << endl;
exit(0);
}
void finalPrice2(int itemQuantity) {
float price;
price = itemQuantity * 2.50;
cout << "Your total is $" << price << endl;
cout << "Thank you for using my shop" << endl;
exit(0);
}
void finalPrice3(int itemQuantity) {
float price;
price = itemQuantity * 1.25;
cout << "Your total is $" << price << endl;
cout << "Thank you for using my shop" << endl;
exit(0);
} //***** End of functions that calculate price of multiple items *****
int main(void)
{
char selection = ' ';
string lname = "";
string luserAddress;
int itemQuantity;
string orderFinalized;
CustomerInfo myCustomerInfo;
do
{ // Displaying menu
cout << "Hello, welcome to my online shop! What is your name? " << endl;
cin >> lname;
cout << " And what is your shipping address? " << endl;
cin >> luserAddress;
myCustomerInfo.setName(lname);
myCustomerInfo.setAddress(luserAddress);
cout << lname + ", nice to meet you. Here are the items in my shop followed by the price, please enter the number that corresponds to the item you want. \n " << endl;
cout << "Products \n";
cout << "1 - Chocolate candy bar - $3.00" << endl;
cout << "2 - Sour hard candy - $2.50" << endl;
cout << "3 - Mints - $1.25" << endl;
cout << "4 - Exit" << endl << endl;
cout << "Enter selection ";
// Reading User Selection
cin >> selection;
switch (selection)
{
case '1':
cout << "You've chosen a Chocolate candy bar. How many would you like? ";
cin >> itemQuantity;
cout << "Ok, will this finalize your order? Type and enter either 'Yes' or 'No' " << endl;
cin >> orderFinalized;
if (orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES") {
cout << myCustomerInfo.getName() + " your items will be shipped to " << myCustomerInfo.getAddress() << endl;
cout << "Printing your receipt now..." << endl;
finalPrice1(itemQuantity);
}
break;
case '2':
cout << "You've chosen Sour hard candy. How many would you like? ";
cin >> itemQuantity;
cout << "Ok, will this finalize your order? Type and enter either 'Yes' or 'No' " << endl;
cin >> orderFinalized;
if (orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES") {
cout << myCustomerInfo.getName() + " your items will be shipped to " << myCustomerInfo.getAddress() << endl;
cout << "Printing your receipt now..." << endl;
finalPrice2(itemQuantity);
}
break;
case '3':
cout << "You've chosen Mints. How many would you like? ";
cin >> itemQuantity;
cout << "Ok, will this finalize your order? Type and enter either 'Yes' or 'No' " << endl;
cin >> orderFinalized;
if (orderFinalized == "Yes" || "yes" || "YES") {
cout << myCustomerInfo.getName() + " your items will be shipped to " << myCustomerInfo.getAddress() << endl;
cout << "Printing your receipt now..." << endl;
finalPrice3(itemQuantity);
}
break;
case '4':
cout << "Thank you for using my shop. <exiting now...>" << endl;
break;
default: cout << "Invalid selection. Please try again";
}
cout << endl << endl;
} while (selection != '4');
return 0;
}
You need dynamic array. For example:
cin >> itemQuantity;
// create a array during runtime
// and the size is itemQuantity
// you can access the ith array element by items[i]
Item *items= new Item[itemQuantity];
Or you can use the vector,
vector<Item> items;//you can also access the ith element by items[i]
items.push_back(hard_candy);//items = {hard_candy}
items.push_back(soft_candy);//items = {hard_candy, soft_candy}
items.pop_back();//items = {hard_candy}
BTW, the case 3 in your code has some error:
orderFinalized == "Yes" || "yes" || "YES"//wrong
orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES"//right

C++ - How to call variables from functions without using global variables

I've been trying to complete a task but I am not allowed to use global variables.
The task if that you have to make a coffee shop program where user can buy coffee (small, medium or large), then show the total number of coffee cups sold and then then total sales made from selling coffee. I completed half of the code where user can buy but I am stuck at summing coffee sales/cups.
The function cupsSold and totalAmount return 0 or garbage value. Also I am not allowed to use extra library functions.
Is there any way to call variables from another function without using global variables?
This is my main code:
#include <iostream>
using namespace std;
void showChoices()
{
cout << "Welcome to Jason's Coffee Shop\n\n";
cout << "1. Buy Coffee" << endl;
cout << "2. Show total cups sold by size"<< endl;
cout << "3. Show total amount of coffee sold"<<endl;
cout << "4. Show total amount of money made"<< endl;
cout << "0. Exit" << endl;
cout << "\nYour choice: ";
}
void buyCoffee(int numberSmCups,float totalSmCups,int numberMedCups,float totalMedCups, int numberLgCups,float totalLgCups)
{
char coffeeSize, order = 'y';
bool coffeeSelect = true;
float smCoffee = 1.75, mdCoffee = 1.90, lgCoffee = 2.00, totalCoffee = 0;
while(coffeeSelect)
{
if (order == 'y' ||order =='Y'){
cout << "Size of Coffee [S, M, L]: ";
cin >> coffeeSize;
if (coffeeSize == 's' || coffeeSize == 'S')
{
cout << "Quantity of Small Coffee: ";
cin >> numberSmCups;
totalSmCups = numberSmCups * smCoffee;
cout << "Small Coffee: "<<numberSmCups<<", Bill: "<<totalSmCups<<endl;
totalCoffee += totalSmCups;
cout << "Add another coffee [Y or N]: "<<endl;
cin >> order;
}
else if (coffeeSize == 'm' || coffeeSize == 'M')
{
cout << "Quantity of Medium Coffee: ";
cin >> numberMedCups;
totalMedCups = numberMedCups * mdCoffee;
cout << "Medium Coffee: "<<numberMedCups<<", Bill: "<<totalMedCups<<endl;
totalCoffee += totalMedCups;
cout << "Add another coffee [Y or N]: "<<endl;
cin >> order;
}
else if (coffeeSize == 'l' || coffeeSize == 'L')
{
cout << "Quantity of Large Coffee: ";
cin >> numberLgCups;
totalLgCups = numberLgCups * lgCoffee;
cout << "Large Coffee: "<<numberLgCups<<", Bill: "<<totalLgCups<<endl;
totalCoffee += totalLgCups;
cout << "Add another coffee [Y or N]: ";
cin >> order;
}
}
else
break;
}
cout << "--------------\n";
cout << "Your invoice: "<<endl;
cout<< endl;
if (numberSmCups>= 1)
cout << "Small Coffee: "<< numberSmCups <<" cups ($ "<< smCoffee <<"/cup) Amount: $ "<< totalSmCups << endl;
if (numberMedCups>=1)
cout << "Medium Coffee: "<< numberMedCups <<" cups ($ "<< mdCoffee << "/cup) Amount: $ "<< totalMedCups << endl;
if (numberLgCups>=1)
cout << "Large Coffee: " << numberLgCups <<" cups ($ " << lgCoffee << "/cup) Amount: $ " << totalLgCups << endl;
cout<< endl;
cout << "Total Amount: "<<"$ "<< (totalSmCups+ totalMedCups+ totalLgCups) << endl;
cout << "--------------" << endl;
}
void cupsSold(int numberSmCups,int numberMedCups,int numberLgCups){
int lifeSmCups=0;
int lifeMdCups=0;
int lifeLgCups=0;
lifeSmCups = lifeSmCups + numberSmCups;
lifeMdCups = lifeMdCups + numberMedCups;
lifeLgCups = lifeLgCups + numberLgCups;
cout << "Total Small cups: "<<lifeSmCups<<endl;
cout << "Total Medium Cups:"<<lifeMdCups<<endl;
cout << "Total Large Cups:"<<lifeLgCups<<endl;
}
void totalAmount(float totalCoffee)
{
cout << "Total: "<<"$"<<totalCoffee<<endl;
}
int main()
{
int numberSmCups = 0, numberMedCups = 0, numberLgCups = 0, choice;
float totalSmCups = 0, totalMedCups = 0, totalLgCups = 0, totalCoffee;
while (choice != 0)
{
showChoices();
cin >> choice;
cout << endl;
if (choice == 1)
{
cout <<"You selected to buy coffee."<< endl;
buyCoffee(numberSmCups,totalSmCups,numberMedCups,totalMedCups,numberLgCups,totalLgCups);
}
else if (choice == 2)
{
cout << "The total amount of cups sold by size is: "<<endl;
cupsSold(numberSmCups,numberMedCups,numberLgCups);
}
else if (choice == 3)
{
cout << "Total Coffee Sold: "<<endl;
// cupsSold(numberSmCups,numberMedCups,numberLgCups);
}
else if (choice == 4)
{
cout << "Money made from coffee sales: "<<endl;
totalAmount(totalCoffee);
}
else if (choice == 0)
{
cout << "EXIT"<<endl;
break;
}
else
cout << "Invalid Input" << endl;
}
return 0;
}
"Is there any way to call variables from another function without using global variables?"
You need to use by reference parameters, if you want to change the variables passed to a function like this
void cupsSold(int& numberSmCups,int& numberMedCups,int& numberLgCups){
// ^ ^ ^
otherwise these functions just operate on copies rather than your original variable values.