array and string.....

Discussion in 'C++' started by xfreebornx, Aug 31, 2009.

Thread Status:
Not open for further replies.
  1. xfreebornx

    xfreebornx New Member

    Joined:
    Aug 31, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    please i need ideas on how i will put delete function in this program...?
    tnx

    Code:
    #include<iostream>
    #include<algorithm> //for std::sort
    #include<string>
    
    using namespace std;
    
    struct student
    {
      int id;
      string name;
      string nationality;
      string gender; 
    };
    
    void insert(student array[],const  unsigned int MAX_STUDENT);  
    
    bool sortByID(const student& a, const student& b)
    {
    	return ( a.id < b.id );
    }
    
    
    char sortByName(const student& a, const student& b)
    {
    	return ( a.name < b.name);
    }
    
    char sortByNationality(const student& a, const student& b)
    {
    	return (a.nationality < b.nationality);
    }
    
    void mySort(student array[],const unsigned int MAX)
    {
    	int opt = 0;
    	cout<<"Welcome to sorting function please do the following selection"<<endl;
    	cout<<"***************************************************"<<endl;
    	cout<<"* 1.Sort by student ID"<<endl;
    	cout<<"* 2.Sort by student Name"<<endl;
        cout<<"* 3.Sort by Nationality"<<endl;
    	cout<<"***************************************************"<<endl;
    	cout<<"Selection: ";
    	cin >> opt;
    	cout<<"\n\n";
    
    	switch(opt)
    	{
    		case 1: std::sort(array,array+MAX,sortByID); break;
    		case 2:	std::sort(array,array+MAX,sortByName); break;
    		case 3:	std::sort(array,array+MAX,sortByNationality);break;
    	}
    
    }
    
    void display(const student array[], unsigned int MAX);
    int main()
    {	
    	const unsigned int MAX_SIZE = 100;
    	student array[MAX_SIZE];
    
    	int option = 0;
    	bool exitProgram = false;
    
    	do
    	{ 
    		cout <<"Welcome to student recording system"<<endl;
    		cout <<"Please choose one of the following option\n\n"<<endl;
    		cout <<"1.Insert new record"<<endl;
    		cout <<"2.Sort record"<<endl;
    		cout <<"3.Delete record"<<endl;
    		cout <<"4.Display record"<<endl;
    		cout <<"0) Exit program\n\n\n";
        
    		 cin >> option;
    
    		 switch(option)
    		 {
    			case 1: insert(array,MAX_SIZE);  break;
    
    			case 2:	cout<<"you picked sorting\n";
    					cout<<"Now sorting\n\n\n";
    					mySort(array,MAX_SIZE);
    					cout<<"Sorting done\n\n\n";
    					break;
    
    			case 3: //do delete
    			case 4:   display(array,MAX_SIZE); break;
    
    			default : exitProgram = true; break;
    		 }
    
    		}while(!exitProgram);     		
    	  
    
    	return 0;
    
    }
    void insert(student array[],const  unsigned int MAX_STUDENT)
    {
    
    	cout<<"\n\n";
    
      for(unsigned int i = 0 ; i < MAX_STUDENT; i++)
      {
    
    	cout<<"For student #"<<(i+1)<<endl<<endl;;
    	cout <<"Enter student ID: ";
        cin >>array[i].id;
        cin.ignore(100,'\n');
    	cout<<endl;
        
        cout <<"Enter student name: ";
        cin >>array[i].name;
        cin.ignore(100,'\n');
    	cout<<endl;
        
        cout <<"Enter student nationality:  ";
        cin >>array[i].nationality;
        cin.ignore(100,'\n');
    	cout<<endl;
        
        cout <<"Enter student gender:  ";
        cin >>array[i].gender;
        cin.ignore(100,'\n');
    	cout<<endl;
        }
    }
    
    void display(const student array[], unsigned int MAX)
    {
    	cout<<"\\************************************************\\"<<endl;
    	for(unsigned int i = 0; i < MAX; i++)
    	{		
    		cout<<"---------------------------------------------------"<<endl;;
    		cout<<"Student #:"<<(i+1)<<endl;
    		cout<<"ID# : "<<array[i].id<<endl;
    		cout<<"Name : "<<array[i].name<<endl;
    		cout<<"Nationality : "<<array[i].nationality<<endl;		
    		cout<<"Gender : "<<array[i].gender<<endl<<endl;
    		cout<<"---------------------------------------------------"<<endl;;
    	
    	}
    	cout<<"\n\\************************************************\\"<<endl;
    }
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
Thread Status:
Not open for further replies.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice