C++ Code

//This program was created by Tiffany Overstreet on May 24, 2000 for AP Computer
//Science. It will use a struct to organize a record of information. It then
//sorts through the records and outputs the information. This particular program
//will sort through an employee data file and sort by city.

#include <fstream.h>
#include <iostream.h>
#include <apstring.h>
#include <assert.h>

struct employee_info
{
apstring name, street, city, state, zip_code;
int age;
long int salary;
};

int open_input_file(ifstream &in_file);
//This function will open the file.

int file_into_struct(employee_info list[], ifstream &in_file);
//This function will read the file and put the data into the struct.

int bubble_sort(employee_info list[]);
//This function will sort through the data and re-arrange the data
//to the sort specifications.


int main()
{
ifstream in_file;
employee_info list[3];

cout<<"This program was written by Tiffany Overstreet on May 24, 2000."<<endl;
cout<<"It will sort through a list of employee information and will sort the"<<endl;
cout<<"employees by city."<<endl;

open_input_file(in_file);
file_into_struct(list, in_file);
bubble_sort(list);


return 0;
}

open_input_file(ifstream &in_file)
{
apstring in_file_name;
in_file_name="a:\Employe2.txt";
in_file.open(in_file_name.c_str());
assert(! in_file.fail());

return 0;
}


int file_into_struct(employee_info list[], ifstream &in_file)
{
int data;
long int data2;
apstring data3;


for (int x=0; x<3; x++)
{
getline(in_file, list[x].name);
//cout<<list[x].name<<endl;
getline(in_file, list[x].street);
//cout<<list[x].street<<endl;
getline(in_file, list[x].city);
//cout<<list[x].city<<endl;
getline(in_file, list[x].state);
//cout<<list[x].state<<endl;
getline(in_file, list[x].zip_code);
//cout<<list[x].zip_code<<endl;
in_file>>data;
list[x].age=data;
//cout<<list[x].age<<endl;
in_file>>data2;
list[x].salary=data2;
//cout<<list[x].salary<<endl;
getline(in_file, data3);

cout<<endl;
}
in_file.close();

return 0;
}

int bubble_sort(employee_info list[])
{
char in_order;
int number=3;
int num=0;
apstring temp;
int temp_int;
long int temp_long_int;

in_order='n';

while (in_order=='n')
{
in_order='y';
for (num=0; num<number-1; num++)
{
if (list[num].city>list[num+1].city)
{
temp=list[num].city;
list[num].city=list[num+1].city;
list[num+1].city=temp;

//name
temp=list[num].name;
list[num].name=list[num+1].name;
list[num+1].name=temp;

//street
temp=list[num].street;
list[num].street=list[num+1].street;
list[num+1].street=temp;

//state
temp=list[num].state;
list[num].state=list[num+1].state;
list[num+1].state=temp;

//zip_code
temp=list[num].zip_code;
list[num].zip_code=list[num+1].zip_code;
list[num+1].zip_code=temp;

//age
temp_int=list[num].age;
list[num].age=list[num+1].age;
list[num+1].age=temp_int;

//salary
temp_long_int=list[num].salary;
list[num].salary=list[num+1].salary;
list[num+1].salary=temp_long_int;

in_order='n';
}
}
number=number-1;
}

cout<<"Here's the list sorted by city:"<<endl;
for (int y=0; y<3; y++)
{
cout<<list[y].name<<endl;
cout<<list[y].street<<endl;
cout<<list[y].city<<endl;
cout<<list[y].state<<endl;
cout<<list[y].zip_code<<endl;
cout<<list[y].age<<endl;
cout<<list[y].salary<<endl<<endl;
}


return 0;
}

Main Struct Page    Sample Output for the Program
Go back to the main page