0
我剪掉了我的学生管理作业的这一部分,当我比较2个名称,1个来自输入,1是我想搜索的内容时,它打印出结果但立即崩溃。请帮忙。我明白任何回应,谢谢当比较数组中的2个字符串时,程序崩溃
class Student
{
public:
string name;
public:
void input();
};
class list
{
private:
Student ds[100];
int count=0;
int number,addnum=0;
int total=number+addnum;
public:
void input(int y);
void find();
friend class SinhVien;
};
void SinhVien::input()
{
fflush(stdin);
gotoXY(5,9);cout<<"name : ";
cin>>name;
}
void list::input(int y)
{
fflush(stdin);
createBox(2,5,63,20,7);
gotoXY(8,y-3);
cout<<"add student\n";
gotoXY(8,y-1);
if (count==0)
{
cout<<"number of students : ";
cin>>number;
for(int i=1; i<=number; i++)
{
gotoXY(8,7);
cout<<"student "<<i<<endl;
ds[i].input();
count++;
}
}
else if(count>0)
{
cout<<"number of student : ";
cin>>addnum;
for(int i=1; i<=addnum; i++)
{
ds[i+count].input();
count++;
}
}
}
void list::find()
{
int count=0;
string name1;
gotoXY(10,51);
cout<<"type name : ";
cin>>name1;
for(int i=0; i<total; i++)
{
string name2 =ds[i].name;
if(name1.compare(name2) ==0)
{
cout<<"yes";
count++;
}
}
if(count==0)
cout<<"not found";
}
int main()
{
list a;
a.input(1);
int x;
cin>>x;
if (x==1)
a.find();
}
第1步。了解如何使用调试器。找出崩溃发生的位置,并检查变量和内存的值。如果你仍然不确定如何解决这个问题,那么**询问SO – UnholySheep
当'total = number + addnum'被评估时,'number'还没有初始化,并且包含随机垃圾。此时,您的程序会通过访问未初始化的对象来展示未定义的行为。实际上,'total'也包含随机垃圾,然后'find()'中的循环超出了数组边界。 –
明白了,谢谢 –