| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 286 人关注过本帖
标题:问题啊,大侠请教
只看楼主 加入收藏
mikejohn007
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-7-11
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
问题啊,大侠请教
这是我的通讯录代码,能够运行成功,但为什么每次推、退出后再进,前面的数据就没有了,谁来帮下忙啊!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct
     {
         char score;                /*编号*/
         char name[10];             /*姓名*/
         char num[15];              /*号码*/
         char adds[20];                 /*住址*/
     }Person;

Person pe[80];                        
int menu_select()
     {
         char c;
         do{
         system("color 07");
         system("cls");
         printf("\t\t *****通讯录***** \n");                              
         printf("\t\t┌———————┐\n");                  
         printf("\t\t│ 1. 添加记录   │\n");
         printf("\t\t│ 2. 显示记录   │\n");
         printf("\t\t│ 3. 删除记录   │\n");
         printf("\t\t│ 4. 查询记录   │\n");
         printf("\t\t│ 5. 修改记录   │\n");
         printf("\t\t│ 6. 保存记录   │\n");
         printf("\t\t│ 0. 退出程序   │\n");
         printf("\t\t└———————┘\n");
         printf("\t\t请您选择(0-6):");
         c=getchar();  
         }while(c<'0'||c>'6');
         return(c-'0');
     }

int Input(Person per[],int n)
{
     int i=0;
     char sign,x[10];                     
     while(sign!='n'&&sign!='N')
     {
         printf("\t编号:");
         scanf("\t%d",&per[n+i].score);
         printf("\t姓名:");
         scanf("\t%s",per[n+i].name);
         printf("\t电话号码:");
         scanf("\t%s",per[n+i].num);
         printf("\t通讯住址:");
         scanf("\t%s",per[n+i].adds);
         gets(x);   
         printf("\n\t是否继续添加?(Y/N)");
         scanf("\t%c",&sign);
         i++;
     }
     return(n+i);
}

void Display(Person per[],int n)                                         
{
     int i;
     printf("----------------------------------------------------------------------\n");                           /*格式*/
     printf("编号   姓名     电话号码      通讯地址\n");
     printf("----------------------------------------------------------------------\n");
     for(i=1;i<n+1;i++)                                                            
     {
         printf("%-5d%-8s%-13s%-15s%\n",per[i-1].score,per[i-1].name,per[i-1].num,per[i-1].adds);
         if(i>1&&i%10==0)                                                         
         {
             printf("\t-----------------------------------\n");
             printf("\t");
             system("pause");
             printf("\t-----------------------------------\n");
         }
     }
     printf("----------------------------------------------------------------------\n");
     system("pause");
}



int Delete_a_record(Person per[],int n)
{
     char s[20];
     int i=0,j;
     printf("\t请输入想删除记录中的名字:");
     scanf("%s",s);
     while(strcmp(per[i].name,s)!=0&&i<n) i++;
     if(i==n)
     {
         printf("\t通讯录中没有此人!\n");
         return(n);
     }
     for(j=i;j<n-1;j++)
     {
         strcpy(per[j].num,per[j+1].num);
         strcpy(per[j].name,per[j+1].name);
         strcpy(per[j].adds,per[j+1].adds);
         per[j].score=per[j+1].score;
     }
     printf("\t\t\t已经成功删除!\n");
     return(n-1);
}

void Query_a_record(Person per[],int n)
{
   
     int m;
     printf("\t\n请选择查询方式:\n");
     printf("\t┌——————┐\n");
     printf("\t│1------姓名 │\n");
     printf("\t│2------电话 │\n");
     printf("\t│3------地址 │\n");
     printf("\t│4------返回 │\n");
     printf("\t└——————┘\n");
     printf("请选择:");
     scanf("%d",&m);
     while(m!=1&&m!=2&&m!=3&&m!=4)
     {
         printf("输入错误,请重新选择:");
         scanf("%d",&m);
     }

     if(m==1)
     {
         char s[20];
         int i=0;
         printf("\t请输入想查询的姓名:");
         scanf("\t%s",s);
         while(strcmp(per[i].name,s)!=0&&i<n) i++;
         if(i==n)
         {
             printf("\t通讯录中没有此人!\n");
             return;
         }
         printf("\t此人编号: %d\n",per[i].score);
         printf("\t电话号码: %s\n",per[i].num);
         printf("\t通讯地址: %s\n",per[i].adds);
     } ;

     if(m==2)
     {
         char s[20];
         int i=0;
         printf("\t请输入想查询的电话:");
         scanf("\t%s",s);
         while(strcmp(per[i].num,s)!=0&&i<n) i++;
         if(i==n)
         {
             printf("\t通讯录中没有此人!\n");
             return;

         }
         printf("\t此人编号: %d\n",per[i].score);
         printf("\t此人姓名: %s\n",per[i].name);
         printf("\t通讯地址: %s\n",per[i].adds);
     } ;

     if(m==3)
     {
         char s[20];
         int i=0;
         printf("\t请输入想查询的地址:");
         scanf("\t%s",s);
         while(strcmp(per[i].adds,s)!=0&&i<n) i++;
         if(i==n)
         {
             printf("\t通讯录中没有此人!\n");
             return;
         }
         printf("\t此人编号: %d\n",per[i].score);
         printf("\t此人姓名: %s\n",per[i].name);
         printf("\t电话号码: %s\n",per[i].num);

     } ;
}


void Change(Person per[],int n)
{
     char s[20];
     int i=0;
     printf("\t请输入想修改的记录中的名字:");
     scanf("%s",s);
     while(strcmp(per[i].name,s)!=0&&i<n) i++;
     if(i==n)
     {
         printf("\t通讯录中没有此人!\n");
         return;
     }
     printf("\t编号:");
     scanf("\t%d",&per[i].score);
     printf("\t姓名:");
     scanf("\t%s",per[i].name);
     printf("\t电话号码:");
     scanf("\t%s",per[i].num);
     printf("\t通讯住址:");
     printf("\t修改成功!");
}


void WritetoText(Person per[],int n)            
{
     int i=0;
     FILE *fp;                                            /*定义文件指针*/
     char filename[20];                                   /*定义文件名*/
     printf("\t保存到文件\n");                           /*输入文件名*/
     printf("\t请输入所保存的文件名:");
     scanf("\t%s",filename);
     if((fp=fopen(filename,"w"))==NULL)                  
     {
         printf("\t无法打开文件\n");
         system("pause");
         return;
     }
     fprintf(fp,"******************************************通讯录******************************************\n");
     fprintf(fp,"编号      姓名       电话号码            通讯地址\n");
     fprintf(fp,"------------------------------------------------------------------------------------------\n");
     while(i<n)

     {
         fprintf(fp,"%-3d\t%-6s\t%-13s\t%-20s\t%-20s\n",per[i].score,per[i].name,per[i].num,per[i].adds);
         i++;
     }
     fprintf(fp,"------------------------------------------------------------------------------------------\n");
     fprintf(fp,"***************************************共有%d条记录****************************************\n",n);
     fclose(fp);                                          /*关闭文件*/
     printf("保存成功!\n");
}

void main()                                          /*主函数*/
{
     int n=0;
     for(;;)
     {
         switch(menu_select())
         {
         case 1:
         printf("\n\t添加记录到通讯录\n");                          /*添加记录*/
         n=Input(pe,n);
         break;

         case 2:
         printf("\n\t\t\t     通讯录记录表\n");                       /*显示记录*/
         Display(pe,n);
         break;
        
         case 3:
         printf("\n\t从通讯录中删除记录\n");
         n=Delete_a_record(pe,n);                                   /*删除记录*/
         printf("\t");
         system("pause");
         break;

         case 4:
         printf("\n\t在通讯录中查找记录\n");
         Query_a_record(pe,n);                                       /*查找记录*/
         printf("\t");
         system("pause");
         break;
        
         case 5:
         printf("\n\t修改通讯录中的记录\n");
         Change(pe,n);                                              /*修改数据*/
         printf("\t");
         system("pause");
         break;

         case 6:
         printf("\n\t保存功能\n");
         WritetoText(pe,n);                                          /*保存数据*/
         printf("\t");
         system("pause");
         break;

         case 0:
         printf("\n\t\t谢谢使用,再见!\n");                          /*结束程序*/
         printf("\n\t\t");
         system("pause");
         exit(0);
         }
     }
}

 
搜索更多相关主题的帖子: include 通讯录 system 姓名 
2010-07-11 19:14
sunyh1999
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:14
帖 子:1178
专家分:3032
注 册:2009-5-17
收藏
得分:10 
这个程序,一看就有问题,你想想你有保存记录,但是没有加载记录,计算机怎么读取数据呢?你应该再写一个文件打开操作函数,读取修改数据就可以了

欢迎来到我的博客:http://blog..cn/noisunyuhong
2010-07-11 19:51
zisefengye
Rank: 5Rank: 5
等 级:职业侠客
帖 子:167
专家分:386
注 册:2010-6-27
收藏
得分:10 
//personal.h
#ifndef PERSONAL
#define PERSONAL

#include <fstream>
#include <string>
using namespace std;
class Personal
{
public:
    Personal();
    Personal(char*, char*, char*, int, long);
    void writeToFile(fstream&) const;
    void readFromFile(fstream&);
    void readKey();
    int size() const
    {
        return 9 + nameLen + cityLen + sizeof(year) + sizeof(salary);
    }
    bool operator==(const Personal& pr) const
    {
        return strcmp(pr.SSN, SSN) == 0;
    }
protected:
    const int nameLen, cityLen;
    char SSN[10], *name, *city;
    int year;
    long salary;
    ostream& writeLegibly(ostream&);
    friend ostream& operator<<(ostream& out, Personal& pr)
    {
        return pr.writeLegibly(out);
    }
    istream& readFromConsole(istream&);
    friend istream& operator>>(istream& in, Personal& pr)
    {
        return pr.readFromConsole(in);
    }
};

//personal.cpp
#endif
#include "personal.h"
#include <iostream>
using namespace std;

Personal::Personal() : nameLen(10), cityLen(10)
{
    name = new char[nameLen + 1];
    city = new char[cityLen + 1];
}

Personal::Personal(char *ssn, char *n, char *c, int y, long s) : nameLen(10), cityLen(10)
{
    name = new char[nameLen + 1];
    city = new char[cityLen + 1];
    strcpy(SSN, ssn);
    strcpy(name, n);
    strcpy(city, c);
    year = y;
    salary = s;
}

void Personal::writeToFile(fstream &out) const
{
    out.write(SSN, 9);
    out.write(name, nameLen);
    out.write(city, cityLen);
    out.write(reinterpret_cast<const char*>(&year), sizeof(int));
    out.write(reinterpret_cast<const char*>(&salary), sizeof(int));
}

void Personal::readFromFile(fstream &in)
{
    in.read(SSN, 9);
    in.read(name, nameLen);
    in.read(city, cityLen);
    in.read(reinterpret_cast<char*>(&year), sizeof(int));
    in.read(reinterpret_cast<char*>(&salary), sizeof(int));
}

void Personal::readKey()
{
    char s[80];
    cout<<"Enter SSN:";
    cin.getline(s, 80);
    strncpy(SSN, s, 9);
}

ostream& Personal::writeLegibly(ostream &out)
{
    SSN[9] = name[nameLen] = city[cityLen] = '\0';
    out<<"SSN = "<<SSN<<", name = " <<name
        <<", city = "<<city<<", year = "<<year
        <<", salary = "<<salary;
    return out;

}

istream& Personal::readFromConsole(istream &in)
{
    char s[80];
    cout<<"SSN: ";
    in.getline(s, 80);
    strncpy(SSN, s, 9);
    cout<<"Name: ";
    in.getline(s, 80);
    strncpy(name, s, nameLen);
    cout<<"City: ";
    in.getline(s, 80);
    strncpy(city, s, cityLen);
    cout<<"Birthyear: ";
    in>>year;
    cout<<"Salary: ";
    in>>salary;
    in.getline(s, 80);
    return in;
}
//Database.h 摸板比较特殊,实现方法也放在头文件里
#include <iostream>
using namespace std;

template<class T>
class Database
{
public:
    Database();
    void run();
private:
    fstream database;
    char fName[20];
    ostream& print(ostream&);
    void add(T&);
    bool find(const T&);
    void modify(const T&);
    void Show(T&);
    friend ostream& operator<<(ostream& out, Database& db)
    {
        return db.print(out);
    }
};
template<class T>
Database<T>::Database()
{
    cout<<"File name: ";//输入已存在的文件,如f:\personal.txt
    cin>>fName;
}

template<class T>
void Database<T>::add(T &d)
{
    database.open(fName, ios::in | ios::out | ios::binary);
    database.seekp(0, ios::end);
    d.writeToFile(database);
    database.close();
}

template<class T>
void Database<T>::modify(const T &d)
{
    T tmp;
    database.open(fName, ios::in | ios::out | ios::binary);
    database.seekg(ios::beg);
    while(!database.eof())
    {
        tmp.readFromFile(database);
        if(tmp == d)
        {
            cin>>tmp;
            database.seekp(-d.size(), ios::cur);
            tmp.writeToFile(database);
            database.close();
            return;
        }
    }
    database.close();
    cout<<"The record to be modified is not in the database\n";
}

template<class T>
bool Database<T>::find(const T &d)
{
    T tmp;
    database.open(fName, ios::in | ios::binary);
    database.seekg(ios::beg);
    while(!database.eof())
    {
        tmp.readFromFile(database);
        if(tmp == d)
        {
            database.close();
            return true;
        }
    }
    database.close();
    return false;
}

template<class T>
ostream& Database<T>::print(ostream &out)
{
    T tmp;
    database.open(fName, ios::in | ios::binary);
    database.seekg(ios::beg);
    while(1)
    {
        tmp.readFromFile(database);
        if(database.eof())
            break;
        cout<<tmp<<endl;
    }
   
    database.close();
    return out;
}


template<class T>
void Database<T>::Show(T &d)
{
    T tmp;
    database.open(fName, ios::in | ios::binary);
    database.seekg(ios::beg);
    while(1)
    {
        tmp.readFromFile(database);
        if(database.eof())
            break;
        if(tmp == d)
        {
            cout<<tmp<<endl;
        }
    }
    database.close();
}

template<class T>
void Database<T>::run()
{
    char option[5];
    T rec;
    cout<<"1. Add 2. Find 3. Modify a record 4. Show all record 5. Exit\n";
    cout<<"Enter an option: ";
    cin.getline(option, 4);
    while(cin.getline(option, 4))
    {
        if(*option == '1')
        {
            cin>>rec;
            add(rec);
        }
        else if(*option == '2')
        {
            rec.readKey();
            cout<<"The record is ";
            if(find(rec) == true)
            {
                cout<<"in the database\n";
                Show(rec);
            }
            else
                cout<<" not in the database\n";

        }
        else if(*option == '3')
        {
            rec.readKey();
            modify(rec);
        }
        else if(*option == '4')
        {
            //cout<<*this;
            print(cout);

        }
        else if(*option != '5')
        {
            cout<<"Wrong option\n";
        }
        else
            return;
        //cout<<*this;
        cout<<"Enter an option:";
    }
}
//主函数
#include "personal.h"
#include "database.h"
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
    Database<Personal> db;
    db.run();
    getchar();
}
2010-07-11 20:20
快速回复:问题啊,大侠请教
数据加载中...
 
   



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

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