0

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;
    }
}
Adam Rich
  • 86
  • 2
  • 10
  • 1
    Are you looking for the [`return` statement](http://en.cppreference.com/w/cpp/language/return)? – Jerry Coffin Sep 17 '14 at 21:46
  • Even though you have declared a void function, you can call RETURN; statement to exit the function. It does not return anything, it just quit executing the rest of the function. – Juniar Sep 17 '14 at 21:54
  • I suggest you pick up a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) on C++. – Captain Obvlious Sep 17 '14 at 21:56

3 Answers3

3

Add a return statement to your code. return:

Terminates the execution of a function and returns control to the calling function (or to the operating system if you transfer control from the main function). Execution resumes in the calling function at the point immediately following the call.

Source: MSDN

Applied to your code:

...
    if (changeAvailable < chaneBack )
    {
        cout << "No Enough change for the purchase." <<endl;
        cout << "Thanks for using my program" <<endl;
        return;
    }
...
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
3

You can do so in one of two ways:

  1. Instead of having nothing in your else block, put everything else (that you don't want to run in the event that your if block is executed) in this block.

  2. Put a return; at the end of your if block. This will cause your function to quit instead of executing any further code.

wolfPack88
  • 4,163
  • 4
  • 32
  • 47
3

You will want to call return.

    if (changeAvailable < chaneBack )
    {
        cout << "No Enough change for the purchase." <<endl;
        cout << "Thanks for using my program" <<endl;
        return;
    }
Richard Shurtz
  • 1,641
  • 1
  • 15
  • 11