Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Help !! String arrays n delete function (http://www.go4expert.com/forums/help-string-arrays-n-delete-function-t19459/)

asibuan 16Sep2009 21:29

Help !! String arrays n delete function
 
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

SaswatPadhi 17Sep2009 19:10

Re: Help !! String arrays n delete function
 
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: CPP

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.


All times are GMT +5.5. The time now is 14:54.