Little Loop Help

I'm writing a program with an outer loop and an inner loop. When you select what size pizza you want, you'll be taken to an inner loop to pick toppings. I'm stuck trying to figure out how to use a loop that incorporates my first choice, and then adds whatever toppings I choose after each selection. Can I use a method as my inner loop that accepts my pizza size choice as its parameter?

Thanks in advance, and here's what I have right now:

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
#include <iostream>
using namespace std;

class pizza
{
public:
   int size;
};

int main()
{
   pizza value;
   char response;

   do
   {
      cout << "What size pizza? \n\n";
      cout << " 1 for small \n 2 for medium \n 3 for large\n Q to quit\n";
      cin >> response;

      if (response == 1 || response == 2 || response == 3)
      {
         value.size = response;
         if (response = 1)
            //inner menu loop - small pizza
         else if (response = 2)
            //inner menu loop - medium pizza
         else if (response = 3)
            //inner menu loop - large pizza
      }
      else if (response == 'q' || response == 'Q')
         break;
   } 
   while (not sure what my while will be yet...)
}
Last edited on
Line 24-26 and 28, change the '=' for an ==operator , as you are not 'assigning' a value, but 'comparing' it to another :

1
2
3
4
5
if (response == 1)
            //inner menu loop - small pizza
         else if (response == 2)
            //inner menu loop - medium pizza
         else if (response == 3)


This might not fix it all, but it sure what a problem I immediatly saw.
Last edited on
Yes. You would just do it as so
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
#include <iostream>
using namespace std;

class pizza
{
public:
   int size;
};

int main()
{
   pizza value;
   char response;

   do
   {
      cout << "What size pizza? \n\n";
      cout << " 1 for small \n 2 for medium \n 3 for large\n Q to quit\n";
      cin >> response;

      if (response == 1 || response == 2 || response == 3)
      {
         value.size = response;
         innerLoop(value.size);  
      }
      else if (response == 'q' || response == 'Q')
         break;
      else
          //I would suggest handling errors
   } 
   while (not sure what my while will be yet...)
}

So this would assume the inner loop for each size of pizza has the same logic. If you need any help from here, let me know
Last edited on
However I would suggest using more of an object oriented approach. It is good program practice. Something like 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
#include <iostream>
using namespace std;

class pizza
{
private:
   int size;
   vector<string> toppingList;  //or you could use an array, if you know the max toppings...
                                          //or a good assumption for max toppings.
public:
   pizza()  //default contructor
   {
    this.size = 0;
   } 

   void addToppings()
   {
        string topping;
        int numToppings;
        cout << "How Many toppings would you like?\n";
        cin >> numToppings;

        for (i = 0; i < numToppings; i++)
        {
             cout << "Enter topping number " << i << ":\n";
             cin >> topping;
             this.toppingList.push_back(topping);
        }
   }
};

int main()
{
   string response;
   vector<pizza> order;
   pizza currentPizza;
   
   do
   {
      cout << "What size pizza? \n\n";
      cout << " 1 for small \n 2 for medium \n 3 for large\n Q to quit\n";
      cin >> response;

      if (response == 1 || response == 2 || response == 3)
      {
         currentPizza.size = response;
         currentPizza.addToppings();  
         order.push_back(currentPizza);  //put current pizza onto orderList
      }
      else if (response == 'Q' || response == 'Q') 
      {
         break;
      }
      else
      {
          //I would suggest handling errors, i.e user enters something other than 1,2,3, q, Q
      }
   } 
   while (response != 'q' || response != 'Q')
}

Thanks so much for the advice, Nick Schuck! I'm taking it all in and trying to determine how to best incorporate what you've shared.

I do have a question though: I will be using an array instead of a vector, but it's my first program using an array, so I'm not sure how to do it. How would one take input from the user and grab the corresponding object from the array?

Ps. I'm working on the errors, too. I was trying to keep the code short.
Last edited on
Topic archived. No new replies allowed.