如何让我的函数在某一行停止执行?

时间:2014-09-17 21:44:10

标签: c++

我为一个简单的售卖机器写了这个,这个代码是计算1美元以下的后退变化。 我面临的唯一问题是:如果void函数中的第二个if语句为true,我希望我的函数停止激活函数的其余部分。 那么,我该怎么做呢?

#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;
    }
}

3 个答案:

答案 0 :(得分:3)

在代码中添加return语句。 return

  

终止函数的执行并将控制权返回给调用函数(如果从主函数转移控制,则返回操作系统)。执行将在呼叫后紧接呼叫功能中重新开始。

来源:MSDN

应用于您的代码:

...
    if (changeAvailable < chaneBack )
    {
        cout << "No Enough change for the purchase." <<endl;
        cout << "Thanks for using my program" <<endl;
        return;
    }
...

答案 1 :(得分:3)

您可以通过以下两种方式之一完成此操作:

  1. 在此else块中没有任何内容,在此块中放置其他所有内容(在执行if块时不希望运行的内容)。< / p>

  2. return;放在if块的末尾。这将导致您的函数退出而不是执行任何进一步的代码。

答案 2 :(得分:3)

您需要致电return

    if (changeAvailable < chaneBack )
    {
        cout << "No Enough change for the purchase." <<endl;
        cout << "Thanks for using my program" <<endl;
        return;
    }