不可以啊~~能不能帮我看看这段程序是哪出错了么?怎么就是无法连名字带等级之类都存进去呢?
#include<iostream.h>
#include<fstream.h>
#include<string.h>
class Human
{
char Name[20];
int LV,HP;
public:
Human()
{
LV=1,HP=100;
}
void GetName(char *na)
{ strcpy(Name,na);}
void ShowState()
{
cout<<"角色名:"<<Name<<endl;
cout<<"等级: "<<LV<<endl;
cout<<"生命: "<<HP<<endl;
}
}player;
void Save()
{
fstream file("save.dat",ios::out|ios::binary);
if(!file)
{
cout<<"存档出错"<<endl;
}
file.write((char*)&player,sizeof(player));
file.close();
}
void Load()
{
fstream file;
file.open("save.dat",ios::in|ios::binary|ios::nocreate);
if(!file)
{
cout<<"读档出错"<<endl;
}
file.read((char*)&player,sizeof(player));
file.close();
}
void main()
{
Human player;
char name[20];
cout<<"请输入主角的名字:";
cin>>name;
player.GetName(name);
int i=0;
while(i<4)
{
cout<<"请问您需要存档还是读档? 0:Check 1:存档 2:读档 3.改名 4.Exit"<<endl;
cin>>i;
switch(i)
{
case 0: player.ShowState();break;
case 1: Save();break;
case 2: Load();break;
case 3:
cout<<"请输入主角的名字:";
cin>>name;
player.GetName(name);
break;
default:
cout<<"退出";
}
}
player.ShowState();
}