c++问题求解

来源:百度知道 编辑:UC知道 时间:2024/05/08 10:23:11
#include <iostream.h>
# define N 2
struct node
{int num;
char name [N];
char male[N];
int tel;
node *next;};

void main(){
node *p;
node *head='\0';
int i;
for (i=0;i<N;i++)
{p=new(node);
cout<<"input"<<i<<"information";
cin>>(*p).num>>(*p).name>>(*p).male>>(*p).tel;
(*p).next=head;
head=p;}
char x[N];
cout<<"input name:";
cin>>x[N];

while (p!='\0')
{if((*p).name==x)
{cout<<(*p).num<<" "<<(*p).name<<" "<<(*p).male<<" "<<(*p).tel;break;}
else p=(*p).next;}if (p=='\0') cout<<"not found";
}
错在哪?谢谢!

这样就行了:有注释:

#include <iostream.h>
#include "string.h"
#define N 10 // 字符数组的长度 name[N], male[N]
#define M 3 // 信息总条数

struct node
{
int num;
char name [N];
char male[N];
int tel;
node *next;
};

void main(){
node *head=NULL; //写成NULL 更好
int i;
for (i=0;i<M;i++)
{
node *p=new node;
cout<<"input "<<i<<" information:"<<endl;
cin>>(*p).num>>(*p).name>>(*p).male>>(*p).tel;
(*p).next=head;
head=p;
}
char x[N];
cout<<"input name:";
cin>>x; // 不能是 x[N]
node *p = head;
while (p!=NULL)
{
if(strcmp(p->name,x)==0) // 不能是 (*p).name==x
{
cout<<(*p).num<<" "<<(*p).name<&l