//Dana Yang //March 22, 2001 //Mr. Klix CS1 Period 7 //namegame.cpp //Program Description: This is a program sorts the name by the length and alphabet. #include //for general i/o #include //for apvectors #include //for getch and clrscr #include //for apstrings #include //for file i/o void get(apvector &names); void display(const apvector &names); void sort(apvector &names); void send(const apvector &names); apstring find(apvector &names); void last(apvector &names); int main() { apvector names(50); //to store the names apstring longest; //to asign the longest name to a variable cout<< "Welcome\n"; get(names); cout<< "Names collected:\n"; display(names); getch(); clrscr(); last(names); cout<< "Last names first:\n"; display(names); getch(); clrscr(); sort(names); cout<< "sorted names:\n"; display(names); cout<< "computer is sending names to file, press enter.\n"; getch(); send(names); clrscr(); longest = find(names); cout<< "The longest name is: "<< longest<< endl; cout<< "With a length of "<< (longest.length()-3)<< " letters."; getch(); clrscr(); return(0); } //******************************************************************************************* //start of function get() void get(apvector &names) { ifstream fin("a:names2.txt", ios::nocreate); if(fin.fail()) { cerr<< "file error - not opened"; exit(0); } int i = 0; //to help loop while(!fin.eof()) { getline(fin, names[i]); i++; } fin.close(); names.resize(i-1); return; } //end of function get() //***************************************************************************************** //***************************************************************************************** //start of function display() void display(const apvector &names) { for(int i = 0;i < names.length();i++) { cout<< names[i]<< " *** "; if((i+1)%3 == 0) cout<< endl; } cout<< endl; return; } //end of function display() //**************************************************************************************** //**************************************************************************************** //start of function sort() void sort(apvector &names) { apstring temp; //to switch names and sort for(int k = names.length()-1;k > 0;k--) for(int i = 0;i < k;i++) { if(names[i] > names[i+1]) { temp = names[i+1]; names[i+1] = names[i]; names[i] = temp; } } return; } //end of function sort() //**************************************************************************************** //**************************************************************************************** //start of function send() void send(const apvector &names) { ofstream fout("a:names2.txt",ios::nocreate); if(fout.fail()) { cout<< "Error opening output file - insert disk"; exit(0); } for(int i = 0;i < names.length();i++) fout<< names[i]<< endl; cerr<< "information sent"; getch(); clrscr(); fout.close(); return; } //end of function send() //*************************************************************************************** //*************************************************************************************** //start of function find() apstring find(apvector &names) { apstring temp; //to switch names for(int k = names.length()-1;k > 0;k--) for(int i = 0;i < k;i++) { if(names[i].length() < names[i+1].length()) { temp = names[i+1]; names[i+1] = names[i]; names[i] = temp; } } return(names[0]); } //end of function find() //****************************************************************************************** //****************************************************************************************** //start of function last() void last(apvector &names) { apstring edit; //to change from first to last int space; //finds position of space for(int i = 0;i < names.length();i++) { space = names[i].find(' '); edit = names[i].substr(space, (names[i].length()-space)); edit += ", "; edit += names[i].substr(0, space); names[i] = edit; } return; } //end of function last() //******************************************************************************************