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
|
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct persons{
string first_name;
string last_name;
};
void openfile(persons student[], int& item,ifstream&file,ofstream& outfile);
void swappersons(persons& name1, persons& name2);
void sortbyfirstname(persons student[], int& item);
void output(persons student[], int& item,ofstream&outfile);
void closefile(ifstream& file,ofstream&outfile);
void sortbylastname(persons student[],int& item);
//void addcontact(persons& tmp,persons& student[],int& item,ofstream& outfile);
int main(){
int item,choice;
persons student[1000],tmp;
ifstream file;
ofstream outfile;
string info;
openfile(student, item,file,outfile);
while(choice!=4){
cout<<"MAIN MENU- please select an option"<<endl;
cout<<"select 1 to sort names by first name"<<endl;
cout<<"select 2 to sort names by last name"<<endl;
cout<<"select 3 to add a name"<<endl;
cout<<"select 4 to end program"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"*****************************"<<endl;
sortbyfirstname(student,item);
output(student, item,outfile);
cout<<endl;
break;
case 2:
cout<<"*****************************"<<endl;
sortbylastname(student,item);output(student, item,outfile);
cout<<endl;
break;
case 3:
//addcontact(tmp,student,item,outfile);
cout<<"cant do it yet"<<endl;
break;
case 4:
cout<<"program has ended"<<endl;
return 0;
default:
cout << "Invalid input, Terminating Program" << endl;
break;
}
closefile(file,outfile);
}
}
void openfile(persons student[], int& item,ifstream&file,ofstream& outfile)
{
file.open("students");
if (!file)
{
cout << "file open error" <<endl;
}
item = 0;
while (file >> student[item].first_name >> student[item].last_name)
{
item++;
}
}
//void read_infile(ifstream&file,int&item,persons student[]){
/*void addcontact(persons& tmp,persons& student[],int& item,ofstream& outfile){
cout<<"enter first name"<<endl;
cin>>tmp.first_name;
for(int i=0;i<item+1;i++){
outfile<<getline(cin,tmp.first_name);
item++;}
cout<<"enter last name"<<endl;
cin>>tmp.last_name;
for(int i=0;i<item+1;i++){
outfile<<getline(cin,tmp.last_name);
item++;}
}
*/
void swappersons(persons& name1, persons& name2){
persons temp;
temp=name1;
name1=name2;
name2=temp;
}
void sortbyfirstname(persons student[], int& item){
for(int a=0; a<item-1; a++)
{
for(int i=0; i<item-1; i++)
if(student[i].first_name > student[i+1].first_name)
swappersons(student[i],student[i+1]);
}
}
void sortbylastname(persons student[],int& item){
for(int a=0; a<item-1; a++)
{
for (int i=0; i<item-1; i++)
if(student[i].last_name>student[i+1].last_name)
swappersons(student[i],student[i+1]);
}
}
void output(persons student[], int& item,ofstream&outfile){
outfile.open("studentstmp");
for(int i=0; i<item; i++){
outfile<< student[i].first_name<<" "<<student[i].last_name<<" "<<endl;
cout<< student[i].first_name<<" "<<student[i].last_name<<" "<<endl;}
outfile.close();
}
void closefile(ifstream& file,ofstream&outfile){
file.close();
//outfile.close();
cout<<"********************************"<<endl;
}
|