Do-While Loop. Help!

I have this assignment where I have to simulate a soda pop machine. I have to have the program loop through until one of the soda pops is out of stock. The thing is, my program does this except it will run until all are out of stock (goes into negatives) except for one...then stops.


Here is my loop code.

If anyone doesn't understand me, please ask to clarify in the comments.

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
  do 
	{
		int m = rand() % 5;
		double money = (double)(rand() % 20 + 80) / 100.00;
		double change = (money - drinks[m].drinkCost);

		cout << setw(12) << "Drink Name" 
			  << setw(15) << "Cost"
			  << setw(22) << "Amount left" << endl;
		cout << endl;

		for (int i = 0; i < SIZE; i++)
		{
			cout << setw(10) << drinks[i].drinkName 
				  << setw(14) << drinks[i].drinkCost 
				  << setw(18) << drinks[i].numberDrinks << endl;
		}

		
		cout << "The drink you chose was: " << drinks[m].drinkName << endl;
		cout << "The price is : $" << fixed << setprecision(2) << drinks[m].drinkCost << endl;
		cout << "You pay: $" << fixed << setprecision(2) << money << endl;
		cout << "Your change is : $" << fixed << setprecision(2) << change << endl;
		drinks[m].numberDrinks--;
		machineCash = machineCash + drinks[m].drinkCost;
		

	} while ((drinks[0].numberDrinks > 0) || (drinks[1].numberDrinks > 0) || (drinks[2].numberDrinks > 0) ||
		 (drinks[3].numberDrinks > 0) || (drinks[4].numberDrinks > 0));

	cout << endl;
	cout << fixed << setprecision(2) << "total cash: " <<  "$" << machineCash << endl;
Last edited on
Your problem is you are not invalidating the drinks that are empty. I would recommend either checking to see if the drinks are empty before drinks[m].numberDrinks--;

or use a better container for the drinks, e.g.:

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98


#include <iostream>
#include <cstdlib>
#include <list>
#include <iomanip>
using namespace std;

struct Drink
{
    Drink(string _name, int _num, double _cost)
    : drinkName(_name), numberDrinks(_num), drinkCost(_cost)
    { }

    string drinkName;
    int numberDrinks;
    double drinkCost;
};

typedef std::list<Drink*> Drink_t;
typedef std::list<Drink*>::iterator DrinkItr_t;
typedef std::list<Drink*>::const_iterator DrinkConstItr_t;

void print(const Drink_t& _drink_list)
{
    cout << setw(12) << "Drink Name"
    << setw(15) << "Cost"
    << setw(22) << "Amount left" << endl;
    cout << endl;

    for (DrinkConstItr_t itr = _drink_list.begin(); itr != _drink_list.end(); ++itr)
    {
        cout << setw(12) << (*itr)->drinkName
        << setw(15) << (*itr)->drinkCost
        << setw(22) << (*itr)->numberDrinks << endl;
    }
}

int main()
{
    // Create the list of drinks
    // Use pointers so later, all_drinks will have same instance of each drink
    // i.e. modify drink in "drinks" will also modify drink in
    // "all_drinks"
    Drink_t available_drinks;
    available_drinks.push_back(new Drink("Dr. Pepper", 10, 0.5));
    available_drinks.push_back(new Drink("Pepsi", 15, 0.5));
    available_drinks.push_back(new Drink("Surge", 8, 0.25));
    available_drinks.push_back(new Drink("Coca-Cola", 12, 0.5));
    // copy of drinks
    Drink_t all_drinks = available_drinks;

    double machineCash = 0.;

    // set the random seed
    srand(time(NULL));

    do
    {
        // just a separator to make output clear
        cout.fill('=');
        cout << setw(50) << "" << endl;
        cout.fill(' ');

        int m = rand() % available_drinks.size();
        DrinkItr_t drink = available_drinks.begin();
        // STL list doesn't have random access so must advance iterator
        std::advance(drink, m);
        double money = (double)(rand() % 20 + 80) / 100.00;
        double change = (money - (*drink)->drinkCost);

        // print list of all drinks
        cout << "\nList of all drinks:\n" << endl;
        print(all_drinks);
        // print list of available
        cout << "\nList of available drinks:\n" << endl;
        print(available_drinks);
        // print selection info
        cout << "\nThe drink you chose was: " << (*drink)->drinkName << endl;
        cout << "The price is : $" << fixed << setprecision(2) << (*drink)->drinkCost << endl;
        cout << "You pay: $" << fixed << setprecision(2) << money << endl;
        cout << "Your change is : $" << fixed << setprecision(2) << change << endl;
        // decrease number of drinks
        (*drink)->numberDrinks--;
        // if out of drinks remove from available drinks
        if((*drink)->numberDrinks == 0)
            available_drinks.erase(drink);
        // add machine cash
        machineCash += (*drink)->drinkCost;

        // since we are removing drinks if they are empty, we just do the do/while
        // loop until available_drinks no longer has any drinks in it
    } while (available_drinks.size() > 0);
    
    cout << endl;
    cout << fixed << setprecision(2) << "total cash: " <<  "$" << machineCash << endl;

}
output for above code (changing each original drink total to 2):

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
==================================================

List of all drinks:

  Drink Name           Cost           Amount left

  Dr. Pepper            0.5                     2
       Pepsi            0.5                     2
       Surge           0.25                     2
   Coca-Cola            0.5                     2

List of available drinks:

  Drink Name           Cost           Amount left

  Dr. Pepper            0.5                     2
       Pepsi            0.5                     2
       Surge           0.25                     2
   Coca-Cola            0.5                     2

The drink you chose was: Pepsi
The price is : $0.50
You pay: $0.92
Your change is : $0.42
==================================================

List of all drinks:

  Drink Name           Cost           Amount left

  Dr. Pepper           0.50                     2
       Pepsi           0.50                     1
       Surge           0.25                     2
   Coca-Cola           0.50                     2

List of available drinks:

  Drink Name           Cost           Amount left

  Dr. Pepper           0.50                     2
       Pepsi           0.50                     1
       Surge           0.25                     2
   Coca-Cola           0.50                     2

The drink you chose was: Coca-Cola
The price is : $0.50
You pay: $0.91
Your change is : $0.41
==================================================

List of all drinks:

  Drink Name           Cost           Amount left

  Dr. Pepper           0.50                     2
       Pepsi           0.50                     1
       Surge           0.25                     2
   Coca-Cola           0.50                     1

List of available drinks:

  Drink Name           Cost           Amount left

  Dr. Pepper           0.50                     2
       Pepsi           0.50                     1
       Surge           0.25                     2
   Coca-Cola           0.50                     1

The drink you chose was: Pepsi
The price is : $0.50
You pay: $0.91
Your change is : $0.41
==================================================

List of all drinks:

  Drink Name           Cost           Amount left

  Dr. Pepper           0.50                     2
       Pepsi           0.50                     0
       Surge           0.25                     2
   Coca-Cola           0.50                     1

List of available drinks:

  Drink Name           Cost           Amount left

  Dr. Pepper           0.50                     2
       Surge           0.25                     2
   Coca-Cola           0.50                     1

The drink you chose was: Coca-Cola
The price is : $0.50
You pay: $0.92
Your change is : $0.42
==================================================

List of all drinks:

  Drink Name           Cost           Amount left

  Dr. Pepper           0.50                     2
       Pepsi           0.50                     0
       Surge           0.25                     2
   Coca-Cola           0.50                     0

List of available drinks:

  Drink Name           Cost           Amount left

  Dr. Pepper           0.50                     2
       Surge           0.25                     2

The drink you chose was: Dr. Pepper
The price is : $0.50
You pay: $0.83
Your change is : $0.33
==================================================

List of all drinks:

  Drink Name           Cost           Amount left

  Dr. Pepper           0.50                     1
       Pepsi           0.50                     0
       Surge           0.25                     2
   Coca-Cola           0.50                     0

List of available drinks:

  Drink Name           Cost           Amount left

  Dr. Pepper           0.50                     1
       Surge           0.25                     2

The drink you chose was: Surge
The price is : $0.25
You pay: $0.98
Your change is : $0.73
==================================================

List of all drinks:

  Drink Name           Cost           Amount left

  Dr. Pepper           0.50                     1
       Pepsi           0.50                     0
       Surge           0.25                     1
   Coca-Cola           0.50                     0

List of available drinks:

  Drink Name           Cost           Amount left

  Dr. Pepper           0.50                     1
       Surge           0.25                     1

The drink you chose was: Dr. Pepper
The price is : $0.50
You pay: $0.84
Your change is : $0.34
==================================================

List of all drinks:

  Drink Name           Cost           Amount left

  Dr. Pepper           0.50                     0
       Pepsi           0.50                     0
       Surge           0.25                     1
   Coca-Cola           0.50                     0

List of available drinks:

  Drink Name           Cost           Amount left

       Surge           0.25                     1

The drink you chose was: Surge
The price is : $0.25
You pay: $0.86
Your change is : $0.61

total cash: $3.50
Topic archived. No new replies allowed.