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;
}
| |