5 Replies - 903 Views - Last Post: 01 September 2012 - 11:41 PM Rate Topic: -----

#1 Sawamura  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-September 12

Student record system don't know how to delete

Posted 01 September 2012 - 01:44 PM

I am new to C++ and I have browse through the forum to get ideas on how to solve my assignment using struct and so far I am having problem with even just looping the program without it prompting me a yes or no.I am not asking you guys to do the home work for me but it would be great if you could point where did I go wrong here . I am suppose to make a student record system where the user can add/delete/modify/display the data .However I have no clue how I should display the data using struct and arrays .Also I am not sure how I am able to delete/modify using arrays . Please help me....

#include<iostream>
#include <string>
#include<cstdlib>
using namespace std;

int a,b,c;
int size = 0;


struct student
{
 char name[150];
 char faculty[150];
 int id;
 char course[100];

};
int main()
{
    char choice,another;
    int i=0;

    struct student stud[10];


    bool quit = false;
    while(1)
    {


        system("cls");
         cout << "\t\t==== STUDENT RECORD SYSTEM ====";
         cout << "\n \t\t\t 1. Add    Records";
         cout << "\n \t\t\t 2. Delete    Records";
         cout << "\n \t\t\t 3. Modify    Records";
         cout << "\n \t\t\t 4. Display    Records"<<endl;
         cout << "\t\t Select Your Choice :=> ";

         cin >>choice;

         switch(choice)

        {
            case '1' :



            system("cls");

            for(i=0; i<2; i++)


{

                  system("cls");

                cout << "Enter Name : ";

                cin >> stud[i].name;

                cout << "Enter Faculty : ";

                cin >> stud[i].faculty;

                cout << "Enter Student ID    : ";

                cin >> stud[i].id;

                cout << "Enter Student Course   : ";

                cin >> stud[i].course;


}



            break;

            case '2' :

            break;

            case '3':
            break;

            case '4':

            system("cls");

            for(i=0; i<2; i++)
            {
              cout<<"\nID number student  # "<<i<<": "<<stud[i].name;
	         cout<<"\nFirst name student # "<<i<<": "<<stud[i].faculty;
	         cout<<"\nGender student     # "<<i<<": "<<stud[i].id;
	       cout<<"\nAge student        # "<<i<<": "<<stud[i].course<<"\n";



            }

            break;

            case '5' :

            exit(0);


            default:
                		cout << "Not a valid choice.\n"
                     		        << "Choose again.\n";


        string input;
        cout<<"Do you want to quit? [yes/no]";
        cin>>input;
        {

            if (input == "yes")
            {
                quit = true;
            }


        }



    }
    system("pause");
    return 0;
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Student record system don't know how to delete

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: Student record system don't know how to delete

Posted 01 September 2012 - 03:02 PM

Quote

.However I have no clue how I should display the data using struct and arrays .


How would you modify the data if it was simply an array of integers? A struct is no different than basic types in terms of memory. To edit an array, you simply edit the contents at that memory location (via [] operator). If you want to delete something from an array...well that's a little more tricky. Since you cannot explicitly delete memory on the stack, the easily solution would be to define a boolean for each student which declares them as active, or inactive. Or, alternatively, you could shift the student array and rewrite over the student that is being deleted, however this becomes inefficient when dealing with large amounts of memory.

i.e.

struct Point {
   int x, y;
};

int main(int argc, char *argv[]) {
   Point points[2];
   
   //set point 0
   points[0].x = 1;
   points[0].y = 2;
   
   //set point 1
   points[1].x = 10;
   points[1].y = 20;
   
   // edit point 0
   points[0].x = 2;
   points[0].y = 3;
   
   //since data is just data, you can representing the data as integers
   //this is just for educational purposes, you rarely doing anthing like this
   int *data = (int*)points;
   for(int i=0; i<sizeof(points) / sizeof(int); i++)  {
      std::cout<<data[i]<<std::endl;
   }
   
   return 0;
}


This post has been edited by jjl: 01 September 2012 - 03:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 762
  • View blog
  • Posts: 2,214
  • Joined: 20-March 10

Re: Student record system don't know how to delete

Posted 01 September 2012 - 09:38 PM

Hi,

jjl makes some great points I would just like to add further...

your program entry point main should remain uncluttered as possible.

It could look something like this

//Program Entry Point
int main()
{
    int choice = 0;


    struct student stud;
    stud.i=0;//initialise counter variable i

    bool quit = false;
    while(quit == false)
    {

        choice = menu();


        switch(choice)

        {
        case 1 :


            stud.add_Record();
            break;

        case 2 :
            stud.delete_Record();
            break;

        case 3:
            stud.modify_Record();
            break;

        case 4:
            stud.display_Record();
            break;

        case 5 :

            exit(0);
            break;

        default:
            cout << "Not a valid choice.\n"
                 << "Choose again.\n";


            string input;
            cout<<"Do you want to quit? [yes/no]";
            cin>>input;
            {

                if (input == "yes")
                {
                    quit = true;
                }


            }



        }
        system("pause");

    }
    return 0;
}



your struct could look something like

struct student
{
    string name[150];
    string faculty[150];
    int id[150];
    string course[150];
    int i; //acts as counter variable
    void add_Record();
    void display_Record();
    void delete_Record();
    void modify_Record();
};



to give you an idea, your main menu should be moved into a function something like this

// start of menu function
int menu(void)

{
    int choice = 0;
    while(choice ==0)
    {


        system("cls");
        cout << "\t\t==== STUDENT RECORD SYSTEM ====";
        cout << "\n \t\t\t 1. Add    Records";
        cout << "\n \t\t\t 2. Delete    Records";
        cout << "\n \t\t\t 3. Modify    Records";
        cout << "\n \t\t\t 4. Display    Records"<<endl;
        cout << "\t\t Select Your Choice :=> ";

        cin >>choice;
    }
    return choice;
}



Modifying a record would just be like add record except you would ask which record number then just overwrite that record.

Deleting a record is trickier as has been said by jjl the whole thing could be made easier if you are allowed to use a vector array ....

Are you allowed to use vectors for your assignment ?

Regards

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#4 Sawamura  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-September 12

Re: Student record system don't know how to delete

Posted 01 September 2012 - 10:55 PM

View Postjjl, on 01 September 2012 - 03:02 PM, said:

Quote

.However I have no clue how I should display the data using struct and arrays .


How would you modify the data if it was simply an array of integers? A struct is no different than basic types in terms of memory. To edit an array, you simply edit the contents at that memory location (via [] operator). If you want to delete something from an array...well that's a little more tricky. Since you cannot explicitly delete memory on the stack, the easily solution would be to define a boolean for each student which declares them as active, or inactive. Or, alternatively, you could shift the student array and rewrite over the student that is being deleted, however this becomes inefficient when dealing with large amounts of memory.

i.e.

struct Point {
   int x, y;
};

int main(int argc, char *argv[]) {
   Point points[2];
   
   //set point 0
   points[0].x = 1;
   points[0].y = 2;
   
   //set point 1
   points[1].x = 10;
   points[1].y = 20;
   
   // edit point 0
   points[0].x = 2;
   points[0].y = 3;
   
   //since data is just data, you can representing the data as integers
   //this is just for educational purposes, you rarely doing anthing like this
   int *data = (int*)points;
   for(int i=0; i<sizeof(points) / sizeof(int); i++)  {
      std::cout<<data[i]<<std::endl;
   }
   
   return 0;
}



Running the code I would get the output

2
3
10
20

so by specifying the address I would be able to edit the values inside the arrays ?


@snoopy

This is the Modified code and there is no restriction in my assignment the lecturer didn't mention that we couldn't use vector in our coding .

#include<iostream>
#include <string>
#include<cstdlib>

using namespace std;



struct student
{
    string name[150];
    string faculty[150];
    int id[150];
    string course[150];
    int i; //acts as counter variable
    void add_Record();
    void display_Record();
    void delete_Record();
    void modify_Record();
};

// start of menu function
int menu(void)

{
    int choice = 0;
    while(choice ==0)
    {


        system("cls");
        cout << "\t\t==== STUDENT RECORD SYSTEM ====";
        cout << "\n \t\t\t 1. Add    Records";
        cout << "\n \t\t\t 2. Delete    Records";
        cout << "\n \t\t\t 3. Modify    Records";
        cout << "\n \t\t\t 4. Display    Records"<<endl;
        cout << "\t\t Select Your Choice :=> ";

        cin >>choice;
    }
    return choice;
}

//To add Record
void add_Record()
{
    int i=0;
                system("cls");

                cout << "Enter Name : ";

                cin >> stud[i].name;

                cout << "Enter Faculty : ";

                cin >> stud[i].faculty;

                cout << "Enter Student ID    : ";

                cin >> stud[i].id;

                cout << "Enter Student Course   : ";

                cin >> stud[i].course;

     return ;

}





//Program Entry Point
int main()
{
    int choice = 0;


    struct student stud;
    stud.i=0;//initialise counter variable i

    bool quit = false;
    while(quit == false)
    {

        choice = menu();


        switch(choice)

        {
        case 1 :


            stud.add_Record();
            break;

        case 2 :
            stud.delete_Record();
            break;

        case 3:
            stud.modify_Record();
            break;

        case 4:
            stud.display_Record();
            break;

        case 5 :

            exit(0);
            break;

        default:
            cout << "Not a valid choice.\n"
                 << "Choose again.\n";


            string input;
            cout<<"Do you want to quit? [yes/no]";
            cin>>input;
            {

                if (input == "yes")
                {
                    quit = true;
                }


            }



        }
        system("pause");

    }
    return 0;
}




Why does it says that stud was not declared in this scope at line 52 ? . I tried putting in the function using


stud add_Record()
{

    int i=0;
                system("cls");

                cout << "Enter Name : ";

                cin >> stud[i].name;

                cout << "Enter Faculty : ";

                cin >> stud[i].faculty;

                cout << "Enter Student ID    : ";

                cin >> stud[i].id;

                cout << "Enter Student Course   : ";

                cin >> stud[i].course;

     return ;

}



It's still isn't working .
Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 762
  • View blog
  • Posts: 2,214
  • Joined: 20-March 10

Re: Student record system don't know how to delete

Posted 01 September 2012 - 11:37 PM

Well,

I changed it a bit and you would have to use the scope resolution operator ::

something like this will work

void student::add_Record()
{

    system("cls");

    cout << "Enter Surname : ";

    cin >> name[i];
    cin.ignore(30, '\n');
    cout << "Enter Faculty : ";
    cin >> faculty[i];
    cin.ignore(30, '\n');
    cout << "Enter Student ID    : ";
    cin >> id[i];
    cin.ignore(30, '\n');
    cout << "Enter Student Course   : ";
    cin >> course[i];

    i++;
}



I can't just code the entire answer for you because

1. It's against the TOS

2. You wont learn anything much.

Try doing a bit of research on std::vector
and specifically the public member function vector::erase this will make the whole thing a lot easier.

Best Wishes

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#6 Sawamura  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-September 12

Re: Student record system don't know how to delete

Posted 01 September 2012 - 11:41 PM

thanks snoopy you helped me a lot I will give my best .
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1