break in switch statement terminates program

Hi guys I am writing a restaurant ordering system,the code is pretty sloppy and I will need to break it into more functions(tidy it up) anyway I have come across a problem for some reason when my list of items are empty when I type 7 when the program starts up it will print nothing as expected but seems to return even though I use a break statement

it should only return if 9 is entered

here is the function that is giving the problems

note I can post full code if needed

thanks

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
  bool order(vector<food> &items)
{
    printMenu();
    int choice;
    cin >> choice;
    switch(choice)
    {
    case 1:
    {
        int quantity = 0;
        int wantsFries = 0;
        int friesQuantity = 0;

        cout << "how many would you like?" << endl;
        cin >> quantity;
        for(int i = 0; i < quantity; i++)
        {
            Burger burger;
            items.push_back(burger);
        }
        cout << "would you like fries with that press 1 for yes" << endl;
        cin >> wantsFries;

        if(wantsFries == 1)
        {
            cout << "how many portions would you like that?" << endl;
            cin >> friesQuantity;

            for(int i = 0; i < friesQuantity; i++)
            {
                fries freshFries;
                items.push_back(freshFries);
            }
        }
        orderDrink(items);
    }
    break;

    case 2:
    {
        int quantity = 0;
        int wantsFries = 0;
        int friesQuantity = 0;
        cout << "how many would you like?" << endl;
        cin >> quantity;
        for(int i = 0; i < quantity; i++)
        {
            chesseBurger burger;
            items.push_back(burger);
        }
        cout << "would you like fries with that press 1 for yes" << endl;
        cin >> wantsFries;

        if(wantsFries == 1)
        {
            cout << "how many portions would you like with that?" << endl;
            cin >> friesQuantity;

            for(int i = 0; i < friesQuantity; i++)
            {
                fries freshFries;
                items.push_back(freshFries);
            }
        }
        orderDrink(items);
    }
    break;
    case 3:
    {
        int quantity = 0;
        int wantsFries = 0;
        int friesQuantity = 0;
        cout << "how many would you like?" << endl;
        cin >> quantity;
        for(int i = 0; i < quantity; i++)
        {
            mexicanBurger burger;
            items.push_back(burger);
        }
        cout << "would you like fries with that press 1 for yes" << endl;
        cin >> wantsFries;

        if(wantsFries == 1)
        {
            cout << "how many portions would you like?" << endl;
            cin >> friesQuantity;

            for(int i = 0; i < friesQuantity; i++)
            {
                fries freshFries;
                items.push_back(freshFries);
            }
        }
        orderDrink(items);
    }
    break;
    case 7:{
        for(int i = 0; i < items.size(); i++){

            cout << "item : " << items.at(i).getName() <<  " price : " << items.at(i).price << endl;
        }
    }
        break;
    case 8:
    {
        double totalPrice = 0;

        for(int i = 0; i < items.size(); i++)
        {
            totalPrice += items[i].price;
        }

        cout << "total price $" << totalPrice << endl;
    }
    break;
    case 9:
    {
        return false;;
    }
    }
}



Last edited on
it should only return if 9 is entered
What is it supposed to do in the other cases? If you want to print the menu again you need a loop.

It doesn't matter what case, die function will end with an invalid result. The compiler should have at least warned you due to the missing return at the end.
very good point coder777 I added return true after the switch so if the switch breaks then it will return true if gets into the test case 9 only then will it return false

also the loop is written in main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15


int main()
{
    int total = 0;
    vector<food> items;

    while(true)
    {
        if(!order(items))
        {
            break;
        }
    }
}
Last edited on
Topic archived. No new replies allowed.