1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help !! String arrays n delete function

Discussion in 'C++' started by asibuan, Sep 16, 2009.

  1. asibuan

    asibuan New Member

    Joined:
    Sep 16, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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 = 2;
        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;
    }
    
    
    ========================================================

    anyone got any idea how to create a delete function ?? asap . Thx in advandce
     
    Last edited by a moderator: Sep 17, 2009
  2. SaswatPadhi

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

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    What exactly do you want your delete function to do ?

    (1) Delete a specific student (getting his ID from user) :
    (Of course, assuming ID to be unique)

    Code:
    void delete(student array[],const  unsigned int MAX_STUDENT)
    {
      bool found=false;
      unsigned int ID;
      cout << "Enter the ID of the student to be removed : ";
      cin >> ID;
      for(unsigned int i = 0 ; i < MAX_STUDENT; i++)
      {
        // search for the id
        if(array[i].id == ID)
        {
          // move all ids after the required one, one step backward in the list
          for(unsigned int j = i ; i < MAX_STUDENT-1; j++)
            array[i] = array[i+1];
          found=true;
          break;
        }
      }
      if(found)  cout << "ID " << ID << " removed successfully !\n";
      else        cout << "ID " << ID << " was not found !\n";
    }
    In this case, remember to reduce the count of MAX_STUDENT or whichever variable you use to store the total number of students in the list, by one (after calling the delete function). The delete function cannot modify it's value as you are passing it as a "const".

    (2) Delete the whole list :
    Simple enough.
    Just make the count 0 so that new values will be written "over" the old values.
     

Share This Page