• 全部
  • 问答

C++程序,请指导。在线等

paddy102 2003-11-02 10:41:12
问题在于:error1: 输入一条后,用brower却显示出两条相同记录
error2: 删除不能执行
error3: 怎么实现“退出程序后真接返回dos界面”

如果你有什么好的想法请完善这个程序,让我学习。 我是个菜鸟!

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class telephone
{
public:
telephone();
~telephone();
void initialize();
void insert();
void search();
void del();
void browser();
private:
string name;
string phone;
int count;
};

telephone::telephone()
{
count = 0;
}

telephone::~telephone()
{
}

void telephone::initialize()
{
for(;;)
{
char c;
cout<<" "<<"* * * * * * * * * * * * * * 电 话 薄 模 拟 程 序 * * ";
cout<<"* * * * * * * * * * * * * "<<endl;
cout<<endl;
cout<<" "<<"阅读电话薄(B)"<<'\t'<<"加入电话薄(I)"<<'\t'<<"查找电话(F)"<<'\t'
<<"删除电话(D)"<<'\t'<<"退出(X)"<<endl;
cout<<endl;
cout<<"Please press B | I | F | D | X for choice:"<<endl;
cin>>c;
switch(c)
{
case 'B':
case 'b':
cout<<"You have choosed B(阅读电话薄)"<<endl;
browser();
break;
case 'I':
case 'i':
cout<<"You have choosed I(加入电话薄)"<<endl;
insert();
break;
case 'F':
case 'f':
cout<<"You have choosed F(查找电话)"<<endl;
search();
break;
case 'D':
case 'd':
cout<<"You have choosed D(删除电话)"<<endl;
del();
break;
case 'X':
case 'x':
cout<<"You have choosed X(退出程序)"<<endl;
return;
break;
default:
cout<<"你没按要求输入,请看看提示后再试!"<<endl;
}
}
}


void telephone::insert()
{
string strName,strPhone;
cout<<"Enter the name of the new:\n";
cin>>strName;
cout<<"Then the phone:\n";
cin>>strPhone;

ofstream fout("telephoneBook.txt",ios::app);
fout<<'\t'<<strName<<'\t'<<strPhone<<endl;
count++;
cout<<"sum recorded:\n"<<count;
cout<<"Ok,it has finished!"<<endl;
}

void telephone::search()
{
string strName="";
string strPhone="";
char c;
cout<<"Search by name or by phone?\n";
cout<<"Press one key, \'1\'for name, \'2\'for phone:\n";
cin>>c;
if (c == '1')
{
cout<<"Enter the name:\n";
cin>>strName;
}
if (c == '2')
{
cout<<"Enter the phone:\n";
cin>>strPhone;
}
ifstream fin("telephoneBook.txt",ios::in);
while (!fin.eof())
{
fin>>name>>phone;
if (strName==name || strPhone==phone)
{
cout<<"The following is the result:\n";
cout<<'\t'<<"Name: "<<name<<'\t'<<"Phone: "<<phone<<endl;
return;
}
}
cout<<"Sorry,can not find you want!"<<endl;
}

void telephone::browser()
{
//cout<<"There are total "<<count<<" items in this telephone book."<<endl;
ifstream fin("telephoneBook.txt",ios::in);
int i = 0;
while (!fin)
{cout<<"No record in file\n";
return ;
}
while (!fin.eof()) // error 1
{
fin>>name>>phone;
cout<<'\t'<<i++<<". Name: "<<name<<" Phone: "<<phone<<endl;

}
}

void telephone::del()
{
string strName,strPhone;
char c;
int flag = 0;
cout<<"Delete by name or by phone?\n";
cout<<"Press one key, \'1\'for name, \'2\'for phone:"<<endl;
cin>>c;
if (c == '1')
{
cout<<"Enter the name you want to delete:\n";
cin>>strName;
}
if (c == '2')
{
cout<<"Enter the phone you want to delete:\n";
cin>>strPhone;
}
ifstream fin("telephoneBook.txt",ios::in|ios::out);
ostream out(fin.rdbuf()); //基本流缓冲区的输出流
while (!fin.eof())
{
fin>>name>>phone;
if (strName==name || strPhone==phone)
{
cout<<"You want to delete item, Name: "<<name<<" Phone: "<<phone
<<", Really? "<<" Press(Y/N):\n"<<endl;
cin>>c;
if (c == 'Y' || c == 'y')
{
name = "";
phone = "";
flag = 1;
}
if (c == 'N' || c == 'n')
{
flag = 0;
//out<<'\t'<<phone<<'\t'<<name<<endl;
}
}
else
out<<'\t'<<phone<<'\t'<<name<<endl;
}
if (flag == 1)
cout<<"Ok,it has been deleted."<<endl;
else
cout<<"Sorry,can not find the item that you want to delete!"<<endl;
}


int main()
{
telephone tel;
tel.initialize();
return 0;
}












...全文
7 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Wolf0403 2003-11-02
肯定是学 C 的出身,呵呵。类的设计。。。我想用【一塌糊涂】来形容
回复
zhaochong12 2003-11-02
error1: 输入一条后,用brower却显示出两条相同记录
让它显示俩..
error2: 删除不能执行
不让删
error3: 怎么实现“退出程序后真接返回dos界面”
重新启动...

这个帖子一分没有,楼主还真的很强呀......这么长的程序,都懒得看了......
回复
相关推荐
发帖
C++ 语言
创建于2007-09-28

5.9w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
申请成为版主
帖子事件
创建了帖子
2003-11-02 10:41
社区公告
暂无公告