-3

I am trying to open a text file and pass the lines of the text file to a vector. The first digit in each line is the size of the vector and since I do not know the end point of the text file I am using a while loop to find the end. The idea is that I can take a text file and run a merge sort on it. So, for example:

3 5 4 9
5 0 2 6 8 1

sorted it would be become:

4 5 9
0 1 2 6 8 

The problem I am having is that when I sort a vector that is larger than the prior vector (as in the example) I do not get output. It is probably something simple that I just have over looked. I am pretty sure the issue is in the code below. Thanks for any pointers.

while (!file.eof())
{
    int size;
    file >> size;
    vector<int> myVector(size); 

    int n = 0; 
    while (n < size && file >> myVector[n])
    {
        ++n;
    }

    sort(myVector);

    for (int j = 0; j < size; ++j)
    {
        if (file.eof()) break; 

        cout << myVector[j] << ' ';
    }

    cout << '\n'; 
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
jarbeme
  • 1
  • 3
  • 2
    There is no output in that snippet to actually reproduce your problem. – Zeta Jan 10 '18 at 20:30
  • I have edited it for output. – jarbeme Jan 10 '18 at 20:32
  • 2
    Why do you check `file.eof()` in the display loop? – François Andrieux Jan 10 '18 at 20:33
  • 2
    Why do you check `file.eof()` in the outer loop? See https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong –  Jan 10 '18 at 20:37
  • No, I can remove the sort call or put it there and either way I end up with the same results. Just either sorted or not sorted. – jarbeme Jan 10 '18 at 20:42
  • Give [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) a read. `while (n < size && file >> myVector[n])` is correct, but `while (!file.eof())` leaves `file >> size;` unprotected. – user4581301 Jan 10 '18 at 20:43

2 Answers2

1

The problem is this line:

if (file.eof()) break; 

Once you've read the last line of the file, file.eof() will be true. So the first time through the loop that's supposed to print the sorted vector, you break out of the loop and don't print anything. It has nothing do with whether the vector is larger than the previous vector, it's just a problem with the last line of the file. The fix is to get rid of that unnecessary line.

You also need to change the main loop. while (!file.eof()) is the wrong way to loop over a file's contents (see the linked questions for full explanations). Use:

int size;
while (file >> size) {
    ...
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

because of the line :

if(file.eof()) break;

if you get to eof your program wont print anything since you break the printing loop on its first iteration for instance - if there are no chars after 8 in your example - you wont get output ,but even a single space can change that

besides that - is there any chance or cases that your sorting function clears a vector ? or changes it ?

Daniel
  • 106
  • 6