辽宁装备制造学院:内行请进!

来源:百度文库 编辑:神马品牌网 时间:2024/04/30 11:45:50
为什么不能打印?
能编译通过,为什么打印函数就是不能打印出结果?
运行环境:Visual C++6.0

//#include<stdio.h>
#include<iostream>//不能写成#include<iostream.h>,不知道为什么?
using namespace std;

struct Llist
{
char name[20];
int sign;
struct Llist *next;
};

struct Llist *creat()
{
struct Llist *p,*p1,*p2;
int flag=0;//在建表过程中判断用户是否要退出建立工作
cout<<"if you have finish instruction please let the flag be 0! Continue let it be 1"<<endl;
p=new Llist;//建立第一个结点,不知道是不是这样????????????还是struct Llist
p1=p;
p2=p;
cout<<"Please input the name the person:"<<endl;
cin.get(p->name,20);
cin.get();//吸收回车键
cout<<"Please input the sign the person:"<<endl;
cin>>p->sign;
cin.get();//吸收回车键
cout<<"/t* Another?Please input the flag: *"<<endl;
cin>>flag;
cin.get();//吸收回车键
while(flag)
{
p2=new Llist;
p1->next=p2;
cout<<"Please input the name the person:"<<endl;
cin.get(p2->name,20);
cin.get();//吸收回车键
cout<<"Please input the sign the person:"<<endl;
cin>>p2->sign;
cin.get();//吸收回车键
cout<<"Another?Please input the flag:"<<endl;
cin>>flag;
cin.get();//吸收回车键
if(flag)
{
p1=p2;
}
}
p2->next=NULL;//尾结点指针置空
return(p);
}

void display(Llist *p)
{
do
{
cout<<p->name<<endl;
cout<<p->sign<<endl;
p++;
}while(p->next!=NULL);
// cout<<p->name<<endl;//打印最后一个结点
// cout<<p->sign<<endl;
}

void main()
{
struct Llist *p;
int m;
cout<<"Please input the m:"<<endl;
cin>>m;
cin.get();//吸收回车键
if(m==1)
{
p=creat();
display(p);
}

}

帮你纠正了几处错误,可以了
#include<iostream> //c++标准库必须这样写
using namespace std;

struct Llist
{
char name[20];
int sign;
Llist *next; //去掉struct修饰,下面几处也是如此
};

Llist *creat()
{
Llist *p,*p1,*p2;
int flag=0;
cout<<"if you have finish instruction please let the flag be 0! Continue let it be 1"<<endl;
p=new Llist;
p1=p;
p2=p;
cout<<"Please input the name the person:"<<endl;
cin.get(p->name,20);
cin.get();
cout<<"Please input the sign the person:"<<endl;
cin>>p->sign;
cin.get();
cout<<"/t* Another?Please input the flag: *"<<endl;
cin>>flag;
cin.get();
while(flag)
{
p2=new Llist;
p1->next=p2;
cout<<"Please input the name the person:"<<endl;
cin.get(p2->name,20);
cin.get();
cout<<"Please input the sign the person:"<<endl;
cin>>p2->sign;
cin.get();
cout<<"Another?Please input the flag:"<<endl;
cin>>flag;
cin.get();
if(flag)
{
p1=p2;
}
}
p2->next=0; //这里空指针是这样定义的
return(p);
}

void display(Llist *p)
{
do
{
cout<<p->name<<endl;
cout<<p->sign<<endl;
//p++; //不要用p++,因为p不是数组
}while(p=p->next); //这样跳转

}

void main()
{
Llist *p;
int m;
cout<<"Please input the m:"<<endl;
cin>>m;
cin.get();
if(m==1)
{
p=creat();
display(p);
}

cin>>m;
}