字典程序,但是还有一个小问题,希望大家帮帮小弟,先谢谢大家了。。。
#include "iostream.h"#include "fstream.h"
#include "string.h"
#define MAX 100
struct WORD
{
char english[10];
char chinese[20];
};
class dic
{
private:
WORD word[MAX];
public:
int n;
dic();
void input();
void check();
void alter();
void show();
};
dic::dic()
{
n=1;
}
void dic::input()
{
cout<<"english:";
cin>>word[n].english;
cout<<"chinese:";
cin>>word[n].chinese;
ofstream file("dictionary.dat",ios::out|ios::app);
file.write((char*)&word[n],sizeof(WORD));
n++;
}
void dic::check()
{
int j=1;
bool isfind=false;
char checkname[10];
cout<<"checkname;";
cin>>checkname;
ifstream checkfile("dictionary.dat");
if(!checkfile)cout<<"open error!";
while(checkfile.read((char*)&word[j],sizeof(WORD)))
{
if(strcmp(checkname,word[j].english)==0)
{
cout<<"chinese:"<<word[j].chinese;
isfind=true;
}
else j++;
}
if(!isfind)cout<<"没有找到!"<<endl;
}
void dic::alter()
{
int i;
char alterword[10],alterchinese[20];
bool isfind=false;
cout<<"请输入你想修改的单词:";
cin>>alterword;
ifstream checkfile("dictionary.dat");
if(!checkfile)cout<<"error!"<<endl;
while(checkfile.read((char*)&word[i],sizeof(WORD)))
{
if(strcmp(alterword,word[i].english)==0)
{
cout<<"你修改的单词中文意思:";
cin>>alterchinese;
strcpy(alterchinese,word[i].chinese);
isfind=true;
}
i++;
}
if(!isfind)cout<<"没有这个单词!"<<endl;
}
void dic::show()
{
int j=1;
ifstream infile("dictionary.dat");
if(!infile)cout<<"open error!"<<endl;
while(infile.read((char*)&word[j],sizeof(WORD)))
{
cout<<word[j].english<<word[j].chinese<<endl;
j++;
}
}
void main()
{
dic dictionary;
int N,m=1;
cout<<"...............字典..........................."<<endl;
cout<<"1..............输入单词......................."<<endl;
cout<<"2..............查找单词......................."<<endl;
cout<<"3..............修改单词......................."<<endl;
cout<<"4..............显示字典库....................."<<endl;
cout<<"5..............退出..........................."<<endl;
while(m)
{
cin>>N;
switch(N)
{
case 1: dictionary.input();break;
case 2: dictionary.check();break;
case 3: dictionary.alter();break;
case 4: dictionary.show();break;
case 5: m=0;break;
}
}
}
程序编译的时候没有问题,但是在运行的时候有这样的错误。运行入下:
1
english: go
chinese: 拜拜
1
english:work
chinese:工作
4
go拜拜
work工作
3
请输入你想修改的单词:go
你修改的单词中文意思:走
4
go拜拜
work工作
我们可以看到:我修改了go的中文意思,但是在显示的时候go的中文意思没有改变。我这点不清楚,为什么会这个样子呢?谢谢大家指点!