Structs

This code intends to output a selection of the snack a user chooses along with the price and change owed by a vending machine. I need it to subtract one from the inventory of the item selected and I intend to do that (line 54) but nothing happens. If anyone has any clue as to what to do to fix this then that would be great.


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
#include <string>
#include <iostream>

using namespace std;

struct Snack
{
    string name;
    double cost;
    int inventory;
};

int main()
{
    string snack;
    struct Snack vendor[4] = {
        {"Oreo Cookie Package", 0.75, 25},
        {"Granola Bars", 0.80, 30},
        {"Oatmeal Raisin Cookies", 0.75, 15},
        {"Tortilla Chips", 0.60, 20},
    };
    bool run = true;
    double profit = 0.00;
    while (run)
    {
        for (int i = 0; i < 4; i++)
        {
            if (vendor[i].inventory > 0)
            {
                cout << i+1 << ".  " << vendor[i].name << endl;
            }
        }
        int selection;
        cout << "Enter the number of the snack you would like to purchase.  If you are done, enter 0 to quit: " << endl;
        cin >> selection;
        if (selection == 0)
            run = false;
        double payment;
        cout << "Your snack costs $" << vendor[selection - 1].cost << ".  Please enter at least as much as your snack costs, but no more than $5.00: " << endl;
        cin >> payment;
        while (payment<vendor[selection - 1].cost || payment>5.00)
        {
            cout << "You have not entered a valid amount.  Please try again: " << endl;
            cin >> payment;
        }
        profit += vendor[selection - 1].cost;
        cout << "Your change is: " << payment - vendor[selection - 1].cost << endl;
        cout << "" << endl;
        vendor[selection - 1].inventory --;
    }
    cout << "Your total money made is: $" << profit << endl;
    for (int i = 0; i < 4; i++)
    {
        cout << "There are " << vendor[i].inventory << " " << vendor[i].name << "'s remaining";
    }
    return 0;
}
Last edited on
This is what it outputs but I would like it to say something like (Inventory remaining: "number remaining here"

1
2
3
4
5
6
7
8
9
1.  Oreo Cookie Package
2.  Granola Bars
3.  Oatmeal Raisin Cookies
4.  Tortilla Chips
Enter the number of the snack you would like to purchase.  If you are done, enter 0 to quit: 
1
Your snack costs $0.75.  Please enter at least as much as your snack costs, but no more than $5.00: 
5.00
Your change is: 4.25
Last edited on
I think I see your problem,

the problem is you are not breaking out of the loop if the user enters 0. So if the user enters 0 run now equals false but more code comes after this so instead break out of the loop if the user enters 0.

*edit if you want it to display "Inventory remaining number remaining" just put your for loop inside your main while loop

1
2
3
4
5
6
7
8

 for (int i = 0; i < 4; i++)
    {
        cout << "There are " << vendor[i].inventory << " " << vendor[i].name << "'s remaining"; 
    }

// place this inside main while loop


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
#include <string>
#include <iostream>

using namespace std;

struct Snack
{
    string name;
    double cost;
    int inventory;
};

int main()
{
    string snack;
    struct Snack vendor[4] = {
        {"Oreo Cookie Package", 0.75, 25},
        {"Granola Bars", 0.80, 30},
        {"Oatmeal Raisin Cookies", 0.75, 15},
        {"Tortilla Chips", 0.60, 20},
    };
    double profit = 0.00;
    while (true) // change here
    {
        for (int i = 0; i < 4; i++)
        {
            if (vendor[i].inventory > 0)
            {
                cout << i+1 << ".  " << vendor[i].name << endl;
            }
        }
        int selection;
        cout << "Enter the number of the snack you would like to purchase.  If you are done, enter 0 to quit: " << endl;
        cin >> selection;
        if (selection == 0)
            break; // change here
        double payment;
        cout << "Your snack costs $" << vendor[selection - 1].cost << ".  Please enter at least as much as your snack costs, but no more than $5.00: " << endl;
        cin >> payment;
        while (payment<vendor[selection - 1].cost || payment>5.00)
        {
            cout << "You have not entered a valid amount.  Please try again: " << endl;
            cin >> payment;
        }
        profit += vendor[selection - 1].cost;
        cout << "Your change is: " << payment - vendor[selection - 1].cost << endl;
        cout << "" << endl;
        vendor[selection - 1].inventory --;
    }
    cout << "Your total money made is: $" << profit << endl;
    for (int i = 0; i < 4; i++)
    {
        cout << "There are " << vendor[i].inventory << " " << vendor[i].name << "'s remaining";
    }
    return 0;
}

Last edited on
Topic archived. No new replies allowed.