Linked List and Input/Output Problems

I'm having problems having a program read data input from a txt file, putting in a linked list, and then outputting the information into a different txt file.
Last edited on
OK, thanks for letting us know
I need help with this..Can anyone help me
show us what you've done so far and where exactly you're getting stuck
This is my implementation file. The program is creating the new txt file and putting the header info but not the data from the input txt file.



//************************************
//* IMPLEMENTATION FILE (Retail.cpp) *
//************************************

#include "stdafx.h"
#include "Retail.h"

Inventory* FirstRecordPointer;

//***************
//* Constructor *
//***************
Inventory::Inventory()
{
ProductIDNumber = 0;
ManufacturerIDNumber = 0;
QuantityInStock = 0;
WholesalePrice = 0.0;
MarkUpPercentage = 0.0;
RetailPrice = 0.0;
ProductDescription[24] = '\0';
NextRecordPointer = NULL;
}



//**********************************************
//* Add an item to the head of the linked list *
//**********************************************
void Inventory::InsertItem( int InitProductIDNumber, int InitManufacturerIDNumber, double InitWholesalePrice,
double InitMarkUpPercentage, char InitProductDescription[], int InitQuantityInStock,
Inventory* CurrentRecordPointer )
{
Inventory* CurrentPointer;
Inventory* TrailPointer;
bool Found;

ProductIDNumber = InitProductIDNumber;
ManufacturerIDNumber = InitManufacturerIDNumber;
WholesalePrice = InitWholesalePrice;
MarkUpPercentage = InitMarkUpPercentage;
strcpy_s( ProductDescription, InitProductDescription );
QuantityInStock = InitQuantityInStock;


//*********************************************************
//* First Case: The list is empty. The node containing *
//* the new item is the only node and thus the first node *
//* in the list. *
//*********************************************************
if( FirstRecordPointer == NULL )
{
FirstRecordPointer = CurrentRecordPointer;
}
else
{
CurrentPointer = FirstRecordPointer;
Found = false;

//**************************
//* Search the linked list *
//**************************
while( CurrentPointer != NULL && !Found )
{
if( CurrentPointer->ProductIDNumber >= InitProductIDNumber )
{
Found = true;
}
else
{
TrailPointer = CurrentPointer;
CurrentPointer = CurrentPointer->GetLink( );
}
}

//*****************************************************
//* Second Case: The new item is smaller than the *
//* smallest item in the list. The new item goes at *
//* the beginning of the list and the head pointer is *
//* adjusted. *
//*****************************************************
if( CurrentPointer == FirstRecordPointer )
{
NextRecordPointer = FirstRecordPointer;
FirstRecordPointer = CurrentRecordPointer;
}

//****************************************************
//* Third Case: The new item is to be inserted *
//* within the list. If the item is larger than all *
//* items in the list, it is inserted at the end of *
//* the list. Thus, the value of *
//* "CurrentPointer" is NULL and the new item *
//* is inserted after "TrailPointer." If the item is *
//* to be inserted somewhere in the middle of the *
//* list, the new item is inserted between *
//* "TrailPointer" and "CurrentPointer." *
//****************************************************
else
{
TrailPointer->NextRecordPointer = CurrentRecordPointer;
NextRecordPointer = CurrentPointer;
}
}
}

//************************************************
//* Count the number of items in the linked list *
//************************************************
int Inventory::CountItems( )
{
Inventory* CurrentRecordPointer;
int ItemCounter = 0;

for( CurrentRecordPointer = FirstRecordPointer;
CurrentRecordPointer != NULL;
CurrentRecordPointer = CurrentRecordPointer->GetLink( ) )
{
ItemCounter++;
}

return( ItemCounter );
}

//************************************
//* Write the contents of the linked *
//* list to an external report file *
//************************************
void Inventory::ListRecords( char* ReportName )
{
ofstream OutputFile;
Inventory* CurrentRecordPointer;
char Separator[150] = "_____________________________________________________________________________________________________________________________________________________";

OutputFile.open((char*)(void*)Marshal::StringToHGlobalAnsi (String::Concat(Directory::GetCurrentDirectory(),
"\\InventoryFileForOutput.txt")));


//********************************************
//* Display header for inventory information *
//********************************************
OutputFile << endl << Separator << endl << endl;
OutputFile << setw(60) << setfill(' ') << ReportName << endl;
OutputFile << Separator;
OutputFile << endl << endl << setw(15) << setfill(' ') << "Product ID Number"
<< setw(30) << setfill(' ') << "Manufacturer ID Number"
<< setw(25) << setfill(' ') << "Product Description"
<< setw(25) << setfill(' ') << "Wholesale Price"
<< setw(21) << setfill(' ') << "Mark-Up Percentage"
<< endl << Separator << endl << Separator << endl;

//**********************************************************
//* Records are listed until the NULL value is encountered *
//**********************************************************
CurrentRecordPointer = FirstRecordPointer;
while( CurrentRecordPointer !=NULL )
{
OutputFile << endl;
OutputFile << right;
OutputFile << endl << setw(15) << setfill(' ')
<< CurrentRecordPointer->GetProductIDNumber( );
OutputFile << setw(15) << setfill(' ')
<< CurrentRecordPointer->GetManufacturerIDNumber( );
OutputFile << setw(35) << setfill(' ')
<< CurrentRecordPointer->GetProductDescription( );
OutputFile << fixed << showpoint << setprecision(2) << setw(15) << setfill(' ') << " $ "
<< CurrentRecordPointer->GetWholesalePrice( );
OutputFile << setw(10) << setfill(' ')
<< CurrentRecordPointer->GetMarkUpPercentage( );
OutputFile << "The Number Of Items In The List: " << ItemCounter;
CurrentRecordPointer = CurrentRecordPointer->GetLink( );
}

OutputFile << endl << Separator << endl;

OutputFile.close( );
return;
}
//*************************************************
//* Return the value of the private class members *
//*************************************************
int Inventory::GetProductIDNumber( ) const
{
return( ProductIDNumber );
}

int Inventory::GetManufacturerIDNumber( ) const
{
return( ManufacturerIDNumber );
}

char* Inventory::GetProductDescription( ) const
{
return( ProductDescription );
}

double Inventory::GetWholesalePrice( ) const
{
return( WholesalePrice );
}

double Inventory::GetMarkUpPercentage( ) const
{
return( MarkUpPercentage );
}

int Inventory::GetQuantityInStock( ) const
{
return( QuantityInStock );
}

double Inventory::GetRetailPrice( ) const
{
return(RetailPrice );
}

Inventory* Inventory::GetLink( ) const
{
return NextRecordPointer;
}

//**********************************************
//* Set the value of the private class members *
//**********************************************
void Inventory::SetProductIDNumber( int InitProductIDNumber )
{
ProductIDNumber = InitProductIDNumber;
}

void Inventory::SetManufacturerIDNumber( int InitManufacturerIDNumber )
{
ManufacturerIDNumber = InitManufacturerIDNumber;
}

void Inventory::SetProductDescription( char* InitProductDescription )
{
strcpy_s( ProductDescription, InitProductDescription );
}

void Inventory::SetWholesalePrice( double InitWholesalePrice )
{
WholesalePrice = InitWholesalePrice;
}

void Inventory::SetMarkUpPercentage( double InitMarkUpPercentage )
{
MarkUpPercentage = InitMarkUpPercentage;
}

void Inventory::SetRetailPrice( double InitRetailPrice )
{
RetailPrice = InitRetailPrice;
}

void Inventory::SetLink( Inventory* InitLink )
{
NextRecordPointer = InitLink;
}
Last edited on
can you please repost using code-tags, v difficult reading such a long program unformatted. thanks
what do you mean using code-tags?
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//************************************
//* IMPLEMENTATION FILE (Retail.cpp) *
//************************************

#include "stdafx.h"
#include "Retail.h"

Inventory* FirstRecordPointer;

//***************
//* Constructor *
//***************
Inventory::Inventory()
{
	ProductIDNumber = 0;
	ManufacturerIDNumber = 0;
	QuantityInStock = 0;
	WholesalePrice = 0.0;
	MarkUpPercentage = 0.0;
	RetailPrice = 0.0;
	ProductDescription[24] = '\0';
	NextRecordPointer = NULL;
}



//**********************************************
//* Add an item to the head of the linked list *
//**********************************************
void Inventory::InsertItem( int InitProductIDNumber, int InitManufacturerIDNumber, double InitWholesalePrice, 
						   double InitMarkUpPercentage, char InitProductDescription[], int InitQuantityInStock, 
						   Inventory* CurrentRecordPointer )
{
	Inventory* CurrentPointer;
	Inventory* TrailPointer;
	bool Found;

		ProductIDNumber = InitProductIDNumber;
		ManufacturerIDNumber = InitManufacturerIDNumber;
		WholesalePrice = InitWholesalePrice;
		MarkUpPercentage = InitMarkUpPercentage;
		strcpy_s( ProductDescription, InitProductDescription );
		QuantityInStock = InitQuantityInStock;
		
		
		//*********************************************************
		//* First Case: The list is empty. The node containing    *
		//* the new item is the only node and thus the first node *
		//* in the list.                                          *
		//*********************************************************
		if( FirstRecordPointer == NULL )
		{
			FirstRecordPointer = CurrentRecordPointer;
		}
		else
		{
			CurrentPointer = FirstRecordPointer;
			Found = false;
		
			//**************************
			//* Search the linked list *
			//**************************
			while( CurrentPointer != NULL && !Found )
			{
				if( CurrentPointer->ProductIDNumber >= InitProductIDNumber )
				{
					Found = true;
				}
				else
				{
					TrailPointer = CurrentPointer;
					CurrentPointer = CurrentPointer->GetLink( );
				}
			}

			//*****************************************************
			//* Second Case: The new item is smaller than the     *
			//* smallest item in the list. The new item goes at   *
			//* the beginning of the list and the head pointer is *
			//* adjusted.                                         *
			//*****************************************************
			if( CurrentPointer == FirstRecordPointer )
			{
				NextRecordPointer = FirstRecordPointer;
				FirstRecordPointer = CurrentRecordPointer;
			}

			//****************************************************
			//* Third Case: The new item is to be inserted       *
			//* within the list. If the item is larger than all  *
			//* items in the list, it is inserted at the end of  *
			//* the list. Thus, the value of                     *
			//* "CurrentPointer" is NULL and the new item        *
			//* is inserted after "TrailPointer." If the item is *
			//* to be inserted somewhere in the middle of the    *
			//* list, the new item is inserted between           *
			//* "TrailPointer" and "CurrentPointer."             *
			//****************************************************
			else
			{
				TrailPointer->NextRecordPointer = CurrentRecordPointer;
				NextRecordPointer = CurrentPointer;
			}
		}
}

//************************************************
//* Count the number of items in the linked list *
//************************************************
int Inventory::CountItems( )
{
	Inventory* CurrentRecordPointer;
	int ItemCounter = 0;

	for( CurrentRecordPointer = FirstRecordPointer;
		CurrentRecordPointer != NULL;
		CurrentRecordPointer = CurrentRecordPointer->GetLink( ) )
	{
		ItemCounter++;
	}

	return( ItemCounter );
}

//************************************
//* Write the contents of the linked * 
//* list to an external report file  *
//************************************
void Inventory::ListRecords( char* ReportName )
{
	ofstream OutputFile;
	Inventory* CurrentRecordPointer;
	char Separator[150] = "_____________________________________________________________________________________________________________________________________________________";

	OutputFile.open((char*)(void*)Marshal::StringToHGlobalAnsi (String::Concat(Directory::GetCurrentDirectory(),
		"\\InventoryFileForOutput.txt")));

	
	//********************************************
	//* Display header for inventory information *
	//********************************************
	OutputFile << endl << Separator << endl << endl;
	OutputFile << setw(60) << setfill(' ') << ReportName << endl;
	OutputFile << Separator;
	OutputFile << endl << endl << setw(15) << setfill(' ') << "Product ID Number"
		<< setw(30) << setfill(' ') << "Manufacturer ID Number"
		<< setw(25) << setfill(' ') << "Product Description"
		<< setw(25) << setfill(' ') << "Wholesale Price"
		<< setw(21) << setfill(' ') << "Mark-Up Percentage"
		<< endl << Separator << endl << Separator << endl;

	//**********************************************************
	//* Records are listed until the NULL value is encountered *
	//**********************************************************
	CurrentRecordPointer = FirstRecordPointer;
	while( CurrentRecordPointer !=NULL )
	{
		OutputFile << endl;
		OutputFile << right;
		OutputFile << endl << setw(15) << setfill(' ') 
			<< CurrentRecordPointer->GetProductIDNumber( );
		OutputFile << setw(15) << setfill(' ') 
			<< CurrentRecordPointer->GetManufacturerIDNumber( );
		OutputFile << setw(35) << setfill(' ')
			<< CurrentRecordPointer->GetProductDescription( );
		OutputFile << fixed << showpoint << setprecision(2) << setw(15) << setfill(' ') << " $ " 
			<< CurrentRecordPointer->GetWholesalePrice( );
		OutputFile << setw(10) << setfill(' ')
			<< CurrentRecordPointer->GetMarkUpPercentage( );
		OutputFile << "The Number Of Items In The List: " << ItemCounter;
		CurrentRecordPointer = CurrentRecordPointer->GetLink( );
	}

	OutputFile << endl << Separator << endl;

	OutputFile.close( );
	return;
}
//*************************************************
//* Return the value of the private class members *
//*************************************************
int Inventory::GetProductIDNumber( ) const
{
	return( ProductIDNumber );
}

int Inventory::GetManufacturerIDNumber( ) const
{
	return( ManufacturerIDNumber );
}

char* Inventory::GetProductDescription( ) const
{
	return( ProductDescription );
}

double Inventory::GetWholesalePrice( ) const
{
	return( WholesalePrice );
}

double Inventory::GetMarkUpPercentage( ) const
{
	return( MarkUpPercentage );
}

int Inventory::GetQuantityInStock( ) const
{
	return( QuantityInStock );
}

double Inventory::GetRetailPrice( ) const
{
	return(RetailPrice );
}

Inventory* Inventory::GetLink( ) const
{
	return NextRecordPointer;
}

//**********************************************
//* Set the value of the private class members *
//**********************************************
void Inventory::SetProductIDNumber( int InitProductIDNumber )
{
	ProductIDNumber = InitProductIDNumber;
}

void Inventory::SetManufacturerIDNumber( int InitManufacturerIDNumber )
{
	ManufacturerIDNumber = InitManufacturerIDNumber;
}

void Inventory::SetProductDescription( char* InitProductDescription )
{
	strcpy_s( ProductDescription, InitProductDescription );
}

void Inventory::SetWholesalePrice( double InitWholesalePrice )
{
	WholesalePrice = InitWholesalePrice;
}

void Inventory::SetMarkUpPercentage( double InitMarkUpPercentage )
{
	MarkUpPercentage = InitMarkUpPercentage;
}

void Inventory::SetRetailPrice( double InitRetailPrice )
{
	RetailPrice = InitRetailPrice;
}

void Inventory::SetLink( Inventory* InitLink )
{
	NextRecordPointer = InitLink;
}
I'm sorry but it doesn't seem like you're serious about getting help. Your header file, main(), input file – all are missing and we have to keep asking you for every bit of information at each stage. If you rally want help please spend some time drafting a thoughtful, self-contained post instead of dumping anything that comes to hand. Think what pieces of information anybody who attempts to help you will need and try and address those as much as possible to start with rather than wasting time going back and forth. Or maybe someone more perspicacious can discern your problem(s)
sorry I didn't know that I needed to put all of that in and yes I'm serious about getting help.
Here is the Header File

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
//*********************************
//* SPECIFICATION FILE (Retail.h) *
//*********************************

#ifndef RETAIL_H
#define RETAIL_H

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;
using namespace System;
using namespace System::IO;
using namespace System::Windows::Forms;
using namespace System::Runtime::InteropServices;

class Inventory
{
public:

	//***************
	//* Constructor *
	//***************
	Inventory( );

	//*******************************************
	//* Create a report listing the records     *
	//* that currently comprise the linked list *
	//*******************************************
	void ListRecords( char* );

	//**********************************
	//* Add an item to the linked list *
	//**********************************
	void InsertItem( int, int, double, double, char [], int, Inventory* );

	//************************************************************
	//* Count and display the number of items in the linked list *
	//************************************************************
	int CountItems( );

	//*******************************************
	//* Return the value of the private members *
	//*******************************************
	int GetProductIDNumber( ) const;
	int GetManufacturerIDNumber( ) const;
	double GetWholesalePrice( ) const;
	double GetMarkUpPercentage( ) const;
	char* GetProductDescription( ) const;
	int GetQuantityInStock( ) const;
	double GetRetailPrice( ) const;
	Inventory* GetLink( ) const;

	//****************************************
	//* Set the value of the private members *
	//****************************************
	void SetProductIDNumber( int );
	void SetManufacturerIDNumber( int );
	void SetWholesalePrice( double );
	void SetMarkUpPercentage( double );
	void SetProductDescription( char [] );
	void SetQuantityInStock( int );
	void SetRetailPrice( double );
	void SetLink( Inventory* );

private:
	int ProductIDNumber;
	int ManufacturerIDNumber;
	int QuantityInStock;
	double WholesalePrice;
	double MarkUpPercentage;
	double RetailPrice;
	mutable char ProductDescription[25];
	Inventory* NextRecordPointer;
	int ItemCounter;
};

#endif

Here is the Input file--InventoryFileForInput.txt

10001 5000 15.00 .20 500 Count 24 Pound White Paper 050
10002 5020 05.00 .25 10 pack 3 1/2 Floppy Disks 125
10003 5005 35.50 .15 Black Ink Cartridge (HP 24) 023
10004 5001 02.00 .22 100 pack Black Ball Point Pen 160
10005 5001 07.00 .23 20 x 36 Desk Blotter 017




Last edited on
Topic archived. No new replies allowed.