How to print a linked list

I am having issues printing my linked list. Can someone help me with the solution with this?

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//*****************************************************************************
//CODE FILENAME: Taylor-wk8-prog1
//DESCRIPTION: This program reads a file that contains data about apartments for 
//rent.  It then allows the user to add/delete listing from that file.
//CLASS/TERM: CS362/Summer-Term 2
//DESIGNER: Cody Taylor
//FUNCTIONS: readFile - reads the file
//			 newFile - ask user to load existing file
//		  	 userOption - user chooses what to do
//			 addNew - function is user wants to add new listing
//			 deleteList - function if user wants to delete a listing
//			 writeData- function to write data the user enters back to the file
//			 main - calls the functions
//*****************************************************************************

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>
#include <iomanip>
#include <cstdlib>
using namespace std;
struct houses{ //creates a struct for the listing
	string phoneNum;
	double price;
	string vacancy;
	houses* next;
};

houses *housesPtr,
*listTop = NULL;

const int NOT_FOUND = -1;	
const int MAX = 50000; //max listings
const string APT_INFO = "RENTALS.TXT";

bool ShowRentals(houses* &listTop);
char CheckChoice(char pick);
bool ReadFile(char& , houses* &listTop);//reads the file
char newFile();//ask user to load existing file
void userOption(int& count);//user chooses what to do
void addNew(int& count);//function is user wants to add new listing
void deleteList(int& count);//function if user wants to delete a listing
void writeData(int count);//function to write data the user enters back to the 
//file



//*********************************************************************
// FUNCTION: main
// DESCRIPTION: Calls the functions, gives description to user
//**********************************************************************
int main()
{


	char choiceOne;
	char choice;
	char useFile;		// read in data from existing file
	bool valid = true;	// bool flags for successful functions
	bool flag = true;
	bool okay = true;
	bool firstTry = true;	// for checking whether to skip newline
	bool content = true;	// there's data in the file
	bool correct = true;	// phone is correct
	string userPhone;		// entered at keyboard 
	int found = NOT_FOUND;
	string fileName;		// user entered filename
	string phoneNumber;		// user entered phone number
	char useData;
	int count = 0;

	cout << "This program will give a list of apartments to the user.  It will "
			"provide the user with the phone number, price, and whether or not "
			"the apartment is rented."
			<< endl << endl;
	cout << "Load existing data from a file? (Y/N) ";
	cin >> choiceOne;
	choiceOne = toupper(choiceOne);

	
	if (choiceOne == 'Y')	// only if read file in
	{
		flag = ReadFile (choiceOne, listTop);

	}	
	
//	do		// whether file read or not
//	{
		
		cout << endl << endl;
		cout << "Main Menu: " << endl;
		cout << "   S - Show Rentals" << endl;
		cout << "   M - Modify Monthly Rent" << endl;
		cout << "   A - Add Rental" << endl;
		cout << "   D - Delete Rental" << endl;
		cout << "   E - Exit Program" << endl;
		cout << "Please enter selection from choices above: ";
		cin >> choice;
		choice = toupper(choice);

		choice = CheckChoice (choice);
return 0;
}
bool ReadFile (char& useFile, houses* &listTop)
{
		ifstream rentalFile;
		char answer;
		char pauseChar;
		string phoneNum;
		double price;
		string phone;
		double rent;
		bool vacancy;
		string fileName;
		bool rented;
		bool flag;
		houses *newRental;			
		listTop = NULL;
		houses *last = NULL;		

		
		do
		{
				// if user wants to use existing data
			if (useFile == 'Y')		
			{
				cout << "Enter the filename: ";
				cin >> fileName;
				
				rentalFile.open(fileName.c_str());
				
				if (!rentalFile)
				{
					cout << "Error! File not found.";
					cin.clear();			
					cout << endl << "Enter new filename (Y) or go directly ";
					cout << "to action menu (N)?  ";
					cin >> useFile;
					useFile = toupper (useFile);
					flag = false;
				}
			}
			
		} while ((!rentalFile) && (useFile == 'Y'));
			
		if (useFile == 'Y')	
		{	
			rentalFile >> phone;			// prime read
			
			newRental = new (nothrow) houses;
			newRental->next = NULL;

	
			do
			{

				if (rentalFile)
				{
					rentalFile >> rent >> vacancy;

					
					if ( newRental == NULL)
					{
						cout << "Cannot allocate space." << endl << endl;	
						cin >> pauseChar;	
						flag = true;
					
					}
					else
					{
						if (listTop == NULL)
						{
							last = newRental->next;
							listTop = newRental;
						}
						else
						{
							newRental->next = listTop;
							listTop = newRental;

									
						}
						newRental -> phoneNum = phone;
						newRental -> price = rent;
						newRental -> vacancy = vacancy;
					
						rentalFile >> phoneNum;
						newRental = new (nothrow) houses;
						newRental->next = NULL;


					}
				}
				
			} while ((newRental != NULL) && (rentalFile));
			
			
			rentalFile.close();
			flag = true;

		}
			
		return flag;
	}

char CheckChoice(char pick)
	{
		while ((pick != 'S') && (pick != 'M') && (pick != 'A') && (pick != 'D') && (pick != 'E'))
		{
			cout << endl;
			cout << "Invalid entry, please enter a valid choice from the list below.\n";
			cout << endl << endl;
			cout << "Main Menu: " << endl;
			cout << "   S - Show Rentals" << endl;
			cout << "   M - Modify Monthly Rent" << endl;
			cout << "   A - Add Rental" << endl;
			cout << "   D - Delete Rental" << endl;
			cout << "   E - Exit Program" << endl;
			cout << "Please enter selection from choices above: ";
			cin >> pick;

			pick = toupper(pick);
		}
		return pick;
	}
bool ShowRentals(houses* &listTop)
{

}
closed account (N36fSL3A)
We could help if you told us your issue.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void ShowRentals(houses* &listTop)
{
//copy the Node into temporary one
houses * walker = listTop ;


while(walker) //This loop will stop when it reaches the end of the list.. ( when walker == null)
{ 
cout << walker->phonenum <<"      "  << walker-> price << "      " << walker->vacancy << '\n' ;
walker = walker->next ; 
}


}


I don't know if this is what you want exactly .. But I give it a try ...
and why it is return bool in your code ??
and why the parameter is a reference to pointer and not a pointer only ??
I would go all the way to const houses * parameter.

Then, for education, a ostream & operator<< ( ostream &, const houses & );
Topic archived. No new replies allowed.