通讯录管理系统运行不了,希望高手指点指点!!
#include <iostream>#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
class record
{
public:
record();
char inter_face();
void add_person();
void show_person();
void alter();
void del_person();
void select();
void sort_person();
void save_new();
private:
string name;
string number;
string email;
string qq;
string addr;
string post_number;
}
record::record()
{
name="\0";
number="\0";
email="\0";
qq="\0";
addr="\0";
post number="\0";
}
char record::inter_face()//首页
{
system("class");
cout<<" ※ ※ ※ ※ ※ ※ ※ ※ ※ 通讯录管理系统 ※ ※ ※ ※ ※ ※ ※ ※ ※"<<endl<<endl;
cout<<" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = "<<endl;
cout<<" ☆ 电商091 ☆"<<endl<<endl;
cout<<" ************************************************************************"<<endl;
cout<<" * -------------- ※ 功能选择如下:※ ------------- *"<<endl<<endl;
cout<<" * 1、首页 *"<<endl;
cout<<" * 2、添加 *"<<endl;
cout<<" * 3、显示 *"<<endl;
cout<<" * 4、修改 *"<<endl; *"<<endl;
cout<<" * 5、删除 *"<<endl;
cout<<" * 6、查询 *"<<endl;
cout<<" * 7、排序 *"<<endl;
cout<<" * 8、保存 *"<<endl; *"<<endl;
cout<<" ************************************************************************"<<endl<<endl;
char choose;
cin>>choose;
return choose;
}
void record::add_person()//添加
{
cout<<endl<<"根据下面的提示输入联系人信息"<<endl<<endl
<<"姓名":";
cin>>name;
cout<<"电话:";
cin>>number;
cout<<"email:";
cin>>email;
cout<<"qq:";
cin>>qq;
cout<<"地址:";
cin>>addr;
cout<<"邮编:";
cin>>post number;
save_new();
cout<<endl<<endl<<"新联系人的信息已经保存好!!"<<endl<<endl;
}
void record::show_all()//显示
{
ifstream inData("record.txt",ios::init);
if (!inData)
{
cout<<endl<<"对不起! 没有找到文件!!"<<endl;
return;
}
bool flag=ture;
string record;
while (getline(inData,record))
{
if(flag)
{
cout<<endl<<"所有联系人信息如下:"<<endl;
}
cout<<record<<endl;
flag=false;
}
if(flag)
{
cout<<endl<<"通讯录中没有联系人!!"<<endl<<endl;
}
else
{
cout<<endl<<"所有联系人已经显示出来!!"<<endl<<endl;
}
}
void record::alter()//修改
{
ofstream outData("temp.txt",ios::out");
ifstream inData ("a.txt",ios::init);
if(!outData||!inData)
{
cout<<endl<<endl"对不起! 没有找到文件!!"<<endl;
return;
}
string sign;
cout<<endl<<"你要修改的姓名或电话号码:";
cin>>sign;
string strl;
bool flag=ture;
string str;
while(inData>>name>>number)
{
getline(inData,str);
if ((sign==name)||(sign==number))
{
cout<<endl<<"你想修改的联系人:"<<endl<<endl;
cout<<strl<<endl;
cout<<setionsflags(ios::left)<<setw(10)"<<name<<" "<<number<<str<<endl;
cout<<endl<<"请根据下面的提示修改信息:"<<endl;
cout<<"姓名":";
cin>>name;
cout<<"电话:";
cin>>number;
cout<<"email:";
cin>>email;
cout<<"qq:";
cin>>qq;
cout<<"地址:";
cin>>addr;
cout<<"邮编:";
cin>>post number;
save_new();
flag=false;
break;
}
outData setionsflag(ios::left)<<setw(10)<<name
<<" "<<number<<str<<endl;
}
if(flag)
{
cout<<endl<<"通讯录中没有联系人!!"<<endl<<endl;
}
else
{
while (getline(inData,str))
{
outData<<str<<endl;
}
outData.close();
inData.close();
ofstream out("a.txt",ios::out);
ifstream in("temp.txt",ios::init);
if(!out||!in)
{
cout<<endl<<"对不起!不能打开文件!!"<<endl<<endl;
return;
}
while (getline(in,str))
{
out<<str<<endl;
}
out.close();
in.close();
cout<<endl<<"这个人已在你的通讯录中修改!!"<<endl<<endl;
}
}
void delect()//删除
{
int i;
char name2[20];
cout<<"请输入想要删除的姓名:"<<endl;
cin>>name2;
for(i=0;i<=n;i++)
{
if(strcmp(record[i].name,name2)!=0)
continue;
else
{
for(int j=i;j<n-1;j++)
record[j]=record[j+1];
n--;
cout<<"已删除该姓名资料"<<endl;
}
break;
}
if(i>n)
{cout<<"不存在该姓名,请重新输入"<<endl;}
}
void record::select()//查找
{
ifstream inData("record.txt",ios::in);
if (!inData)
{
cout<<endl<<"对不起! 没有找到文件!!"<<endl;
return;
}
string sign;
cout<<endl<<"输入你想找的联系人姓名或电话:"<<;
cin>>sign;
}
string str1;
bool flag=true;
string str;
while (inData>>name>>number)
{
getline(inData,str);
if (name==sign)||(number==sign))
{
cout<<endl<<"你要查找的联系人是:"<<endl<<endl;
cout<<str1<<endl;
cout<<setionflags(ios::left)<<setw(10)<<name
<<number<<str<<endl;
flag=false;
break;
}
}
}
void sort() //排序
{
cout<<"<1>按姓名排序"<<endl;
cout<<"<2>按电话排序"<<endl;
cout<<"<3>按家庭地址排序"<<endl;
cout<<"请选择排序方式:";
int select3;
cin>>select3;
switch(select3)
{
case 1://按姓名排序
{
int i,j; //j是趟数
records t; //t为某一空格
for(j=0;j<n-1;j++)
for(i=0;i<n-j-1;i++)
if(record[i].name>record[i+1].name)
{t=record[i];record[i]=record[i+1];record[i+1]=t;}
{ cout<<" 姓名 电话 家庭地址 邮编 E_mail QQ"<<endl;}
for(i=0;i<n;i++)
{
cout<<setw(4)<<record[i].name<<" "<<setw(4)<<record[i].telephone<<" "<<setw(4)<<record[i].addr<<" "<<setw(4)<<record[i].post_num<<" "<<setw(4)<<record[i].E_mail<<" "<<setw(4)<<record[i].QQ<<endl;
}
}
break;
case 2://按电话排序
{
int i,j; //j是趟数
records t; //t为某一空格
for(j=0;j<n-1;j++)
for(i=0;i<n-j-1;i++)
if(record[i].telephone>record[i+1].telephone)
{t=record[i];record[i]=record[i+1];record[i+1]=t;}
{ cout<<" 姓名 电话 家庭地址 邮编 E-mail QQ"<<endl;}
for(i=0;i<n;i++)
{
cout<<setw(4)<<record[i].name<<" "<<setw(4)<<record[i].telephone<<" "<<setw(4)<<record[i].addr<<" "<<setw(4)<<record[i].post_num<<" "<<setw(4)<<record[i].E_mail<<" "<<setw(4)<<record[i].QQ<<endl;
}
}
break;
case 3://按家庭地址排序
{
int i,j; //j是趟数
records t; //t为某一空格
for(j=0;j<n-1;j++)
for(i=0;i<n-j-1;i++)
if(record[i].addr>record[i+1].addr)
{t=record[i];record[i]=record[i+1];record[i+1]=t;}
{ cout<<" 姓名 电话 家庭地址 邮编 E-mail QQ"<<endl;}
for(i=0;i<n;i++)
{
cout<<setw(4)<<record[i].name<<" "<<setw(4)<<record[i].telephone<<" "<<setw(4)<<record[i].addr<<" "<<setw(4)<<record[i].post_num<<" "<<setw(4)<<record[i].E_mail<<" "<<setw(4)<<record[i].QQ<<endl;
}
}
}
}
void record::save_new ()//保存
{
ofstream outData("a.txt",ios::app);
if(!outData)
{
cout<<endl<<"对不起!文件打不开!!"<<endl<<endl;
return;
}
outData sationsflags(ios::left)<<setw(10)<<name<<" "<<setw(8)<<number<<" "<<setw(10)
<<email<<" "<<setw(10)<<qq<<" "<<setw(10)<<" "<<addr<<setw(10)<<" "<<
post number<<endl;
}
outData.close();
}
enum power{add="1", show="2",delect="3",select="4",sort="5",save="6",end="0"};
int main()
{
char choose()
record ab;
while (choos=ab.inter_face())
{
switch(choose)
{
case add:
ab.add_person();
break;
case show:
ab.show_person();
break;
case del:
ab.del_person();
break;
case alter:
ab.alter_person();
break;
case select:
ab.select_person();
break;
case save:
ab.save_person();
break;
case end:
cout<<endl<<"通讯录管理系统"<<endl<<endl;
exit(0);
break;
dafault:
break;
}
}
return 0;
}