oxc000005 VC

来源:百度知道 编辑:UC知道 时间:2024/07/02 14:08:18
#include"iostream"
using namespace std;

typedef struct Students
{
Students * next;
string no;
int mark;
}stu;

int main()
{
FILE *in,*out;
in = fopen("in.txt","r");
out = fopen("out.txt","w");
if (in == NULL)
{
cout<<"in error!";
exit(0);
}
if (out == NULL)
{
cout<<"out error!";
exit(0);
}
stu student;
stu * p = &student;
for(int i = 0;i<10;i++)
{
fscanf(in,"%s",&p->no);
fscanf(in,"%d",&p->mark);
p->next = new stu;
p = p->next;
}
p = NULL;
p = &student;
while(p)
{
cout<<p->mark<<endl;
p = p->next;
}
cout<<"right";
return 0;
}

循环的时候多生成了一个节点,导致尾节点的后继没赋成空。

for(int i=0;;i++)
{
fscanf(stdin,"%s",&p->no);
fscanf(stdin,"%d",&p->mark);
if (i == 9) break;
p->next = new stu;
p = p->next;
}
p->next = NULL;