| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 610 人关注过本帖
标题:诚请高手讲解一下这个程序的错误 新手上路
取消只看楼主 加入收藏
龙合我亲笔
Rank: 2
等 级:论坛游民
帖 子:4
专家分:10
注 册:2010-6-6
收藏
 问题点数:0 回复次数:0 
诚请高手讲解一下这个程序的错误 新手上路
// 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;
}

}
搜索更多相关主题的帖子: 讲解 
2010-06-08 18:57
快速回复:诚请高手讲解一下这个程序的错误 新手上路
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.047525 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved