Thread: help

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    1

    Post help

    How can i do this programm??

    Make a project called proj9_sortAddress, and add a new C++ source file called sortAddress.cpp. Including any source code from your earlier projects you feel to be useful, write a program which:

    Loads in your address book from file into an array of people.
    Performs a bubble sort, to sort this array into alphabetical order
    Saves your sorted address book into a new text file.

    Code:
    #include<iostream.h>
    #include<string.h>
    #include<fstream.h>
    #include<stdlib.h>
    
    
    class Person
    {
    private:
    	int age;
    	char first_name[10];
    	char second_name[10];
    	char phone_number[20];
    	
    public:
    	
    	bool set_full_name(char fn[],char sn[]);
    	void get_full_name(char full_name[]);
    	void get_phone_number(char into_here[]);
    	bool set_phone_number(char from_here[]);
    	void set_age(int a);
    	int get_age();
    	
    	void print();
    	void input(char pn[],char fn[],char sn[]);
    	
    	bool load(ifstream &fin);
    	void print2(ofstream &fout);
    	//void save(ofstream &fout);
    	void copy(Person & copyFromMe);
    	
    };
    
    
    void Person::set_age(int a)
    {
    	age = a;
    }
    
    int Person::get_age()
    {
    	return age;
    }
    
    bool Person::set_full_name(char fn[],char sn[])
    {
    	int n=0;
    	first_name[0]='\0';
    	second_name[0]='\0';
    	
    	if(strlen(fn)<10) strcpy(first_name,fn);
    	else return false;
    	
    	if(strlen(sn)<10) strcpy(second_name,sn);
    	else return false;
    	
    	return true;
    }
    
    
    void Person::get_full_name(char full_name[])
    {
    	full_name[0]='\0';
    	strcat(full_name,first_name);
    	strcat(full_name, " ");
    	strcat(full_name,second_name);
    	
    }
    
    
    bool Person::set_phone_number(char from_here[])
    {
    	
    	phone_number[0]='\0';
    	
    	if(strlen(from_here)<20)
    	{	
    		
    		strcpy(phone_number,from_here);
    		return true;
    		
    	}
    	else
    	{
    		
    		return false;
    	}
    	
    	
    }
    
    
    void Person::get_phone_number(char into_here[])
    
    {
    	into_here[0]='\0';
    	strcat(into_here,phone_number);
    } 
    
    
    void Person::print()
    {
    	cout<<"\n\t*\tfullname \t: "<<first_name<<" "<<second_name;
    	cout<<"\n\t*\tphone number\t: "<<phone_number;
    	cout<<"\n\t*\tage \t\t: "<<age<<"\n\n";
    }
    
    bool Person::load(ifstream &fin)
    {
    	
    	
    	fin>>first_name;
    	if(fin.good()==false) return false;
    	
    	fin>>second_name;
    	if(fin.good()==false) return false;
    	
    	fin>>phone_number;
    	if(fin.good()==false) return false;
    	
    	fin>>age;
    	if(fin.good()==false) return false;
    	
    	return true;
    	
    }
    
    
    void Person::print2(ofstream &fout)
    {
    	fout<<first_name<<" "<<second_name<<"\t"<<phone_number<<"\t"<<age<<endl;
    	
    }
    
    
    void main()
    
    {
    	
    	
    	Person person_list[50];
    	char fname[20];
    	Person swap;
    	int i;
    	char temp1,temp2;
    	char filename1[] = "h:\\test_file.txt";
    	char filename2[] = "h:\\test2.txt";
    	
    	ifstream fin;
    	ofstream fout;
    	fin.open(filename1);
    	fout.open(filename2);
    	
    	if(fin.is_open()==false)
    	{
    		cout<<"\noops - couldn't open file for reading!";
    		return;
    	}
    	else
    		cout<<"done\n";
    	int number_of_loaded_people = 0;
    	
    	while(!(fin.eof()))
    	{
    		if(person_list[number_of_loaded_people].load(fin)==true)
    			number_of_loaded_people++;
    	}	
    	
    	
    		for(i=0;i<number_of_loaded_people;i++)
    		{
    			person_list[i].get_full_name(temp1);
    			person_list[i+1].get_full_name(temp2);
    			if(strcmp(temp1,temp2)>0)
    			{
    				
    				swap.copy(person_list[i]);
    				person_list[i].copy(person_list[i+1]);
    				person_list[i+1].copy(swap);
    				
    			}
    		}
    		
    	
    	
    	print function
    	save to file function
    	fin.close();
    	fout.close();
    	
    	
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You can do a search of the board or the net to look for a description and code to implement a bubble search, if you don't know what that is. You need to use methods in the string.h header file to compare C style strings that you use in your program. If you aren't sure how to use strcmp() or similar functions, please search the board or read your compilers help file. You already appear to be familiar with file handling. Then about the only other thing you need to do is determine whether you are going to sort alphabetically by the second_name member or the first_name member.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Another solution is a sorted STL container such as a set.

    Kuphryn

Popular pages Recent additions subscribe to a feed