### Tracking the subtotal

I'm having trouble to code a function to keep track of a subtotal. My program is basically asking a user what are they buying, how much is it, & calculates the total. Then it asks them if they are purchasing something else. I want the total for the 2nd purchase to be added to the first one and so forth. I left the "subTotal" function empty because I tried so many things and it did not work.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````#include using namespace std; void getInput(struct Item*); double calcCost(int, double); void output(int, double, string, double); double subTotal(struct Item*); struct Item { string object; int quantity; double price; double total; }; int main() { struct Item order; char repeat = 'y'; while (repeat == 'y' || repeat == 'Y') { getInput(&order); cout << "Would you like to purchase something else? (y/n): "; cin >> repeat; } return 0; } void getInput(struct Item* input) { cout << "What kind of item are you purchasing?: "; cin >> input->object; cout << "What is the price of the item?: "; cin >> input->price; cout << "What is the quantity of the item?: "; cin >> input->quantity; input->total = calcCost(input->quantity, input->price); output(input->quantity, input->price, input->object, input->total); } double calcCost(int quantity, double price) { return quantity*price; } void output(int quantity, double price, string object, double total) { cout << "You have bought " << quantity << " units of " << object << " at \$" << price << " each for a total of \$" << total << "." << endl; } double subTotal(struct Item* box) { return 0; } ``````
Last edited on
You don't need a function for that, it's one line of code to add (well, it's two). Before enetring the loop declare a variable to hold the total `int total = 0;`. Then, after the purchase at the end of loop add this line:
`total += order.total; `
Last edited on
Topic archived. No new replies allowed.