| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 610 人关注过本帖
标题:诚请高手讲解一下这个程序的错误 新手上路
只看楼主 加入收藏
龙合我亲笔
Rank: 2
等 级:论坛游民
帖 子:4
专家分:10
注 册:2010-6-6
收藏
 问题点数:0 回复次数:6 
诚请高手讲解一下这个程序的错误 新手上路
// 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
rainbow1
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:277
专家分:839
注 册:2010-5-19
收藏
得分:0 
不说明什么情况,别人是没法帮你查问题的。
2010-06-11 12:51
rainbow1
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:277
专家分:839
注 册:2010-5-19
收藏
得分:0 
先提出几个更改意见:
(我用的是DEV-C++)
1、把#include "stdafx.h"这一句注释掉,即:
  //#include "stdafx.h"
2、 if( people = NULL )为错误语句,应改为
 if( people == NULL )
“=”为赋值,而“==”则是用于比较,后面的两个子函数也是一样。
3、查找联系人和显示联系人当中,为什么是“a+”而不是“a”?难道查找联系人和显示联系人也需要更改文件吗?
4、当我把上面的几处更改后,程序或以正常编译。但写入的文件为二进制码文件,而不是ASC码,这个地方还需要更改。

2010-06-11 18:56
rainbow1
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:277
专家分:839
注 册:2010-5-19
收藏
得分: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 )
//    if ( !people )
    {
        cout<<"内存分配失败!"<<endl;
        exit(0);
    }

    cout<<"输入联系人姓名:";
    cin>>people->name;
    getchar();
    cout<<"联系人电话:";
    cin>>people->telephonenum;
//    cout << endl;
//    getchar();
   
    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 main(int argc, char* 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-11 18:59
迷失的木桶
Rank: 4
等 级:业余侠客
帖 子:52
专家分:230
注 册:2010-5-29
收藏
得分:0 
楼上是老师吗?很仔细啊,佩服。
2010-06-11 23:39
LQPhilosophy
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-6-21
收藏
得分:0 
请问一下这个程序实现什么的?
2010-06-21 10:12
南国利剑
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:29
帖 子:1165
专家分:3536
注 册:2010-4-12
收藏
得分:0 
NOT BAD!

南国利剑
2010-06-21 11:06
快速回复:诚请高手讲解一下这个程序的错误 新手上路
数据加载中...
 
   



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

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