Can't break out of loop when user tells the program to exit?

I am writing a menu based program where the user chooses from the menu what to do with an array. The last option is to exit the program, but when I run the program, it still tells the user to initialize the array when it should be displaying that the program was exited. This is my code for main:

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
  while (choice != -999){

        if(size == 0)
        {
            choice = 1;
            cout << "First initialize the array" << endl;
        }

        switch (choice) {
            case 1:
                size = initializeArray(arr);
                cout << "Size of Array: " << size << endl;
                cout << endl;
                break;
            case 2:
                displayArray(arr, size);
                break;
            case 3:
                calculate(arr, min, max, sum, average, size);
                cout << "Min: " << min << endl;
                cout << "Max: " << max << endl;
                cout << "Sum: " << sum << endl;
                cout << "Average: " << average << endl;
                break;
            case 4:
                cout << "Enter a value to count: ";
                cin >> value;
                count = countElement(arr, size, value);
                cout << value << " appears " << count << " times." << endl;
                break;
            case 5:
                cout << "Enter a position to remove from the array: ";
                cin >> newPos;
                newPos++;
                if (newPos < 0 || newPos > size)
                {
                    cout << "Invalid Index " << endl;
                }
                else
                {
                    size = removeValue(arr, newPos, size);
                    displayArray(arr, size);
                }
                break;
            case 6:
                cout << "Enter a value to insert: ";
                cin >> insert;
                cout << "Enter a position to insert the value in: ";
                cin >> pos;
                pos--;
                if (pos < 0 || pos > size)
                {
                    cout << "Invalid Index " << endl;
                }
                else
                {
                    size = insertValue(arr, insert, pos, size);
                    displayArray(arr, size);
                }
            case 7:
                choice = -999;
                break;
        }
        if (choice == -999)
            return 0;
        else
            choice = displayMenu();


Why doesn't the program exit when the user enters 7??
would you mind giving us all of your code that way we are able to understand your goal better and we could also test run it. Thxs ;)
Sorry about that, here's my full code:
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
201
202
203
204
205
206
207
208
209
210
#include <iostream>
#include <iomanip>
using namespace std;

int displayMenu();
int initializeArray(int arr[]);
void displayArray(int arr[], int size);
void calculate(int arr[],int& min, int& max, int& sum, double& average, int size);
int countElement(int arr[], int size, int value);
int removeValue(int arr[], int newPos, int size);
int insertValue(int arr[], int value, int pos, int size);


const int SIZE = 10;


int main()
{
    int choice = 0;
    int arr[SIZE];
    int size = 0;
    int min = 0;
    int max = 0;
    int sum = 0;
    double average = 0;
    int count = 0;
    int value = 0;
    int insert = 0;
    int pos = 0;
    int remove = 0;
    int newPos = 0;

    choice = displayMenu();
    cout << "Choice = " << choice << endl;
    cout << endl;
    while (choice != -999){

        if(size == 0)
        {
            choice = 1;
            cout << "First initialize the array" << endl;
        }

        switch (choice) {
            case 1:
                size = initializeArray(arr);
                cout << "Size of Array: " << size << endl;
                cout << endl;
                break;
            case 2:
                displayArray(arr, size);
                break;
            case 3:
                calculate(arr, min, max, sum, average, size);
                cout << "Min: " << min << endl;
                cout << "Max: " << max << endl;
                cout << "Sum: " << sum << endl;
                cout << "Average: " << average << endl;
                break;
            case 4:
                cout << "Enter a value to count: ";
                cin >> value;
                count = countElement(arr, size, value);
                cout << value << " appears " << count << " times." << endl;
                break;
            case 5:
                cout << "Enter a position to remove from the array: ";
                cin >> newPos;
                
                if (newPos < 0 || newPos > size)
                {
                    cout << "Invalid Index " << endl;
                }
                else
                {
                    size = removeValue(arr, newPos, size);
                    displayArray(arr, size);
                }
                break;
            case 6:
                cout << "Enter a value to insert: ";
                cin >> insert;
                cout << "Enter a position to insert the value in: ";
                cin >> pos;
                pos--;
                if (pos < 0 || pos > size)
                {
                    cout << "Invalid Index " << endl;
                }
                else
                {
                    size = insertValue(arr, insert, pos, size);
                    displayArray(arr, size);
                }
            case 7:
                choice = -999;
                break;
        }
        if (choice == -999)
            return 0;
        else
            choice = displayMenu();

    }
}

int displayMenu()
{
    cout << "Enter -999 to stop" << endl;
    int choice;
    cout << "Menu:" << endl;
    cout << "1. Initialize Array" << endl;
    cout << "2. Display Array" << endl;
    cout << "3. Show min, max, sum and average " << endl;
    cout << "4. Search" << endl;
    cout << "5. Remove an element at a specific index" << endl;
    cout << "6. Add an element at specific index" << endl;
    cout << "7. Exit" << endl;

    cin >> choice;

    return choice;
    cout << endl;

}
int initializeArray (int arr[])
{
    int value;
    int counter = 0;
    for (int i = 0; i < SIZE; i++)
    {
        cout << "Enter  value or a negative number to stop: ";
        cin >> value;
        if (value < 0)
            break;
        else
        {
            arr[i] = value;
            counter++;
        }
    }
    cout << endl;
    return counter;
}
void displayArray(int arr[], int size)
{
    cout << "Array: ";
    for (int i = 0; i < size; i++)
    {
        cout << arr[i] << ' ';
    }
       cout << endl;
}
void calculate(int arr[], int& min, int& max, int& sum, double& average, int size)
{
    min = 9999;
    max = 0;
    for (int i = 0; i < size; i++)
    {
        sum+= arr[i];
        if (arr[i]> max)
            max = arr[i];
        if (arr[i] < min)
            min = arr[i];
    }
    average = (double) sum/size;
    cout << endl;
}
int countElement(int arr[], int size, int value)
{
    int count = 0;
    for(int i = 0; i < size; i++)
    {
        if (arr[i] == value)
            count++;
    }
    cout << endl;
    return count;

}
int removeValue(int arr[], int newPos, int size)
{
    int i = 0;
    for(i = newPos - 1; i < size - 1; i++)
        {
            arr[i] = arr[i+1];
        }

        size--;

        cout << endl;
        return size;
}
int insertValue(int arr[], int value, int pos, int size)
{
    if (size == 10)
        cout << "Array full" << endl;
    else
    {
        int i;
        for (i = size - 1; i >= pos; --i) {
            arr[i + 1] = arr[i];
        }
        arr[pos] = value;
        ++size;
    }
    cout << endl;
    return size;
}


Thanks!
Last edited on
Hello, late reply Sorry but i found your bug, the bug is very simple so I wouldn't doubt it if you already found it. Whenever you input 7 into displaymenu(); function it is sent to the while loop which in this instance is not -999 so the while loop is true but when it gets to the if statement the value of size is indeed 0 so it would turn to choice 1. And by the way, nice project idea, it was an interesting read!
Topic archived. No new replies allowed.