无法从结构数组c ++中提取数据

时间:2017-05-12 03:33:44

标签: c++ arrays struct

程序工作正常并执行所有选项,但在第3和第4选项中,在第3次搜索学生时,程序不会超出数据输入部分,而在第4次搜索数据并打印时,它在int数据类型的变量中给出了垃圾值,而不显示字符串或char类型变量。任何帮助将非常感激。编辑:抱歉给您带来不便,我现在将功能从主代码中分离出来。希望现在更容易理解。

#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');}

1 个答案:

答案 0 :(得分:1)

这应该看起来更像是一个供人类阅读的代码:)

#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' );
}

您遇到的问题在于您的计划范围。 在main()函数中,您声明了一个数组,可能应该保存所有记录。但是对于每个选项(函数添加/删除/更新/搜索),您都声明一个新选项。这些阵列对这些功能来说是本地,并且正在被遗忘&#34;功能完成后。 为了解决这个问题,你必须将原始数组(main()中的数组)传递给那些函数。

下一个更严重的问题是,您无法在运行时更改数组的大小!您在第3个选项中访问的元素是一些内存中的随机字节,这可能会造成很多麻烦。 用例如创建一个数组100个字段(student s[100];),甚至更好地尝试使用std::vector,其大小可以根据记录数量进行调整。

第三件事,请记住格式化代码非常重要。希望这只是一个复制粘贴问题,使您的代码看起来不可读,但如果情况并非如此,那么请尝试更改。格式良好的代码更容易理解,并使您更容易调试它。