can’t extract data from a structure array c++ [closed]

This should look a bit more like a code intended for humans to read 🙂

#include<iostream>
#include<string>

using namespace std;

int ncr;
int me,n=0,u,y,l;
char opt1,opt2,opt3,opt4;

struct student
{
  string Name;
  int DoB;
  int Age;
  string Address;
  string Course[5];
  char grade[5];
};

void add();
void remove();
void update();
void search();

int main()
{   
  int opt;
  student s[n];

  while(1)
  {
    cout << " select what you want to do \n\n 1. Add new record \n2. Remove record\n3. Update Record\n4. Search for particular student\n" ;
    cin >> opt;
    if(opt==1)
    {
      add();
    }
    else if(opt==2)
    {
      remove();
    }
    else if(opt==3)
    {
      update();
    }
    else if(opt==4)
    {
      search();
    }
  }
}

void add()
{
  n++;
  student s[n];
  do
  {
    cout << "enter name of student ";
    cin >> s[n-1].Name;
    cout << "enter DoB of student ";
    cin >> s[n-1].DoB;
    cout << "enter age of student ";
    cin >> s[n-1].Age;
    cout << "enter address of student ";
    cin >> s[n-1].Address;
    cout << "enter courses and grades (max 5) \n";

    for(int x=0;x<5;x++)
    {
      cout<<"course:";
      cin>>s[n-1].Course[x];
      cout<<"grade:";
      cin>> s[n-1].grade[x];
    }
    cout << "Roll No. : " << n << "\nName :" << s[n-1].Name << "\nDoB :" << s[n-1].DoB << "\nAge :" << s[n-1].Age << "\nAddress :" << s[n-1].Address;
    for( int x = 0; x < 5; x++ )
    {
      cout << "\n"<<s[n-1].Course[x];
      cout << "   grade :  "<< s[n-1].grade[x]<<"\n";}
      cout << "repeat for another student 'y' or 'n' ?";
      cin >> opt1;
  }
  while(opt1=='y'|| opt1=='Y');
}

void remove()
{
  student s[n];
  do
  {
    cout << "enter student roll no to remove data";
    cin >> l;
    for( int i=l; i < n; i++ )
    {    
      s[l-1].Name=s[l].Name;
      s[l-1].DoB= s[l].DoB;
      s[l-1].Age=s[l].Age;
      s[l-1].Address=s[l].Address;

      for(int j=0;j<5;j++)
      {
        s[l-1].Course[j]=s[l].Course[j];
        s[l-1].grade[j]=s[l].grade[j];
      }
    }
    cout<<"Record Removed\n";
    n--;

    cout << "repeat ? 'y' or 'n' ";
    cin >> opt2;
  }
  while(opt2 == 'y' || opt2 == 'Y');
}


void update()
{
  student s[n];
  do
  {
    cout << "enter the roll no of student you want to update data";
    cin >> u;

    cout <<  "Roll No. : " << u;
    cout << "\nName : ";
    cout << s[u-1].Name;
    cout << "\nDoB : ";
    cout << s[u-1].DoB;
    cout << "\nAge : ";
    cout << s[u-1].Age;
    cout << "\nAddress :";
    cout << s[u-1].Address;
    cout << "\nCourses and Grades\n";
    for( int r=0; r < 5; r++ )
    {
      cout << s[u-1].Course[r];
      cout << "\t"<< s[u-1].grade[r] << endl;
    }
    cout << "enter name of student ";
    cin >> s[u-1].Name;
    cout << "enter DoB of student ";
    cin >> s[u-1].DoB;
    cout << "enter age of student ";
    cin >> s[u-1].Age;
    cout << "enter address of student ";
    cin >> s[u-1].Address;
    cout << "enter courses and grades (max 5) \n";
    for(int x=0; x < 5; x++ )
    {
        cout<<"course:";
        cin>>s[u-1].Course[x];
        cout<<"grade:";
        cin>> s[u-1].grade[x];
    }
    cout << "Roll No. : " << u;
    cout << "\nName : ";
    cout << s[u-1].Name;
    cout << "\nDoB : ";
    cout << s[u-1].DoB;
    cout << "\nAge : ";
    cout << s[u-1].Age;
    cout << "\nAddress :";
    cout << s[u-1].Address;
    cout << "\nCourses and Grades\n";
    for( int r = 0; r < 5; r++)
    {
      cout << s[u-1].Course[r];
      cout << "\t"<< s[u-1].grade[r]<<endl; 
    }   
    cout << "repeat ? 'y' or 'n' ";
    cin >> opt3;
  }
  while(opt3 == 'y' || opt3 == 'Y');
}

void search()
{
  student s[n];
  do
  {
    cout << "enter the roll no of student you want to search data of";
    cin >> y;

    cout << "Roll No. : " << n << "\nName :" << s[y-1].Name << "\nDoB :" << s[y-1].DoB << "\nAge :" << s[y-1].Age << "\nAddress :" << s[y-1].Address << "\nCourses and Grades\n";
    for( int r=0; r < 5; r++ )
    {
      cout << s[y-1].Course[r];
      cout << "\t" <<  s[y-1].grade[r]<<endl;
    }
    cout << "repeat ? 'y' or 'n' ";
    cin >> opt4;
  }
  while( opt4 == 'y' || opt4 == 'Y' );
}

The problem you’re having is with the scope of your programme.
In the main() function you declare an array, which presumably should be holding all the records. However for every option (function add/remove/update/search) you declare a new one. These arrays are local to those functions and are being “forgotten” after the function is finished.
In order to fix that, you have to pass the original array (the one from main()) to those functions.

The next problem, which is more severe is the fact, that you can’t change the size of an array in the runtime! The elements you were accessing in the 3rd option were some random bytes in the memory and this can cause a lot of trouble.
Either create an array with e.g. 100 fields ( student s[100]; ), or even better try using std::vector, which can size you can adjust according to the number of records.

Third thing, remember that formatting code is extremely important. Hopefully it was just a copy-paste problem that made your code look unreadable, but if it’s not the case then try to change. Well formatted code is easier to understand and will make it easier for you to debug it.

Leave a Comment