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;
}












...全文
36 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Wolf0403 2003-11-02
  • 打赏
  • 举报
回复
肯定是学 C 的出身,呵呵。类的设计。。。我想用【一塌糊涂】来形容
zhaochong12 2003-11-02
  • 打赏
  • 举报
回复
error1: 输入一条后,用brower却显示出两条相同记录
让它显示俩..
error2: 删除不能执行
不让删
error3: 怎么实现“退出程序后真接返回dos界面”
重新启动...

这个帖子一分没有,楼主还真的很强呀......这么长的程序,都懒得看了......
内容简介 《C++程序设计》作者深入调查了我国大学的程序设计课程的现状和发展趋势,参阅了国内外数十种有关C++的教材,认真分析了学习者在学习过程中遇到的困难,研究了初学者的认识规律。在本书中做到准确定位,合理取舍内容,设计了读者易于学习的教材体系,并且以通俗易懂的语言化解了许多复杂的概念,大大减少了初学者学习C++的困难。C++是近年来国内外广泛使用的现代计算机语言,它既支持面向过程的程序设计,也支持基于对象和面向对象的程序设计。国内许多高校已陆续开设了C++程序设计课程。但是,由于C++涉及概念很多,语法比较复杂,内容十分广泛,使不少人感到学习难度较大,难以入门。 考虑到许多读者在学习C++前未学过其他语言,本书把入门起点降低,读者不需要具备C语言的基础。本书包括14章,分为4篇:基本知识;面向过程的程序设计;基于对象的程序设计;面向对象的程序设计。本书内容全面,例题丰富,概念清晰,循序渐进,易于学习。 《C++程序设计》是依据ANSI C++标准进行介绍的,引导读者从一开始就按标准C++的规定编程。本书配有两本辅导教材,即《C++程序设计题解与上机指导》和《C++编程实践指导》。 C++程序设计》是大学各专业学生学习C++的基础教材,也是适于初学者学习的教材。即使没有教师讲授,读者也能看懂本书的大部分内容。

64,439

社区成员

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

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