I wrote this this for a simple vendding machine, and this code is to calculate the back change under 1 dollar. The only problem I am facing is: I want my function to stop excuting the rest of the function if the second if statement in the void function is true. So, How can I do it?
#include <iostream>
#include <string>
using namespace std;
void changecalculator (int purchaseAmount, int& Qav, int& Dav, int& Nav,int& totalPurchases, int& purchases)
{
int changeAvailable;
//set the variables for the change back function.
int QBack ,DBack ,NBack ;
//calculating the change that the machine should give back.
int chaneBack = 100 - purchaseAmount ;
//test if the purchase amount is a multiple of 5.
if (purchaseAmount %5 == 0)
{
//printing the purchase amount to the screen for the customer.
cout << "You entered a purchase amount of " << purchaseAmount << " cents." <<endl;
cout <<endl;
//calculating the denominations of the change.
QBack = std::min(chaneBack / 25, Qav) ; //Calculating quarters back.
chaneBack -= QBack * 25 ; // subtract the back quarters from the back change.
DBack = std::min(chaneBack/10, Dav); //Caculating the back dimes.
chaneBack -= DBack* 10; //Subtracting the back dimes from the back change.
NBack = std::min(chaneBack/ 5, Nav); //Calculating the back nickels.
chaneBack = QBack*25 + DBack*10 + NBack*5 + chaneBack; //Caculating the change back by adding the denominations.
changeAvailable = Qav * 25 + Dav * 10 + Nav * 5 ;
if (changeAvailable < chaneBack )
{
cout << "No Enough change for the purchase." <<endl;
cout << "Thanks for using my program" <<endl;
}
else
{
}
//Caculating the number of the coins that should be given to the customer as a change.
int coinsNum = QBack + DBack + NBack;
//printing information amount the back change given to the customer.
cout <<"Your change of " <<chaneBack <<" cents is given as " <<QBack <<" Q, " <<DBack <<" D,and " <<NBack <<" N." <<endl;
cout << "The value of your " <<coinsNum <<" coins adds up to " <<chaneBack <<" cents." <<endl;
cout << "Thank you for using my program." <<endl;
//Subtract the given denominations from the available denominations.
Qav -= QBack;
Dav -= DBack;
Nav -= NBack;
//Print visual information for the number of the remaining denominations.
//This part is only to show that the program can keep track of coins.
cout << "Quarters available: " <<Qav <<endl;
cout << "Dimes available: " <<Dav <<endl;
cout << "Nickels available: " <<Nav <<endl;
cout << "total purchases: " <<totalPurchases <<endl;
cout << "purchases: " <<purchases <<endl ;
/* set back the variable the original value so we can keep going
with function that would run after this step if the customer annswered yes. */
chaneBack -= chaneBack;
}
else
{
//print out an error if the purchase amount is not a multiple of 5.
cout << "Unable to process an invalid purchase amout of " <<purchaseAmount <<" cents." <<endl;
cout << "Thank you for using my program." <<endl;
}
}
int main()
{
//set the variables
int Qav=0 ; //Quarters available
int Dav=0 ; //Dimes available
int Nav=0 ; //Nickels available
int purchaseAmount ; //The purchase amount
int totalPurchases = 0; //The total of puchases the cusomer did.
int purchases = 0; //The number of purchases the cutomer did.
string answer; //The answer of the customer (y/n)
//The header of the program
cout << "Simple Vending Program for Adam Ashouri (Regular Version)" <<endl;
cout <<endl;
cout <<endl;
//Get the puchase amount from the customer.
cout << "Enter a purchase amount [5 - 100] -->";
cin >> purchaseAmount;
//Calculating the total of the purchases by adding them together.
totalPurchases += purchaseAmount;
//Calculating the number of purchases.
purchases += 1;
changecalculator (purchaseAmount, Qav, Dav, Nav, totalPurchases, purchases);
//asking the customer if they want to do another ourchase.
cout << "Process again (y/n)?";
cin >> answer;
//this loop helps rerun the function everytime the customer wants to.
while(answer == "y")
{
//Enter the new purchase amount
cout << "Enter a purchase amount [5 - 100] -->";
cin >> purchaseAmount;
//adding the second purchase amount to the last amount.
totalPurchases += purchaseAmount;
//adding this purchase to last number of purchases.
purchases += 1 ;
//run the function to caculate the change for the new purchase amount
changecalculator (purchaseAmount, Qav, Dav, Nav, totalPurchases, purchases);
//asks if the customer wants to do another ourchase again.
cout << "Process again (y/n)?";
cin >> answer;
}
}