Code not reading all numbers

The 30 numbers in the file I'm reading from are:
15
95
12
52
27
94
55
68
51
86
9
20
49
54
3
35
17
23
13
16
11
28
39
43
21
1
26
22
81
30

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
    int numbers [30];
    int numbersSorted[30];
    int menuChoice;
    int bubbleCounter=0;

    ifstream file;
    file.open("nums.txt");

    for (int j=0;j<30;j++)
    {
        file >> numbers[j];
    }

    for (int j=0;j<30;j++)
    {
        numbersSorted[j]=numbers[j];
    }

    while (menuChoice!=8)
    {
        cout << "1. Show unsorted list" << endl;
        cout << "2. Show sorted list" << endl;
        cin >> menuChoice;
        cout << endl;

        switch (menuChoice)
        {
            case 1:
                cout << "The numbers are:" << endl;
                for (int j=0;j<30;j++)
                {
                    cout << numbers[j] << endl;

                }
                cout << endl;
                break;

            case 2:
                for (int j=0;j<30;j++)
                {
                    for (int i=0;i<30-bubbleCounter;i++)
                    {
                        if (numbersSorted[i]> numbersSorted[i+1])
                        {
                            int x=numbersSorted[i+1];
                            numbersSorted[i+1]=numbersSorted[i];
                            numbersSorted[i]=x;
                        }
                    }
                    bubbleCounter++;

                }

                for (int j=0;j<30;j++)
                {
                    cout << numbersSorted[j] << endl;
                }
                cout << endl;
                break;


When I sort the array "numbers" in case 2 and output it, the greatest number is 95 (which it should be). But when I use the array "numbersSorted" in case 2, and output it, the greatest number is the second greatest number in the 30 numbers, 94.
You are going to go out of bounds on line 43 for i+1 when, on the initial pass, i=29, since bubblecounter is initially 0.

Thereafter, anything could happen.

Please show complete code in future.

Thank you so much. And this is complete code isn't it? Only int main is missing.
It isn't complete code unless we can compile and run it.

I replied because I could see one obvious error ... but my eyesight isn't good enough to spot everything without trying it out.
Ohh ok, I will post all code from now on. I made bubbleCounter equal to 1 now, and it works with numbersSorted. But why did it work for the array "numbers" even when bubbleCounter was 0? I'm wondering because numbersSorted and numbers start off as the same array.
Last edited on
bubbleCounter isn't used, and there isn't an out-of-bounds problem, until the loop starting on line 41.

There doesn't seem to be any problem with case 1.
I meant for case 2. If i remove the "Sorted"s, it includes 95.
If it accesses beyond array bounds then it takes whatever happens to be in that memory slot ... which could be anything ... and could even give different answers on different machines or with different compile options.

In c++, "lucky dip" may not be all that good; I'd avoid going beyond array bounds.
Topic archived. No new replies allowed.