Printing an unsorted list

This is a discussion on Printing an unsorted list within the C++ Programming forums, part of the General Programming Boards category; I've run into a problem trying to print this list. I'm getting the list from a text file and loading ...

Yes omishompi - the 'other way' is sitting down with a respectable learning resource for C++ and learning the right way. I seriously recommend Accelerated C++ by Koenig and Moo - it's only around $30 on amazon.com.

1)The basic rule for reading from a file is: the read statement should be a while loop conditional, e.g.

Code:

while( inputFile>>data1 )
{
...
...
}

inputFile>>data1 calls a function that reads data into data1 and then returns the inputFile object, which converts the while conditional to:

while(inputFile)

If any errors occur that will prevent you from reading data from the file, the inputFile object will evaluate to false in the while conditional. End of file(eof) is considered an error, so the while loop will terminate correctly when you reach the end of the data in a file. However, there are other possible errors that can occur while reading from a file. If one of those errors occurs, then a loop such as:

Code:

while(!inputFile.eof())
{
}

will try to keep reading data because it hasn't encountered eof yet--but the error will prevent the read statement from reading any data. So the loop will keep looping indefinitely: not able to read in any data because of the error and therefore never reaching eof.

Your loop will work--you're just duplicating a bit. Move the entire read statement into the while conditional.

What value is length? Hopefully, it is initialized to 0. To read in data try this:

1) Make your read statement the while loop conditional and have it read in data[length].
3) Inside the loop increment length.

That will read in all the data in the file, so make sure data[] is big enough.

typedef int ItemType;

What kind of typedef is that? You turn the easy to type "int" into the longer "ItemType". If it's just for practice, ok.

How in the world can I get this list to fully print?

Originally Posted by rockytriton

use a for loop

All the input from the file will be in your array called data[], and length will be the highest index position where input was read in. So, using a for-loop with length as the terminating condition, you can output all the values in data[]. Make sure you are getting the last value in data[] to display.