首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

,用文本文件保存数据的有关问题?

2012-05-13 
求助,用文本文件保存数据的问题??用文本文件保存了数据,在Find()函数中,为什么设置的条件为p!NULL时,在查

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

[code=C/C++][/code]#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;
}

[解决办法]
if(p!=NULL) //为什么这里设置的条件为p!=NULL时, 
cout<<"查无此人!"<<endl; //在查找不到此人时,不会显示“查无此
改为:
if(p==NULL) //为什么这里设置的条件为p!=NULL时, 
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();
}

你在循环中会遍历所有“人”,所以循环完成后,你的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;//查找到人就结束查询。
}
}

[解决办法]
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; //需要怎么改???
}

热点排行