0

My goal is to have a player input a bunch of numbers for an array then that array is written into a text file. Another part of it is to be able to receive a bunch of numbers from a text file and put them into a sorted array of highest to lowest then output that array. But for some reason, I'm getting a lot of errors, ones that i feel like with some research I can fix. Unfortunately, there is one very confusing situation where I test to make sure the unsorted array is correct by outputting each element of the array. This is not a part of the final program but a test for now. I have a for loop that does so and it works perfectly, outputting each number as expected. Then in the next for loop, the exact same thing is supposed to happen but the numbers being outputted are all messed up. I do not understand how. Code below

void readFile(string fName) {
    string fileName = fName + ".txt";
    ifstream myFile(fileName);
    char c;
    string num;
    int count = 0;

    // Bring the array from file to int array
    while (!myFile.eof()) {
        myFile.get(c);
        if (isspace(c) && num != "") {
            int n = stoi(num);
            intArray[count] = n;
            count++;
            num = "";
            continue;
        }
        if (!myFile.eof()) {
            num += c;
        }
    }

    for (int i = 0; i < 10; i++) {
        cout << intArray[i] << endl;
    }

    // Sort the array higest to lowest
    for (int i = 0; i < 10; i++) {
        cout << intArray[i] << "       ";
        for (int j = 9; j >= i; j--) {
            if (j == 0) {
                continue;
            }
            if (intArray[j] > intArray[j - 1]) {
                int temp = arr[j];
                intArray[j] = intArray[j - 1];
                intArray[j - 1] = temp;
            }
        }
        cout << endl;
    }
}

sorry about the formatting above, its being weird so imagine the code is within the function.

This is what this outputs:

1
2
3
4
5
6
7
8
99
234
1
1
1
1
1
1
1
1
1
1

The numbers before the series of 1's is the actual array, the 1's is what is apparently the array according the cout in the last section of code where it says cout << intArray[i]

273K
  • 29,503
  • 10
  • 41
  • 64
  • You're second loop is attempting (and failing) to sort the list. Because the array is being modified inside the second loop you would not expect it to print out the same values as the first loop. Is there a reason why your not using a std::vector for storage and std::sort? – PeteBlackerThe3rd Nov 21 '19 at 00:11
  • Recommended reading: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Nov 21 '19 at 00:26

1 Answers1

2

Your array does appear to be sorted. The reason for all the ones being printed is due to the location of the cout << within the outer loop.

Consider what your array looks like after your first iteration through the inner loop:

234,1,2,3,4,5,6,7,8,99

Now consider that you've incremented i to 1 in the outer loop. When you index your array intArray[i], the ith element is now 1 because you correctly moved it there. Each time, you're moving your smaller elements up one position in the array, then indexing to the position where the 1 is.

Don't try to print the sorted array while you're sorting it. Instead loop over it and print it after the sort.

jkb
  • 2,376
  • 1
  • 9
  • 12
  • yeah i kinda realize now that im dumb – Maddman Nov 21 '19 at 00:35
  • "Dumb" mistakes are often easily exposed by stepping through the program with a debugger and keeping an eye out for where the program does the unexpected. Dumb isn't making a programming mistake. Dumb is not taking advantage of the tools available to help find the mistake. Time spent learning to use your development environment's debugger is time well spent. – user4581301 Nov 21 '19 at 00:47