Program Freezes when reading from file

My program keeps freezing at the readData(extra) function.
I've commented it out, and everything else is working the way I want it to (or at least I think it's working the way I want it to), but when I put the function call in, nothing happens.

I've posted my whole code, but separated the readData function.

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
void readData(info read[])
{
	ifstream file_in;
	file_in.open("talent.txt");

	if (file_in)
	{
		for (int j=0; j<EXTRASIZE; ++j)
		{
			file_in.get(read[j].name, SIZE, ':');
			file_in.ignore(100, ':');
			while (!file_in.eof())
			{
				file_in.get(read[j].age, SIZE, ':');
				file_in.ignore(100, ':');
				file_in.get(read[j].weight, SIZE, ':');
				file_in.ignore(100, ':');
				file_in.get(read[j].height, SIZE, ':');
				file_in.ignore(100, ':');
				file_in.get(read[j].talentDesc, SIZE, ':');
				file_in.ignore(100, ':');
				file_in.get(read[j].availability, SIZE, '\n');
				file_in.ignore(1000, '\n');
			}
			cout << endl << read[j].age << endl;
		}
	}
	else
	{
		cout << "Failed to access file.";
	}
	file_in.close();
}




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
#include "context.h"

int main()
{
	// Structure & local variables
	info extra[EXTRASIZE];
	int option = 0;		// Option that the user chooses to proceed with.
	int appCount = 0;	// Number of applicants signed up.
	
	cout << "\nHello! If you would like to be an extra on this show, please input your information below to get started.\n";

//	readData(extra);				// Reads in all data and places it in an array of structs.
	
	while (option >= 0 && option < 4)
	{		
		promptChoice(option);			// Asks user what they want to do with input.
	
		if (option == 1 && appCount<EXTRASIZE)
		{
			getData(extra,appCount);	// Takes all user's input and adds to existing array of structs.
			++appCount;
		}
		else if (option == 2)
		{
			searchData(extra, appCount);	// Search data for given talent.
		}
		else if (option == 3)
		{
			displayData(extra, appCount);	// Display names of all registered extras.
		}
	}

	writeData(extra);
	cout << "  \n\nGoodbye!\n\n";

	return 0;
}

// Prompts user for what they want to do.
// It also error checks to ensure proper input from user.
int promptChoice(int & option)
{
	cout << "\n1) Add new applicant." << 
		"\n2) Search for a particular talent." << 
		"\n3) Display all entires by name." <<
		"\n4) Quit the program." <<
		"\n\nChoose an option: ";
	cin >> option;
	cin.ignore(100, '\n');	
	// Input testing
	while (option != 1 && option != 2 && option != 3 && option != 4)
	{
		cin.ignore(100, '\n');
		cout << "Try again: ";
		cin >> option;
		cin.ignore(100, '\n');
	}

	return option;
}

// Function that gets all data to place in structure for the user.
void getData(info data[], int & i)
{	
	cout << "\nWhat is your name? ";
	cin.get(data[i].name, SIZE, '\n');
	cin.ignore(100, '\n');
	cout << "What is your age? ";
	cin.get(data[i].age, SIZE, '\n');
	cin.ignore(100, '\n');
	cout << "What is your weight? ";
	cin.get(data[i].weight, SIZE, '\n');
	cin.ignore(100, '\n');
	cout << "What is your height (ex: 5'2\")? ";
	cin.get(data[i].height, SIZE, '\n');
	cin.ignore(100, '\n');
	cout << "Give a very brief description of your talents: ";
	cin.get(data[i].talentDesc, SIZE, '\n');
	cin.ignore(100, '\n');
	cout << "What is your availability? ";
	cin.get(data[i].availability, SIZE, '\n');
	cin.ignore(100, '\n');
}

// Function to write's the data to a new text file.
// Also adds a delimeter of ":" to that
// it may be easily read and kept track of.
void writeData(info write[])
{
	ofstream file_out;
	file_out.open("talent.txt", ios::app);
	if (file_out)
	{
		for (int i=0; i<EXTRASIZE; ++i)
		{
			file_out << write[i].name << ":"
				 << write[i].age << ":"
				 << write[i].weight << ":"
				 << write[i].height << ":"
				 << write[i].talentDesc << ":"
				 << write[i].availability << '\n';
		}
	}
	file_out.close();
	file_out.clear();
}

// This function takes the external data file and reads in all data.
// It will create an array of structs, each element serving the purpose of holding
// its own line of data from the external file.
void readData(info read[])
{
	ifstream file_in;
	file_in.open("talent.txt");

	if (file_in)
	{
		for (int j=0; j<EXTRASIZE; ++j)
		{
			file_in.get(read[j].name, SIZE, ':');
			file_in.ignore(100, ':');
			while (!file_in.eof())
			{
				file_in.get(read[j].age, SIZE, ':');
				file_in.ignore(100, ':');
				file_in.get(read[j].weight, SIZE, ':');
				file_in.ignore(100, ':');
				file_in.get(read[j].height, SIZE, ':');
				file_in.ignore(100, ':');
				file_in.get(read[j].talentDesc, SIZE, ':');
				file_in.ignore(100, ':');
				file_in.get(read[j].availability, SIZE, '\n');
				file_in.ignore(1000, '\n');
			}
			cout << endl << read[j].age << endl;
		}
	}
	else
	{
		cout << "Failed to access file.";
	}
	file_in.close();
}

// Function that will search data on the external file for a specific talent.
// If the function finds someone with the talent, it will all who have it.
// Otherwise, the program will return that nobody was found with this particular talent.
void searchData(info search[], int & appCount)
{
	char searchTalent[SIZE];
	int talentCount = 0;

	cout << "\nWhat talent are you looking for? ";
	cin.get(searchTalent, SIZE, '\n');
	cin.ignore(100, '\n');

	for (int j=0; j < appCount; ++j)
	{
		if ((strcmp(searchTalent, search[j].talentDesc)) == 0)
		{
			cout << search[j].name << " has the talent you're looking for!\n";
			++talentCount;
		}
	}
	if (talentCount == 0)
	{
		cout << "Sorry, nobody with this talent has signed up yet.\n";
	}
}

// Function that displays all names of registered students read from the existing array of stucts in memory.
void displayData (info display[], int & appCount)
{
	for (int k=0; k<appCount; ++k)
	{
		cout << display[k].name << '\n';
	}
}
You need to show where info, SIZE and EXTRASIZE are defined.

Also, an example of the contents of the file "talent.txt" is needed.


However, a couple of things look likely to be the cause of problems.
One is that there is both a for loop and a while loop at lines 8 and 12. One or other of these might work, but both combined is probably going to repeat too many times. Also using eof in a while loop is usually not to be advised.
Topic archived. No new replies allowed.