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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
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.