problem writing out to a file using a structure

Hello All,

I am having a problem writing out an array of structures to a text file. I originally wrote this program with just an int array and it worked. i then changed the program to use structures and it has problems when writing out. the file gets created but nothing gets written to it. I know the output file gets created because when i check the program folder there is a text file with the specified name but empty. the file compiles just fine but when i run the program it jumps out after the output file is created and gives an exception to a function in the xlocale header. anyways my code is posted below, any help would be much appreciated.

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
// IO_Practice.cpp : This is a simple input/output program to practice using structures and fstream in c++
//

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

const int index = 6;

struct players
{
	string name;
	string pos;
	int number;
	int td;
	int catches;
	float passing;
	float receiving;
	float rushing;
};

void printFile(ofstream& outfile, players list[], int index);
void initializeArray(ifstream& infile, players list[], int index);

int main()
{
	ifstream infile;
	ofstream outfile;

	players seahawks[index];

	//int table[index + 1];

	string inputfile;
	string outputfile;

	cout << "Enter a name for the file to read from: " ;
	cin >> inputfile;
	cout << endl;

	infile.open(inputfile.c_str());

	if(!infile)
	{
		cout << "no such file" << endl;

		system("pause");

		return 1;
	}

	initializeArray(infile, seahawks, index);

	for(int i = 0; i <= index; i++)
	{
		cout << setw(10) << seahawks[i].name 
		    << setw(5) << seahawks[i].number 
			<< setw(5) << seahawks[i].pos
			<< setw(5) << seahawks[i].td
			<< setw(5) << seahawks[i].catches
			<< setw(5) << seahawks[i].receiving
			<< setw(5) << seahawks[i].rushing
			<< endl;
	}

	cout << "Enter a name for the file to read to: " ;
	cin >> outputfile;
	cout << endl;

	outfile.open(outputfile.c_str());

	printFile(outfile, seahawks, index);

	infile.close();
	infile.clear();
	outfile.clear();

	cout << "done" << endl;
	system("pause");

	return 0;
}

void printFile(ofstream& outfile, players list[], int index)
{
	for (int i = 0; i <= index; i++)
	{
		outfile << setw(10) << list[i].name << endl;
		outfile << setw(5) << list[i].number
			<< setw(5) << list[i].pos
			<< setw(5) << list[i].td
			<< setw(5) << list[i].catches
			<< setw(5) << list[i].receiving
			<< setw(5) << list[i].rushing;
	}
}

void initializeArray(ifstream& infile, players list[], int index)
{
	int i;

	for (i = 0; i <= index; i++)
	{
		infile >> list[i].name >> list[i].number >> list[i].pos;

	list[i].td = 0;
	list[i].catches = 0;
	list[i].receiving = 0.0;
	list[i].rushing = 0.0;
	list[i].passing = 0.0;
	}

}
Topic archived. No new replies allowed.