求助,用文本文件保存数据的问题??

xiaoou33 2012-03-24 03:28:53
用文本文件保存了数据,在Find()函数中,为什么设置的条件为p!=NULL时,在查找不到此人时,不会显示“查无此人!”好纠结,需要怎么改??
希望路过的帮帮我,谢谢!

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct PhoneMessage //数据类
{
char name[10]; //姓名
char lPhoneNumber[15]; //长号
char sPhoneNumber[10]; //短号
char idNumber[20]; //身份证号
string email; //邮箱地址
PhoneMessage *next;

void Input(); //输入信息
void Display(); //输出信息
void ReadFile(istream &infile); //从文本中读出数据
};


class PhoneFunction //功能类
{
public:
PhoneFunction(); //构造函数
// ~PhoneFunction();
void Insert(); //插入信息
void Show(); //显示全部人的电话信息
void Save(); //保存数据
PhoneMessage *Find(char*); //按姓名查找
private:
PhoneMessage *head,*rear;
ifstream infile;
ofstream outfile;
};

void PhoneMessage::Input()
{
cout<<"请输入姓名:";
cin>>name;
cout<<"请输入电话号码(长号):";
cin>>lPhoneNumber;
cout<<"请输入电话号码(短号):";
cin>>sPhoneNumber;
cout<<"请输入身份证号码:";
cin>>idNumber;
cout<<"请输入邮箱地址:";
cin>>email;
}

void PhoneMessage::Display()
{
cout<<"姓名:"<<name<<'\t'<<"长号:"<<lPhoneNumber<<'\t'
<<"短号:"<<sPhoneNumber<<'\t'<<"身份证号码:"<<idNumber
<<'\t'<<"电子邮箱地址:"<<email<<endl;
}


void PhoneMessage::ReadFile(istream &infile)
{

infile>>name>>lPhoneNumber>>sPhoneNumber>>idNumber>>email;
}

PhoneFunction::PhoneFunction()
{
head=new PhoneMessage; //生成头结点
head->next=new PhoneMessage;
rear=head->next;

infile.open("PhoneMessage.text");
if(!infile)
cout<<"电话号码系统中没有任何号码,请输入号码!"<<endl;
else
{
while(!infile.eof())
{
rear->ReadFile(infile);
rear->next=new PhoneMessage;
rear=rear->next;
}
infile.close();
cout<<"读取电话号码成功!"<<endl;
}
}

void PhoneFunction::Insert() //尾插法插入数据
{
PhoneMessage *s;
string st="yes";
while(st=="yes")
{
s=new PhoneMessage; //生成新结点
rear->Input();
rear->next=s; //新结点插入到表尾
rear=s; //尾指针rear指向新的表尾
cout<<"是否继续输入(yes/no)......";
cin>>st;
}
rear->next=NULL; //尾结点后继指针置空

}

void PhoneFunction::Show()
{
for(PhoneMessage *p=head->next;p!=rear;p=p->next)
p->Display();
}

void PhoneFunction::Save()
{
outfile.open("PhoneMessage.text");
for(PhoneMessage *p=head->next;p!=rear;p=p->next)
outfile<<p->name<<"\t"<<p->lPhoneNumber<<"\t"<<p->sPhoneNumber
<<"\t"<<p->idNumber<<"\t"<<p->email<<endl;
outfile.close();
cout<<"保存成功!"<<endl;
}

PhoneMessage *PhoneFunction::Find(char *n) //按姓名查找
{
PhoneMessage *p;
for(p=head->next;p!=NULL;p=p->next)
{
if(strcmp(p->name,n)==0)
p->Display();
}
if(p!=NULL) //为什么这里设置的条件为p!=NULL时,
cout<<"查无此人!"<<endl; //在查找不到此人时,不会显示“查无此人!”
return 0; //需要怎么改???
}

int main()
{
PhoneFunction Ph;
char name1[10];
Ph.Insert();
Ph.Show();
Ph.Save();

cout<<"请输入要查找的人的姓名:";
cin>>name1;
Ph.Find(name1);

return 0;
}
...全文
545 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoou33 2012-03-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xiaohuh421 的回复:]
PhoneMessage *PhoneFunction::Find(char *n) //按姓名查找
{
PhoneMessage *p;
for(p=head->next;p!=NULL;p=p->next)//问题在这里。下面说明
{
if(strcmp(p->name,n)==0)
p->Display();
}

你在循环中会遍历所有“人”,所以循环完成后,你的p始终为N……
[/Quote]查找到就跳出,那有同名字的怎么办,不就没查找完全???
blingpro 2012-03-24
  • 打赏
  • 举报
回复
PhoneMessage *PhoneFunction::Find(char *n) //按姓名查找
{
PhoneMessage *p;
for(p=head->next;p!=NULL;p=p->next)
{
if(strcmp(p->name,n)==0)
{
p->Display();
break;
}

}
if(p!=NULL) //为什么这里设置的条件为p!=NULL时, //这里改为if(p == NULL)
cout<<"查无此人!"<<endl; //在查找不到此人时,不会显示“查无此人!”
return 0; //需要怎么改???
}
xiaohuh421 2012-03-24
  • 打赏
  • 举报
回复
PhoneMessage *PhoneFunction::Find(char *n) //按姓名查找
{
PhoneMessage *p;
for(p=head->next;p!=NULL;p=p->next)//问题在这里。下面说明
{
if(strcmp(p->name,n)==0)
p->Display();
}

你在循环中会遍历所有“人”,所以循环完成后,你的p始终为NULL。
在按1楼该的同时,再改成如下:
PhoneMessage *PhoneFunction::Find(char *n) //按姓名查找
{
PhoneMessage *p;
for(p=head->next;p!=NULL;p=p->next)//问题在这里。下面说明
{
if(strcmp(p->name,n)==0)
{
p->Display();
break;//查找到人就结束查询。
}
}
xiaoou33 2012-03-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qixing1115 的回复:]
if(p!=NULL) //为什么这里设置的条件为p!=NULL时,
cout<<"查无此人!"<<endl; //在查找不到此人时,不会显示“查无此
改为:
if(p==NULL) //为什么这里设置的条件为p!=NULL时,
cout<<"查无此人!"<<endl; //在查找不到此人时,不会显示“查无此
[/Quote]不行啊,设置为p==NULL时,在找到此人时,也会显示“查无此人!”.
qixing1115 2012-03-24
  • 打赏
  • 举报
回复
if(p!=NULL) //为什么这里设置的条件为p!=NULL时,
cout<<"查无此人!"<<endl; //在查找不到此人时,不会显示“查无此
改为:
if(p==NULL) //为什么这里设置的条件为p!=NULL时,
cout<<"查无此人!"<<endl; //在查找不到此人时,不会显示“查无此

64,680

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧