populate note using text file

my last post was getting a little long, so I will repost a new question here.

For some reason, the insertFromFile() function does not work. I can insert it manually, but when I modify it to work with the file, it doesn't work for some reason.

Can someone please help me with this.

I found a nice tutorial on youtube from Mr rochele.

main:
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
	#include <string>
	#include <iostream>
	#include <fstream>
	#include <cstdlib>
	#include <sstream>
	#include "Assignment4.h"
	
	using namespace std;
	
 //******************************************
 void print_list(Personnel *in_root)
 //=======================================
 // PRINT LIST CONTENTS
 //=======================================
 {
 	Personnel *next_ptr;
 	
 	next_ptr = in_root;
 	
 	if(next_ptr == NULL)
 	{
 		cout << "EMPTY LIST: No nodes to print...." << endl;
	}//end if
	else
	{
		while(next_ptr != NULL)
		{
			cout << next_ptr -> firstName << endl;
			cout << next_ptr -> lastName << endl;
			cout << next_ptr -> jobTitle << endl;
			cout << next_ptr -> empStatus << endl;
			cout << next_ptr -> wage << endl;
			next_ptr = next_ptr -> next;
		}//end while
	}//end else
	system("pause");
 }//end print_list
 
 //=====================Add node by manual input.
 Personnel *add_node(Personnel *in_root)
 //=========================================
 // ADD A NEW NODE
 //=========================================
 {
 	Personnel *next_ptr = NULL;
 	Personnel *prev_ptr = NULL;
 	
 	if(in_root == NULL)
 	{
 		// list is empty
 		if((in_root = new Personnel) != NULL)
 		{
 			next_ptr = in_root;
 			
 			cout << "First Name: ";
 			cin >> next_ptr -> firstName;
 			cout << "Last Name: ";
 			cin >> next_ptr -> lastName;
 			cout << "Job Title: ";
 			cin >> next_ptr -> jobTitle;
 			cout << "Employments Status <F>ull or <P>art time: ";
 			cin >> next_ptr -> empStatus;
 			cout << "Wage: ";
 			cin >> next_ptr -> wage;
 			 			
 			next_ptr -> next = NULL;
 		}//end if
 		else
 		{
 			cout << "ERROR: Unable to allocate memory." << endl;
 			return NULL;
		}//end else
 		return next_ptr;
	}//end if
 	else
 	{
 		//list has members
 		next_ptr = in_root;
 		while(next_ptr -> next != NULL)
 		{
 			next_ptr = next_ptr -> next;
 	    }//end while
 	    prev_ptr = next_ptr;
 	    if((next_ptr = new Personnel) != NULL)
 	    {
 	    	prev_ptr -> next = next_ptr;
 			cout << "First Name: ";
 			cin >> next_ptr -> firstName;
 			cout << "Last Name: ";
 			cin >> next_ptr -> lastName;
 			cout << "Job Title: ";
 			cin >> next_ptr -> jobTitle;
 			cout << "Employments Status <F>ull or <P>art time: ";
 			cin >> next_ptr -> empStatus;
 			cout << "Wage: ";
 			cin >> next_ptr -> wage;
 			
 			next_ptr -> next = NULL;
		}//end if
		else
		{
			cout << "ERROR: Unable to allocate memory." << endl;
		}//end else
		return in_root;
 	}//end else
 };//end Personnel
 
 //**********************
 //GetFileName
 string getFileName()
 {
 string fileName;	
 cout << "Enter Name of file to open INCLUDING extension ::" << endl;
 cin >> fileName;
 //================test to make sure the fileName name has a txt extension
 string ext="";
for(int i = fileName.length()-1;i>fileName.length()-5;i--)
{
    ext += fileName[i];
}
//cout<<ext;
if(ext != "txt.")
 {
    cout<<"File name does not contain the proper extension, Please Enter Name of file to open INCLUDING extension :: " <<endl;
    cin >> fileName;
}//end if
 return fileName;
 }//end getFileName
 
 //=================add node from text file
 string insertFromFile (Personnel *in_root)
 {
	Personnel *next_ptr = NULL;
	Personnel *prev_ptr = NULL;
	string firstName, lastName, jobTitle, fileName;
	char empStatus;
	double wage;
	//int counter = 0;
	ifstream file;
	fileName = getFileName();
	file.open(fileName.c_str());
	
	if(!file.is_open())
    {
       exit(EXIT_FAILURE);
       cout << "File did not open." << endl;
    }// end if

	file >> firstName >> lastName >> jobTitle >> empStatus >> wage;
	//cout << firstName << lastName << jobTitle << empStatus << wage << endl;
	
////////====================================
 while(file)
     {
    	
 	
 	if(in_root == NULL)
 	{
 		// list is empty
 		if((in_root = new Personnel) != NULL)
 		{
 			next_ptr = in_root;
 			next_ptr -> firstName;
			next_ptr -> lastName;
			next_ptr -> jobTitle;
			next_ptr -> empStatus;
			next_ptr -> wage;

 			next_ptr -> next = NULL;
 		}//end if
 		else
 		{
 			cout << "ERROR: Unable to allocate memory." << endl;
 			return NULL;
		}//end else
 		return next_ptr;
	}//end if
 	else
 	{
 		//list has members
 		next_ptr = in_root;
 		while(next_ptr -> next != NULL)
 		{
 			next_ptr = next_ptr -> next;
 	    }//end while
 	    prev_ptr = next_ptr;
 	    if((next_ptr = new Personnel) != NULL)
 	    {
 	    	prev_ptr -> next = next_ptr;
            next_ptr -> firstName;
			next_ptr -> lastName;
			next_ptr -> jobTitle;
			next_ptr -> empStatus;
			next_ptr -> wage;
 			
 			next_ptr -> next = NULL;
		}//end if
		else
		{
			cout << "ERROR: Unable to allocate memory." << endl;
		}//end else
		return in_root;
 	}//end else
 }//end while
 
	}//end insertFromFile
 //=====================getUserInfo=====
 void getUserInfo(){
 	string *firstName, *lastName, *jobTitle;
 	char *empStatus;
 	double *wage;
 	cout << "Enter FIRST name of associate :" << endl;
 	cin >> *firstName;
 	cout << "Enter LAST name of associate :" << endl;
 	cin >> *lastName;
 	cout << "Enter job title of associate :" << endl;
 	cin >> *jobTitle;
 	
 	    while (*empStatus != 'P' && *empStatus != 'p' && *empStatus != 'F' && *empStatus != 'f')
 	    {
 	    	cout << "Is associate <F>ull OR <P>art time ?" << endl;
 	cin >> *empStatus;
		 }//end while
    cout << "Enter hourly wage :" << endl;
    cin >> *wage;
 }//end getUserInput
 //============menu======	
 void menu()
 {
 	Personnel *head = NULL;
	char choice;
 	bool quit = false;
	while (!quit)
      {
 	cout << "*****MENU*****" << endl;
 	cout << "1. Add items from Text File" << endl;
 	cout << "2. Manually Add Item" << endl;
 	cout << "3. Print ALL associates" << endl;
 	cout << "4. Search and Print" << endl;
 	cout << "5. EXIT" << endl;
 	 cout << "Enter a menu choice: " << endl;
        cin >> choice;
        cout << endl;
 	 	
 		switch (choice)
 		{
 			case '1': insertFromFile(head);
			 break;
			 
			case '2': head = add_node(head);
			 break;
			 
			case '3': print_list(head);
			 break;
			 
			case '4': cout << "Function to search for and print an associate to screen" << endl;
			 break;
			 
			case '5': cout << "EXIT" << endl; 
			 quit = true;
			 break;  
		}//end switch
	}//end while
 }//end menu
	
 int main()
 { 
 //-----------variables
	menu();
	system("pause");
	return 0;
 }//end main  

header:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using namespace std;
 
 struct Personnel {

 string firstName;
 string lastName;
 string jobTitle;
 char empStatus;
 double wage;
 //cur_person head
 Personnel *next;
// Personnel *head;
// Personnel *cur_person;
 	
 };// end struct
 
 Personnel *add_node (Personnel *in_root);
 void print_list (Personnel *in_root);
 string fileOpen();
 string insertFromFile (Personnel *in_root);
 void menu();
closed account (48T7M4Gy)
The reason is your code for file source should be analogous to the manual method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if((in_root = new Personnel) != NULL)
 		{
 			next_ptr = in_root;
 			
 			cout << "First Name: ";
 			cin >> next_ptr -> firstName;
 			cout << "Last Name: ";
 			cin >> next_ptr -> lastName;
 			cout << "Job Title: ";
 			cin >> next_ptr -> jobTitle;
 			cout << "Employments Status <F>ull or <P>art time: ";
 			cin >> next_ptr -> empStatus;
 			cout << "Wage: ";
 			cin >> next_ptr -> wage;
 			 			
 			next_ptr -> next = NULL;
 		}//end if 


What that means is you should be writing file >> next_ptr -> firstName etc by the look of it.
Last edited on
Ok, so by writing it like this:
1
2
3
file >> next_ptr -> firstName; 
file >> next_ptr -> lastName ;
etc....


the ">>" know where it has left off in the text file?
ie..., it knows that when called again, it picks up from the last whitespace it encountered and continues like that til eof??
closed account (48T7M4Gy)
the ">>" know where it has left off in the text file?

Yep, there is an internal pointer keeping track.

This tutorial might help you too. http://www.cplusplus.com/doc/tutorial/files/ but you don't have to read the file a complete line at a time the way the demos show.

Keep in mind if your file date is separated by blank spaces and it is in sync with the read-in sequence then it will all work. If you print it out as you go you can verify everything is going to plan.

PS you keep reading the file while( fin >> next_ptr -> firstName)
Hey,
Thanks a lot. I will give this a try when I get home this evening.

Appreciate the help!
Ok, so with the help from dhayden, I have learned quite a bit about linked lists. For some reason, I cant get this to compile. I get an error stating that on line 141, list is not declared in this scope. It is defined it in the "List" struct. I have tried moving the function up and down in the code, tried declaring it in every possible way. I am out of ideas

Main:
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
	#include <string>
	#include <iostream>
	#include <fstream>
	#include <cstdlib>
	#include <sstream>
	#include "Assignment4a.h"
	
		void List::insertFromFile(istream &is)
	{
	    Personnel *node = new Personnel;
	    while (node->read(is)) {
		add(node);
		node = new Personnel;
	    }
	    delete node;		// couldn't read it. Delete it now.
	}						 //end insertFromFile

	// read from an open file, or any stream
	bool Personnel::read(istream &is)
	{
	    is >> firstName >> lastName >> jobTitle >> empStatus >> wage;
	    return is.good();
	}
	
	
	// print to open file or any stream
	void Personnel::print(ostream &os)
	{
	    os << firstName << endl;
	    os << lastName << endl;
	    os << jobTitle << endl;
	    os << empStatus << endl;
	    os << wage << endl;
	}
	
	
	// prompt and read from user on cin
	bool Personnel::fromUser()
	{
	    cout << "Enter person's data:" << endl;
	    cout << "First Name: ";
	    cin >> firstName;
	    cout << "Last Name: ";
	    cin >> lastName;
	    cout << "Job Title: ";
	    cin >> jobTitle;
	    cout << "Employments Status <F>ull or <P>art time: ";
	    cin >> empStatus;
	    cout << "Wage: ";
	    cin >> wage;
	    return cin.good();
	}
	
	void
	List::print(ostream &os)
	 //=======================================
	 // PRINT LIST CONTENTS
	 //=======================================
	{
	    Personnel *next_ptr;
	
	    next_ptr = root;
	
	    if (next_ptr == NULL) {
		os << "EMPTY LIST: No nodes to print...." << endl;
	    }						 //end if
	    else {
		while (next_ptr != NULL) {
		    next_ptr->print(os);  // use the print() method that we defined above
		    next_ptr = next_ptr->next;
		}					 //end while
	    }						 //end else
	}						 //end print_list
	
	 //=====================Add node by manual input.
	void List::add(Personnel * node)
	 //=========================================
	 // ADD A NEW NODE
	 //=========================================
	{
	    node->next = root;
	    root = node;
	};						 //end Personnel
	
	List::~List()
	{
	    // ADD CODE to delete all items in the list here.
	}
	

	
	 //GetFileName
 string getFileName()
 {
 string fileName;	
 cout << "Enter Name of file to open INCLUDING extension ::" << endl;
 cin >> fileName;
 //================test to make sure the fileName name has a txt extension
 string ext="";
for(int i = fileName.length()-1;i>fileName.length()-5;i--)
{
    ext += fileName[i];
}
//cout<<ext;
if(ext != "txt.")
 {
    cout<<"File name does not contain the proper extension, Please Enter Name of file to open INCLUDING extension :: " <<endl;
    cin >> fileName;
}//end if
 return fileName;
 }//end getFileName 
	
    void menu()
    {
 	//Personnel *head = NULL;
	char choice;
 	bool quit = false;
	while (!quit)
      {
 	cout << "*****MENU*****" << endl;
 	cout << "1. Add items from Text File" << endl;
 	cout << "2. Manually Add Item" << endl;
 	cout << "3. Print ALL associates" << endl;
 	cout << "4. Search and Print" << endl;
 	cout << "5. EXIT" << endl;
 	 cout << "Enter a menu choice: " << endl;
        cin >> choice;
        cout << endl;
        
  		switch (choice)
 		{
 			case '1':  {
		                 string fileName = getFileName();
		                 ifstream file;
		                 file.open(fileName.c_str());
		                 if (!file.is_open()) 
						 {
		                     cout << "File did not open." << endl;
		                     exit(EXIT_FAILURE);
		                 }// end if
		list.insertFromFile(file);
	                   }//end case 1

			 break;
			 
			case '2': cout << "Manually add items" << endl;
			 break;
			 
			case '3': cout << "Print ALL items" << endl;
			 break;
			 
			case '4': cout << "Function to search for and print an associate to screen" << endl;
			 break;
			 
			case '5': cout << "EXIT" << endl; 
			 quit = true;
			 break;  
		}//end switch
 	 	
// 		switch (choice)
// 		{
// 			case '1': head = insertFromFile(head);
//			 break;
//			 
//			case '2': head = add_node(head);
//			 break;
//			 
//			case '3': print_list(head);
//			 break;
//			 
//			case '4': cout << "Function to search for and print an associate to screen" << endl;
//			 break;
//			 
//			case '5': cout << "EXIT" << endl; 
//			 quit = true;
//			 break;  
//		}//end switch
	}//end while
 }//end menu

int main()
 { 
 //-----------variables
	menu();
	system("pause");
	return 0;
 }//end main 

Header
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

 using namespace std;

struct Personnel
{
    Personnel() : next(NULL) {}  // always initialize next to NULL
    string firstName;
    string lastName;
    string jobTitle;
    char empStatus;
    double wage;
    Personnel *next;

    bool read(istream &is);	// read from an open file, or any stream
    void print(ostream &os);	// print to open file or any stream
    bool fromUser();		// prompt and read from user on cin
};


struct List {
    List() : root(NULL) {}

    // Delete all items in the list
    ~List();

    // Add a node to the front of the list. node MUST be allocated
    // via new. The List will be responsible for deleting it.
    void add(Personnel *node);
    void insertFromFile(istream &is);

    // Print the list to the stream
    void print(ostream &os);
private:
    Personnel *root;		// head of the list
};
Topic archived. No new replies allowed.