Searching text file with prefix

I am writing a program that reads in a text file that has a name of something, and a weight associated with that name and and stores it in a linked list and sorts it based on the weight and by the name in it. That part is working but I also need to create a function that sorts thru the file and finds every word that starts with a prefix that is entered by the user. For example if the user enters "Am" the function will search the file and return every word that starts with "Am". Anyways I have never done anything like this before and so any help on how to do this would be greatly appreciated!

Here is the Program thus far...
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <list>
using namespace std;

//declare node structure
struct node
{
	//declare string text
	string text;
	//string name;
	int number;
	//declare next and prev node
	struct node *next;
	struct node *prev;
}*start; //start or head of linked list


//delcare class LinkedList
class Term
{
	//public part
public:
	//declare all fucntions
	//this will create linked list
	void CreateList(string file);
	//insert a line at end of list
	void InsertAtEnd(int num,string file, int size);
	void print(string file_name);
	void sort_weight ();
	void sort_query();
	//void SelectionSort();
	//void swap(node *p1, node*p2);

	//private
private:
	//declare head and tail
	node *head;
	node *tail;
};


Functions and 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
#include "Term.h"

void Term :: CreateList(string file)
{
	//delclare node temp.
	node *temp;
	//initialize temp
	temp = new(struct node); 
	//set text(which was declared in class) to file.
	temp -> text = file;
	temp -> next = NULL;

	//If start of list equals NULL enter statement.
	if (start == NULL)
	{
		temp -> prev = NULL;
		//set beginning of list to temp.
		start = temp;
	}
	//if not enter else statement.
	else
	{
		//enter loop if the term after start is not NULL.
		while (start -> next != NULL)
		{
			//set start equal to line proceeding it.
			start = start -> next;
			//set that line equal to temp.
			start -> next = temp;
			//set previous line equal to start.
			temp -> prev = start;
		}
	}
	//End function and return too main.
}
//Creat function that insert something at end of file taking parameters file(content of file), and size(number of lines).
//This function will enter lines into list but reorder them since orginally it insert backwards and finally it will insert new line.
void Term :: InsertAtEnd(int num, string file, int size)
{
	//Declare nodes temp and temp_2.
	struct node *temp, *temp_2;
	//set temp_2 to beginning of list(start).
	temp_2 = start;

	//if you have not reached the end of the file enter loop and do not exit till you reach end of file.
	for (int i = 0; i < size; i++)
	{
		//progress through loop.
		temp_2 = temp_2 -> next;
	}
	//initialize temp.
	temp = new(node);
	temp -> text = file;
	temp -> number = num;

	//if line following q equals NULL enter
	if (temp_2 -> next == NULL)
	{
		//progress q and set equal to tmp.
		temp_2 -> next = temp;
		//set line following tmp to NULL.
		temp -> next = NULL;
		//set line Proceeding tmp to temp_2.
		temp -> prev = temp_2;      
	}

	//if not enter if statement
	else
	{
		//set line following tmp equal to line following temp_2.
		temp -> next = temp_2 -> next;
		temp -> next -> prev = temp;
		temp_2 -> next = temp;
		temp -> prev = temp_2;
	}
}


void Term :: sort_weight ()
{
	node * temphead = start;
	//ListNode * tempnode = NULL;
	int temproll;
	string tempname;
	int counter = 0;
	while (temphead )
	{
		temphead = temphead->next;
		counter++;
	}
	temphead = start -> next;
	
	for (int j=0; j<counter; j++)
	{
		while (temphead->next)  //iterate through list until next is null
		{
			if (temphead->number < temphead->next->number)
			{
				/*tempnode = temphead;
				temphead = temphead->next;
				temphead->next = tempnode;*/
				temproll = temphead->number;
				temphead->number = temphead->next->number;
				temphead->next->number = temproll;

				tempname = temphead->text;
				temphead->text = temphead->next->text;
				temphead->next->text = tempname;
				temphead = temphead->next;//increment node
			}
			else 
				temphead = temphead->next;//increment node
		}	
		temphead = start;//reset temphead
	}
}

void Term :: sort_query ()
{
	node * temphead = start;
	//ListNode * tempnode = NULL;
	string temp;
	int temp_2;
	int counter = 0;
	while (temphead)
	{
		temphead = temphead->next;
		counter++;
	}
	temphead = start -> next;
	
	for (int j=0; j<counter; j++)
	{
		while (temphead->next)  //iterate through list until next is null
		{
			if (temphead->text > temphead->next->text)
			{
				/*tempnode = temphead;
				temphead = temphead->next;
				temphead->next = tempnode;*/
				temp = temphead->text;
				temphead->text = temphead->next->text;
				temphead->next->text = temp;

				temp_2 = temphead->number;
				temphead->number = temphead->next->number;
				temphead->next->number = temp_2;
				temphead = temphead->next;//increment node
			}
			else 
				temphead = temphead->next;//increment node
		}	
		temphead = start;//reset temphead
	}
}
//Declare void function(does not return anything) that will print the current linked list to the screen when requested
//or output it to the input file when requested.  It takes parameters letter(letter entered when prompted in main function) and file_name
//(name of file).
void Term :: print(string file_name)
{
	//declare and initialize line_number.
	int line_number = 1;
	//declare node temp_2
	node *temp_2;
	//set equal to start or beginning of list
	temp_2 = start;

	//if first line is equal to blank space enter statement
	if( temp_2->text == "")
	{
		//proceed to followinf line skipping over this plan line
		temp_2 = temp_2-> next;
	}

	//enter loop if temp_2 != null (have not reached end of file)
	while (temp_2 != NULL && temp_2-> text != "")
	{
		//output line number and each line in the list till you reach the end
		cout << temp_2 -> number << " " << temp_2 -> text << endl;
		//progress to next line
		temp_2 = temp_2 -> next;
		//increment line number
		line_number += 1;
	}
	
}

int main()
{
	Term txt_file;
	string file;
	int num;
	string file_name;
	ifstream infile;
	int size = 0;
	int largest_pos = 0;
	//call function to create list
	txt_file.CreateList(file);
	
	//ask user for file to open and store in variable
	cout << "What file would you like to open: ";
	getline(cin, file_name);
	infile.open(file_name);
	
	//check if file name is valid
	if(infile.fail())
	{
		//if not display error message and end program
		cout <<"The file cannot be opened, terminating program!" << endl;
		system("pause");
		exit(0);
	}

	//use loop to read through file
	while(infile >> num && (getline(infile, file)))
	{

		//call function to insert each line in file into linked list one following the other
		txt_file.InsertAtEnd(num,file, size);
		//increase size
		size += 1;
		//increase largest_pos
		largest_pos += 1;
	}

	txt_file.sort_weight();
	txt_file.print(file_name);
	cout << endl << endl << endl;
	txt_file.sort_query();

	//call function to print current file to screen
	txt_file.print(file_name);
	system("pause");
	return 0;
}


Here is a snippet from one of the text files:
1
2
3
4
5
6
7
8
9
10
11
4551	Pinnacle West Capital
7018	Automatic Data Proc.
8969	Continental Airlines
5725	PPL
1193	Goody's Family Clothing
1694	Community Health Sys.
7875	Cenex Harvest States
5723	Air Products & Chem.
2238	Consol. Freightways
31138	Goldman Sachs Group
2352	PNM Resources 



Topic archived. No new replies allowed.