Problem with Reading File into Array Structure

Im trying to write a code that inputs data from a file into an array of structures, sorts the data alphabetically by last name and then format the data into a report. I included how much code I've written so far. Im not sure if the data file is being read correctly because my output so far only reads the last and first name on the first line and a bunch of number repeating. Could someone please take a look?

This is my data file....

Smith Tom 03141989 59150.00 B 253-760-5811
Hall Debra 05061980 61500.00 C 253-850-6880
Gaines Sarah 11241958 65500.00 d 397-196-1420
Ritz Robert 03301970 80250.00 a 385-300-4068
Martin Kevin 07151990 75000.00 A 397-123-2410
Holloway Jason 12181985 72750.00 a 385-697-9713
Hoan Kimberly 05161991 53125.00 b 253-156-5195
Bryan Victor 01231989 41150.00 D 407-701-7022
Roberts Ben 01121989 55000.00 c 253-519-1818
Little Mary 01181991 40685.00 d 860-714-2039
Samson Rachel 02021981 70515.00 A 305-312-0600
Peach Mel 10301965 60016.00 c 397-142-4061
Carrs Brett 04261969 56250.00 B 253-804-2000
Walsh Edward 09211993 49080.00 d 204-241-3033
Austin Jacob 08131992 48130.00 D 385-402-5741

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
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <iomanip>

using namespace std;

//structure declaration
struct stu_data
{
	char last_name[15];
	char first_name[15];
	int b_month;
	int b_day;
	int b_year;
	float annual_salary;
	char dept_code;
	char phone_number[12];
};

int main()
{
	stu_data stu_rec[15];

	//open file and input data
	ifstream infile;
	infile.open("sampledata.txt");

	int i;
	for (i = 0; i < 15; i++)
	{
		infile.getline(stu_rec[i].last_name, 15, '\n');
		infile.getline(stu_rec[i].first_name, 15, '\n');
		infile >> stu_rec[i].b_month >> stu_rec[i].b_day >> stu_rec[i].b_year 
			>> stu_rec[i].annual_salary >> stu_rec[i].dept_code >> stu_rec[i].phone_number;
		stu_rec[i].dept_code = toupper(stu_rec[i].dept_code);
	}


	//quick display to know if data is being read
	for (i = 0; i < 15; i++)
	{
		cout << stu_rec[i].last_name << stu_rec[i].first_name << stu_rec[i].b_month
			<< stu_rec[i].b_day << stu_rec[i].b_year << stu_rec[i].annual_salary
			<< stu_rec[i].dept_code << stu_rec[i].phone_number; 
		
	}
	//sort array by last name


	//close file
	infile.close();
	return 0;
}
Hint: Take a look at lines 33 and 34 and look at the format of your information in the file. Are the last_name and first_name terminated by newlines?
That output looks a little freaky!

OK, so after minimalizing the input and output, namely limiting it to reading and outputting the last name and first name, I get:
Smith Tom 0314

After taking out the first name, I got the same output. So the error is in the use of getline to read in the names. It is basically reading in the first 14 characters for the last name, and it just flips out from there.

I recommend simply changing your delimiter in the getline statement to a white space ' '. So just use:
infile.getline(stu_rec[i].last_name, 15, ' ');

etc.

OH, and if you use this method, you need to also have spaces to separate the month, day, and year in your file. If you have no control over this somehow, then your method of getting input needs to be different just for that part. Finally, using this method will also not give you leading zeroes for ints, so you may want to consider reading them as strings or something if you need to keep that format.
Last edited on
Your sampledata.txt only seems to have six items but your structure has eight?
This seems to load your sampledata.txt example.
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
#include <iostream>
#include <fstream>
//#include <algorithm>
//#include <string>
//#include <iomanip>

using namespace std;

//structure declaration
//Smith Tom 03141989 59150.00 B 253-760-5811  only six items?
struct stu_data
{
	char last_name[15];
	char first_name[15];
	int  number;
	float annual_salary;
	char dept_code;
	char phone_number[12];
};


#include <iostream>
#include <fstream>
#include <ostream>
#include <string>

using namespace std;


//Function Prototypes

//Main function
int main()
{
  stu_data stu_rec[15];
  string end_of_file = "eof";

  ifstream inFile;
  ofstream outFile;

  inFile.open("sampledata.txt");
  outFile.open("employees");

  if (inFile.fail())
    {
        cout << "Unable to open sampleData.txt file\n";
        return 0;
    }

   //Start read of data from file
   int recordCount = 0;

    while (!inFile.eof())
   {
     inFile >> stu_rec[recordCount].last_name;
     inFile >> stu_rec[recordCount].first_name;
     inFile >> stu_rec[recordCount].number;
     inFile >> stu_rec[recordCount].annual_salary;
     inFile >> stu_rec[recordCount].dept_code;
     inFile >> stu_rec[recordCount].phone_number;
     cout << stu_rec[recordCount].last_name << endl;
     cout << stu_rec[recordCount].first_name << endl;
     cout << stu_rec[recordCount].number << endl;
     cout << stu_rec[recordCount].annual_salary << endl;
     cout << stu_rec[recordCount].dept_code << endl;
     cout << stu_rec[recordCount].phone_number << endl << endl;
     recordCount++;

     //outFile << "\n" << personInfo.last_name << " ";
     //outFile << personInfo.first_name << endl;
     //outFile << personInfo.salary << endl;
     //outFile << personInfo.ss_num << endl;
     //outFile << personInfo.years_employed << endl;

     }

    inFile.close();
    outFile.close();

return 0;
}

Last edited on
Topic archived. No new replies allowed.