Home > Enterprise >  How do I stop running total from resetting to 0 in void function?
How do I stop running total from resetting to 0 in void function?

Time:11-05

I have an assignment where I'm supposed to make a restaurant menu. The menu must loop until the user enters 4 to get the bill. I'm supposed to use a function to display the menu, and another function to calculate the running total AND display the bill when the user finishes ordering.

Problem is, I have to initialize runningTotal before I use it, and I don't know where to put it to stop it resetting to its initialization. Originally I had my calculating function, cost(), set up like this:


void cost(const double ITEM_PRICE, int amtOfItemOrdered, bool isDoneOrdering) {

// initializing runningTotal and declaring other variables...
    double runningTotal = 0, tip, finalBill;

// if statement so bill only displays when order is finished...
    if (isDoneOrdering == false)

        // calculate running total...
        runningTotal  = (ITEM_PRICE * amtOfItemOrdered);

    else {

        // when order finishes, calculate and display bill...
        tip = (runningTotal * 0.20);
        finalBill = (runningTotal   tip);

        cout << "\nFood total: " << runningTotal <<
                "\nTip: " << tip <<
                "\nFinal Bill: " << finalBill;
    }

Which I think results in runningTotal resetting to 0 every time the function executes. When I run the program, order something, and press 4 to see the bill, everything in the bill comes up as 0. Here's an example of the output:

Food total: 0
Tip: 0
Final Bill: 0

I thought it might help to move runningTotal outside the cost() function like this:


int main() {

// initializing runningTotal with 0 in main...
        double runningTotal = 0;

// other variables...
    const double ITEM1_PRICE = 2.50, ITEM2_PRICE = 3.50, ITEM3_PRICE = 4.50;
    int itemOrdered, amtOfItemOrdered;
    bool isDoneOrdering = false;

// loop menu choice until order is finished...
    do {
        displayMenu(ITEM1_PRICE, ITEM2_PRICE, ITEM3_PRICE);
        cout << "Enter your choice: ";
        cin >> itemOrdered;

        // don't ask "how many" when done ordering...
        if (itemOrdered != 4) {
            cout << "\nHow many? ";
            cin >> amtOfItemOrdered;
        }

        switch (itemOrdered) {
        case 1:

        // passing runningTotal to cost() so cost() can use it...
            cost(ITEM1_PRICE, amtOfItemOrdered, isDoneOrdering, runningTotal);
            break;

        case 2:
            cost(ITEM2_PRICE, amtOfItemOrdered, isDoneOrdering, runningTotal);
            break;
        case 3:
            cost(ITEM3_PRICE, amtOfItemOrdered, isDoneOrdering, runningTotal);
            break;
        case 4:
            isDoneOrdering = true;
            cost(0, 0, isDoneOrdering, runningTotal);

            // zeroes are here because cost() asks for parameters,
            // but we're done adding to running total...

            break;
        default:
            0; // empty placeholder statement...
        }
    } while (isDoneOrdering == false);
}

But this has the same result. I think what's happening here is that runningTotal in main is different from runningTotal in cost(), and the value 0 in main's runningTotal is getting passed to the runningTotal in cost(), which is resetting it anyway?

I thought about making a return statement for runningTotal, tip, and finalBill, and displaying the bill outside of the function, but I think the assignment wants me to stick to void functions.

Just tested initializing runningTotal to 1 in main, and the bill displays the following:

Food total: 1
Tip: 0.2
Final Bill: 1.2

So I do think initialization is the problem here. Any help would be appreciated. Thanks.

CodePudding user response:

Since this is C , it makes sense to use classes.

Consider a class to encapsulate the cost calculation, and a separate class for storing the menu.

constexpr double TIP_MARGIN = 0.2;

class BillCalculator
{
    double m_RunningTotal = 0.0;
public:
    void AddCost(const double price, int count)
    {
        m_RunningTotal  = price * count;
    }
   
    double CalculateTip() const
    {
        return m_RunningTotal * TIP_MARGIN;
    }

    double CalculateTotalBill() const
    {
        return GetRunningTotal()   CalculateTip();
    }

    double GetRunningTotal() const
    {
        return m_RunningTotal;
    }
};

Instantiate both classes in main. Having one function that focuses on I/O only will make the code easier to follow.

If you need to pass an instance of either of the classes that you created, pass it by reference.

CodePudding user response:

Variables defined and initialized in the scope of function will stay in the function. In your code, runningTotal in the main() function and runningTotal in the cost() function and thus have different memory space in the memory (although they have same names).

I've noticed you want to access the value of the variable runningTotal in the cost() function and also to modify the variable. Like Peter's commented, you can do that by 2 ways:

  1. Passing runningTotal into the function cost() by parameter or pointer, that is you give the function the variable by identifying the memory space of that variable.

For example:

By parameter:

void cost(your variables..., double& runningTotal) //by parameter.

//calling the function:
cost(your variables..., runningTotal); //it's the same as calling normal function

//using the variable
//you can use its the same as normal.
//note that, any changes made in this function will affect the variable outside, so be carefull

By pointer:

void cost(your varialbe..., double* runningTotal) //by pointer.

//calling the funtion
cost(your variables...,&runningTotal);

//using the variable
//As pointer is simply the address of the variable itself
//you'll have to dereference it before using by adding a * before the variable
*runningTotal = *runningTotal  1;
//note that, changes here also affect the variable outside.
  1. By using a variable of a file scope (global scope):

    //the variable
    
    double runningTotal;
    
    void cost(your variable....){//your code}
    
    int main(){ //your code}
    

Note that, change in this way still affect the variable outside

  • Related