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 ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    20

    Printing an unsorted list

    I've run into a problem trying to print this list. I'm getting the list from a text file and loading it item by item like this

    Code:
    #include <fstream>
    
    using namespace std;
    
    const int MAX_LENGTH = 50;
    
    typedef int ItemType;
    
    class List
    {
    public:
    	List(); //Default constructor
    	void GetList(ifstream&); 
    	bool IsEmpty();
    	bool IsFull();
    	int LengthNow();
    	void Insert(ItemType item);
    	void Delete(ItemType item);
    	bool IsPresent(ItemType item);
    	void SelSort();
    	void Print();
    
    private:
    	int length;
    	ItemType data[MAX_LENGTH];
    };
    Code:
    void List::GetList(ifstream& infile)
    {
    	ItemType item;
    
    	while(infile)
    	{
    		data[length] = item;
    		infile >> item;	
    		length++;
    		cout << endl;
    	}
    }
    The list looks something like:
    1492
    1066
    1917
    1848
    1367

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

    Code:
    void List::Print()
    {
         ??????
    }

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    use a for loop

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    Code:
    class List
    {
    public:
    
    	typedef int value_type;
    	typedef value_type*       iterator;
    
    	iterator begin() const { return data; }
    	iterator end()   const { return data + length; }
    
    	...
    
    	void Print();
    
    private:
    	std::size_t length;
    	value_type data[MAX_LENGTH];
    };

    Code:
    void List::Print()
    {
    	for( iterator iter = begin(); iter != end(); ++iter )
    	{
    		std::cout << *iter << '\n';
    	}
    }
    Last edited by miken; 11-13-2005 at 08:53 AM.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    Thanks for the help. Those pointers are a little more advanced than I. Is there another way?

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    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.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.

    2)
    Code:
    while(infile)
    	{
    		data[length] = item;
    		infile >> item;	
    		length++;
    		cout << endl;
    	}
    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?
    Quote 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.
    Last edited by 7stud; 11-13-2005 at 03:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 02:19 PM
  3. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 12:21 PM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21