Help with Linked Lists

I'm having trouble getting linked lists down.
I have this piece of a realty company program that I want to display input from a file entitled "listings.txt".

My output is only displaying one of the listings and not both of them. Please help.

Thanks!

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>    
#include <cstdlib>        
#include <iomanip>       
#include <fstream>
#include <sstream>
#include <string>
#include <cctype>

using namespace std;

enum statusEnum {AVAILABLE, CONTRACT, SOLD};

struct listingInfo
{
	int mls_number;
	double price;
	statusEnum homeStatus; 
	string zipcode;
	string company;
	
	listingInfo* link;
};

int main()
{
	ifstream listings_file;
	string file_name = "LISTINGS.TXT";
	char loadFile;
	listingInfo* top = NULL;
	listingInfo* newListing = new listingInfo;
	listingInfo* display = top;
	int counter = 0;
	char proceed;
	string line;
	int tempStatus;
	string stat;
	int count = 0;
	
	listings_file.open(file_name.c_str());
		
	top = newListing;
	while ( getline(listings_file, line) && line.length() )
	{
		istringstream listings_line(line);
		
                listings_line >> newListing->mls_number;
                listings_line >> newListing->price;
                listings_line >> tempStatus;
                newListing->homeStatus = static_cast<statusEnum>(tempStatus);
                listings_line >> newListing->zipcode;
                getline(listings_line, newListing->company, ' ');
                getline(listings_line, newListing->company);
						               
                counter++;
          
       	        top = newListing;
        }
    
	cout << endl << endl;
	cout << "         Asking    Listing" << endl;
	cout << "MLS#     Price     Status      Zip Code     Realtor" << endl;
	cout << "------   -------   ---------   ----------   ------------" << endl;
					
	display = top;
				
	while( count < counter && display != NULL )
	{
		if( display->homeStatus == 2)
			stat = "Sold    ";
		if( display->homeStatus == 1)
			stat = "Contract ";
		if( display->homeStatus == 0)
			stat = "Available";
									
		cout << display->mls_number << setw(10)
			 << display->price << setw(12)
			 << stat << setw(13)
			 << display->zipcode << "   "
			 << display->company << endl;
		count++;
				
		display = display->link;
	}

    cout << endl << endl;
    system("PAUSE");
    return 0;
}


Listings.txt:
1
2
111111 229700 0 80501-2345 Jones Realty
345678 176900 1 80513-2918 Metro Brokers

Last edited on
You read each record into the same structure pointed by top. So the second record overwrites the first record.
Last edited on
I can't figure out how to get it to not overwite. Even if I remove top = newListing; from the input loop, i get the same result... I understand how to put one value into this structure, but I don't get how the actual linked list premise works when you're adding multiple elements to the structure...
Last edited on
You need to allocate memory for the structure each time you read a new record.
Last edited on
Topic archived. No new replies allowed.