Note that c++ arrays are 0 based. That is, array[0] is the first element, not array[1].
Dave
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
nope, if you make an array of length 5
int test[5];
then you have to use elements 0 to 4
for (i=0; i<5; i++)
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
I've translated some of the lines of my original program, because there the text that you can read on the screen is Dutch, I'm from Belgium you know..
If you test it out... You'll see that the array doesn't read in one of the txt-files..
We can't test it out if you don't supply the input text files. Which text file doesn't read in?
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
It doesn't read any of them..
Don't assume that a run-time error means that the program isn't reading from the files. Place some cout statements inside the function that reads from the file that display the parameters passed to the function and display what is being read in. Make sure that the parameters are what you want them to be, and that what you want to read in is actually being stored how you want it to be stored, and that you are going through the loop the correct number of times.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
You never initialized aantal_getallen
which is the number of records to read/sort/print.
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
But I don't have errors, the program works..
But when I choose a file, it doesn't give a sorted array of it..
I tested the function with some cout statements and they worked..
If the program worked, you wouldn't be posting here. It doesn't work, as you've said in your earlier posts, and whatever cout statements you placed elsewhere, you have placed none in lees_bestand or you have taken them out already. That is where you need them. You need to figure out how many times you are going through that do...while loop in order to successfully debug the program.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Yes, that's true, I didn't put those cout statements in my posts..
But I've been working on it furter..
And it reads and prints the lists..
But there's one problem, if it always reads one more line in the file I think.. cause the first number of the sorted array is always something like :"-858993460", and I didn't put this number in one of the file .. :s
I first thought that it was because I had set "aantal_getallen" equal to 1, but if I set it equal to zero, it displays only the first number of the file..
-858993460 is almost certainly an uninitialized variable. Do you ever initialize element 0 to anything?I first thought that it was because I had set "aantal_getallen" equal to 1, but if I set it equal to zero, it displays only the first number of the file..
Your first instinct is probably correct on this. If you set it to 1, make sure that element 0 gets assigned to something. The way to solve it is to fix your loop:
for (i=0; i<aantal; i++)
{
if ( ! bestand.eof ())
{
bestand >> bestandrij[i];
aantal++;
}
else
{
break;
}
}
Will i ever catch up to aantal so that the condition of the for-loop is false? If you always go through the loop 0 times, 1 time, or millions of times, then it's not really a for-loop and you should change it. Your real loop control here is the eof and the break , so if the for-loop serves no purpose and simply hinders the number of times you go through the loop, change it or simply delete it.
Also consider initializing all of your array elements to some nonsense value in the beginning. If that nonsense value shows up in the end, chances are extremely high that you never initialized it to a REAL value.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711