诚请高手讲解一下这个程序的错误 新手上路
// telemanage.cpp : 定义控制台应用程序的入口点。//
#include "stdafx.h"
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
//--------------------------------------------------------------------------------
typedef struct
{
string name;//姓名
string telephonenum;//电话号码
}TouchedPerson;
//------------------------------------------------------------------------------
//添加联系人
void AddTelenum(void)
{
FILE *telephonebook;//文件指针
TouchedPerson *people=NULL;
if( (telephonebook = fopen("通讯录.txt","a+"))==NULL )
{
cout<<"文件无法打开!"<<endl;
exit(0);
}
people = new TouchedPerson;
if( people = NULL )
{
cout<<"内存分配失败!"<<endl;
exit(0);
}
cout<<"输入联系人姓名:";
cin>>people->name;
getchar();
cout<<"联系人电话:";
cin>>people->telephonenum;
string choice;
cout<<"是否保存该人?"<<endl;
cout<<"是请按Y或y,否按其他任意键:";
cin>>choice;
if( !((choice=="Y") || (choice=="y")) )
{
if( fwrite(people, sizeof(TouchedPerson), 1, telephonebook) !=1 )
cout<<"读入信息失败!"<<endl;
else
cout<<"信息保存成功!"<<endl;
}
else
cout<<"此人信息未存入通信录!"<<endl;
delete people;//释放指针
fclose(telephonebook);
}//----------------------------------------------------------------------------
//查找联系人
void SearchPeople(void)
{
FILE *telephonebook;//文件指针
TouchedPerson *people=NULL;
if((telephonebook = fopen("通讯录.txt","a+"))==NULL)
{
cout<<"文件无法打开!"<<endl;
exit(0);
}
people = new TouchedPerson;
if( people = NULL )
{
cout<<"内存分配失败!"<<endl;
exit(0);
}
string tempname;//待查找联系人的姓名
cout<<"输入联系人姓名:";
cin>>tempname;
while( fread(people, sizeof(TouchedPerson), 1, telephonebook) )
{
if( !(tempname == people->name) )
cout<<people->name<<" "<<people->telephonenum<<endl;
else
cout<<"不存在该联系人!"<<endl;
}
delete people;//释放指针
fclose(telephonebook);
}//----------------------------------------------------------------------------------------------
//显示所有联系人
void ShowAllPeople(void)
{
FILE *telephonebook;//文件指针
TouchedPerson *people=NULL;
if((telephonebook = fopen("通讯录.txt","a+"))==NULL)
{
cout<<"文件无法打开!"<<endl;
exit(0);
}
people = new TouchedPerson;
if( people = NULL )
{
cout<<"内存分配失败!"<<endl;
exit(0);
}
while( fread(people, sizeof(TouchedPerson), 1, telephonebook) )
{
cout<<"姓名:"<<people->name<<" "<<"电话:"<<people->telephonenum<<endl;
}
delete people;//释放指针
fclose(telephonebook);
}//-----------------------------------------------------------------------------------------
//导航菜单
int menuchoice;//导航变量
void Menu(void)
{
cout<<"**********欢迎使用通信录管理程序**********"<<endl;
cout<<"(1)**********添加联系人!"<<endl;
cout<<"(2)********显示当前通讯录!"<<endl;
cout<<"(3)**********查找联系人!"<<endl;
cout<<"(0)**********退出本程序!"<<endl;
cout<<"请选择:";
cin>>menuchoice;
}
int _tmain(int argc, _TCHAR* argv[])
{
while( 1 )
{
Menu();
switch( menuchoice )
{
case 1:
AddTelenum();
system("pause");
system("cls");
break;
case 2:
ShowAllPeople();
system("pause");
system("cls");
break;
case 3:
SearchPeople();
system("pause");
system("cls");
break;
case 0:
cout<<"谢谢使用!"<<endl;
exit(0);
default:
cout<<"指令错误!"<<endl;
}
return 0;
}
}