can you put loops inside of loops?

Coding for a simulation of a convenience store for a school assignment. Need a case in the switch statement to repeat itself until the right amount is paid. Right now it repeats once, then goes back to the opening store choices. Don't know how to stop this.

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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*

PROGRAM NAME: LAB 3 - Convenience Store
PROGRAM DESCRIPTION: Simple simulation of a convenience store.
PROGRAMMER NAME: Chris Bottini
SOURCES USED: Boyd,
              Ascii Generator from http://www.network-science.de/ascii/,
              Absolute C++ 3rd adition
              cplusplus.com.
DATE CREATED: 2/22/2010
LAST DATE MODIFIED:3/5/2010
*/

#include <iostream>
#include "CinReader.h"
using namespace std;
CinReader reader;

int main()
{

int soda = 0;
    soda = 200;
int jerky = 0;
    jerky = 100;
int toiletPaper = 0;
    toiletPaper = 300;
int beer = 0;
    beer = 800;
int cigarettes = 0;
    cigarettes = 600;
int customerChoice = 0;
int moneyOwed = 0;
int customerMoney = 0;
    customerMoney = 3000;
int moneyPaid = 0;
bool checkOut = false;



    cout << "      ##      ## ######## ##        ######   #######  ##     ## ########" << endl;
    cout << "      ##  ##  ## ##       ##       ##   ##  ##     ## ###   ### ##      " << endl;
    cout << "      ##  ##  ## ##       ##       ##       ##     ## #### #### ##      " << endl;
    cout << "      ##  ##  ## ######   ##       ##       ##     ## ## ### ## ######  " << endl;
    cout << "      ##  ##  ## ##       ##       ##       ##     ## ##     ## ##      " << endl;
    cout << "      ##  ##  ## ##       ##       ##   ##  ##     ## ##     ## ##      " << endl;
    cout << "      ###  ###  ######## ########  ######   #######   ##     ## ########" << endl << endl;
    cout << "                     *********************************" << endl;
    cout << "                     ***TO CHRIS'S CONVENIENCE STORE***" << endl;
    cout << "                     *********************************" << endl << endl;

    cout << "                   $$$$ YOU HAVE " << customerMoney << " PENNIES TO SPEND $$$$" << endl << endl;
    cout << "                 ??? WHAT WOULD YOU LIKE TO PURCHASE TODAY ??? "<< endl << endl;



    do
    {

        cout << "              ITEMS:                               PRICE:" << endl << endl;
        cout << "               1. Soda.............................200 Pennies" << endl;
        cout << "               2. Jerky............................100 Pennies " << endl;
        cout << "               3. Toilet Paper.....................300 Pennies" << endl;
        cout << "               4. Beer.............................800 Pennies" << endl;
        cout << "               5. Cigarettes.......................600 Pennies" << endl;
        cout << "              [0] ................Checkout...................." << endl << endl;
        cout << "MAKE YOUR SELECTION" << endl << endl;
           customerChoice = reader.readInt();

if (customerChoice > 5)
    cout << endl << "You obviously do not know how to count. Pick items 1 through 5." << endl << endl;


switch (customerChoice)
        {
        case 1:

    if (moneyOwed >= customerMoney)
        {
        cout << "!!!YOU HAVE NO MORE MONEY, YOU WILL NOT BE ALLOWED!!!" << endl;
        cout <<      "!!!ANY MORE TRANSACTIONS, PLEASE CHECKOUT!!!" << endl;
        }
    else
         {
            moneyOwed = moneyOwed + soda;

            cout << endl << "You have choosen a soda." << endl << endl;
            cout << "You now owe me " << moneyOwed << " pennies" << endl;
            cout << "Would you like to buy anything else!!!" << endl << endl << endl << endl;

         }
                break;

        case 2:

    if (moneyOwed >= customerMoney)
        {
        cout << "!!!YOU HAVE NO MORE MONEY, YOU WILL NOT BE ALLOWED!!!" << endl;
        cout <<      "!!!ANY MORE TRANSACTIONS, PLEASE CHECKOUT!!!" << endl;

        }
    else
        {
            moneyOwed = moneyOwed + jerky;

            cout << endl << "You have bought some Jerky." << endl << endl;
            cout << "You now owe me " << moneyOwed << " pennies" << endl;
            cout << "Would you like to buy anything else!!!" << endl << endl << endl << endl;
        }
                break;

        case 3:

    if (moneyOwed >= customerMoney)
      {
        cout << "!!!YOU HAVE NO MORE MONEY, YOU WILL NOT BE ALLOWED!!!" << endl;
        cout <<     "!!!ANY MORE TRANSACTIONS, PLEASE CHECKOUT!!!" << endl;
      }
    else
      {
            moneyOwed = moneyOwed + toiletPaper;

            cout << endl << "You have bought Toilet Paper." << endl << endl;
            cout << "You now owe me " << moneyOwed << " pennies" << endl;
            cout << "Would you like to buy anything else!!!" << endl << endl << endl << endl;
      }
                break;

        case 4:

    if (moneyOwed >= customerMoney)
       {
        cout << "!!!YOU HAVE NO MORE MONEY, YOU WILL NOT BE ALLOWED!!!" << endl;
        cout <<     "!!!ANY MORE TRANSACTIONS, PLEASE CHECKOUT!!!" << endl;
       }
    else
        {
            moneyOwed = moneyOwed + beer;

            cout << endl << "You have bought Beer." << endl << endl;
            cout << "You now owe me " << moneyOwed << " pennies" << endl;
            cout << "Would you like to buy anything else!!!" << endl << endl << endl << endl;
        }
                break;


        case 5:

    if (moneyOwed >= customerMoney)
       {
        cout << "!!!YOU HAVE NO MORE MONEY, YOU WILL NOT BE ALLOWED!!!" << endl;
        cout <<     "!!!ANY MORE TRANSACTIONS, PLEASE CHECKOUT!!!" << endl;
       }
    else
        {
            moneyOwed = moneyOwed + cigarettes;

            cout << endl << "You have bought some Cigarettes." << endl << endl;
            cout << "You now owe me " << moneyOwed << " pennies" << endl;
            cout << "Would you like to buy anything else!!!" << endl << endl << endl << endl;
        }
                break;

        case 0:

        cout << "You owe me " << moneyOwed << " pennies" << endl;
        cout << "Enter penny amount to pay..." << endl;
            moneyPaid = reader.readInt();

   // moneyOwed = moneyOwed - moneyPaid;

    if (moneyOwed == moneyPaid)
        {
            checkOut = true;
        cout << "Thank you come again" << endl;
        }

    else if(moneyOwed > moneyPaid)
        {
            moneyOwed = moneyOwed - moneyPaid;
        cout << " THATS NOT ENOUGH PARTNER, DON'T MAKE ME GET MY SHOTTIE " << endl;
        cout << " YOU STILL OWE ME " << moneyOwed << " PENNIES" << endl << endl;
        cout << " Enter Amount to pay..." << endl;
            moneyPaid = reader.readInt();
        }

    else
        {
        cout << " THATS TOO MUCH, YOU ONLY OWE ME " << moneyOwed << " PENNIES" << endl;
        cout << "Enter Amount to pay..." << endl;
            moneyPaid = reader.readInt();
        }
                break;

    }

} while (checkOut == false);

        return 0;
}



I need the case below to repeat until the right amount is paid is it possible to put a loop inside of a 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
 case 0:

        cout << "You owe me " << moneyOwed << " pennies" << endl;
        cout << "Enter penny amount to pay..." << endl;
            moneyPaid = reader.readInt();

   // moneyOwed = moneyOwed - moneyPaid;

    if (moneyOwed == moneyPaid)
        {
            checkOut = true;
        cout << "Thank you come again" << endl;
        }

    else if(moneyOwed > moneyPaid)
        {
            moneyOwed = moneyOwed - moneyPaid;
        cout << " THATS NOT ENOUGH PARTNER, DON'T MAKE ME GET MY SHOTTIE " << endl;
        cout << " YOU STILL OWE ME " << moneyOwed << " PENNIES" << endl << endl;
        cout << " Enter Amount to pay..." << endl;
            moneyPaid = reader.readInt();
        }

    else
        {
        cout << " THATS TOO MUCH, YOU ONLY OWE ME " << moneyOwed << " PENNIES" << endl;
        cout << "Enter Amount to pay..." << endl;
            moneyPaid = reader.readInt();
        }
                break;

    }

} while (checkOut == false);

        return 0;
}

Of course you can put a loop inside a loop...Your indentation is...not that good though so I can't really tell what you are trying to add/fix.
Ok say I am checking out of the store, and I enter an amount higher than what is owed.

co
1
2
3
ut << " THATS TOO MUCH, YOU ONLY OWE ME " << moneyOwed << " PENNIES" << endl;
        cout << "Enter Amount to pay..." << endl;
            moneyPaid = reader.readInt();


above will print. Then I enter another amount and it immediately returns to the beginning part

1
2
3
4
5
6
7
8
9
cout << "              ITEMS:                               PRICE:" << endl << endl;
        cout << "               1. Soda.............................200 Pennies" << endl;
        cout << "               2. Jerky............................100 Pennies " << endl;
        cout << "               3. Toilet Paper.....................300 Pennies" << endl;
        cout << "               4. Beer.............................800 Pennies" << endl;
        cout << "               5. Cigarettes.......................600 Pennies" << endl;
        cout << "              [0] ................Checkout...................." << endl << endl;
        cout << "MAKE YOUR SELECTION" << endl << endl;
           customerChoice = reader.readInt();


Need the inputs to repeat until the right amount is payed.
That's because after you leave the if/else if chain there you hit the break, jumping you out of your switch/case, and then because of your do while loop you go back to the start. You would need to put in another loop around the part you want to pay.
Topic archived. No new replies allowed.