| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 867 人关注过本帖
标题:怎么删除对象的内容重新给个值
取消只看楼主 加入收藏
wskiawv
Rank: 1
等 级:新手上路
帖 子:42
专家分:5
注 册:2008-6-28
结帖率:33.33%
收藏
 问题点数:0 回复次数:0 
怎么删除对象的内容重新给个值
先给大家举例说明一下先
#include<iostream>
using namespace std;

#include<cstdlib>
#include<fstream>
#include<cstring>
#include<conio.h>
//函数声明
void InputData();
void OutputData();
void locate();
void modify();
void del();
int i;//循环变量
char n[6];//学号
class student
{
private:
    char num[6],name[16];
    double chi,math,eng,sum,avg;
public:
    student(double c=0,double m=0,double e=0)
    {
        chi=c;
        math=m;
        eng=e;
    }
   void input()
   {
       cout<<"请输入学号,姓名和语文,数学,英语成绩(请按所提示的顺序输入):"<<endl;
       cin>>num>>name;
       cin>>chi;
       while(cin.fail())
       {
           cout<<"你输入的不是数字!"<<endl;
           cin.clear();
           cin.sync();
           cin>>chi;
       }
       cin>>math;
       while(cin.fail())
       {
           cout<<"你输入的不是数字!"<<endl;
           cin.clear();
           cin.sync();
           cin>>math;
       }
       cin>>eng;
       while(cin.fail())
       {
           cout<<"你输入的不是数字!"<<endl;
           cin.clear();
           cin.sync();
           cin>>eng;
       }
       sum=chi+math+eng;
       avg=sum/3;       
   }
   void ouput()
   {  
       cout<<"--------------------------------------------------------------------------------"<<endl;
       cout<<"学号:"<<num<<"  姓名:"<<name<<"  成绩:  ";
       cout<<"语文:"<<chi<<"  数学:"<<math<<"  英语:"<<eng;
       cout<<"  总分:"<<sum<<"  平均分:"<<avg<<endl;
       cout<<"--------------------------------------------------------------------------------"<<endl;
    
   }
  char *GetNum()
   {
      return num;
   }
};
student s1;//对象
int main()
{
    char x;
    while(true)
    {
        system("cls");
        cout<<"                 ***********************************************"<<endl;
        cout<<"                              学生成绩管理系统"<<endl;
        cout<<"                 ***********************************************"<<endl;
        cout<<endl;
        cout<<"                               1--数据输入"<<endl;
        cout<<"                               2--数据输出"<<endl;
        cout<<"                               3--数据查询"<<endl;
        cout<<"                               4--数据修改"<<endl;
        cout<<"                               5--删除学生"<<endl;
        cout<<"                               0--退出"<<endl;    
        cout<<"请从(0--5)选择:"<<endl;    
        cin>>x;    
        if(x=='0'||x=='1'||x=='2'||x=='3'||x=='4'||x=='5')
        {
            if(x=='1')
                InputData();
            if(x=='2')
                OutputData();
            if(x=='3')
                locate();
            if(x=='4')
                modify();
            if(x=='5')
                del();
            if(x=='0')
            {
                cout<<"退出!"<<endl;
                exit(1);
            }
        }
        else
            cout<<"你输入有误请重新选择!"<<endl;
        getch();
    }
    return 0;
}
void InputData()//输入数据函数
{
    char x;
    ofstream outf;//输出流对象
    outf.open("D:\\grade.dat",ios::out|ios::binary|ios::app);
    if(!outf)
    {
        cout<<"打开文件失败!"<<endl;
    }
    for(i=0;true;i++){
        s1.input();
        outf.write((char *)&s1,sizeof s1);
        cout<<"是否(Y|N)还有其他学生信息的输入?"<<endl;
        cin>>x;
        if(x=='N'||x=='n')
            break;
    }
    cout<<endl;
    outf.close();
}
void OutputData()//输出数据函数
{
    ifstream inf;//输入流对象
    inf.open("D:\\grade.dat",ios::in|ios::binary);
    if(!inf)
    {
        cout<<"打开文件失败!"<<endl;
    }
    else{
        for(i=0;inf.read((char *)&s1,sizeof s1);i++){    
            s1.ouput();
        }
    }
    inf.close();
}
void locate()//查询函数
{
    ifstream inf;//输入流对象
    inf.open("D:\\grade.dat",ios::in|ios::binary);
    if(!inf)
        cout<<"打开文件失败!"<<endl;
    else{
        cout<<"请输入学号查询:";
         cin>>n;
        for(i=0;inf.read((char *)&s1,sizeof s1);i++){
        if(strcmp(s1.GetNum(),n)==0)
        {
            s1.ouput();
        }
        }
    }    
    if(strcmp(s1.GetNum(),n)!=0)
            cout<<"不存在!"<<endl;
    inf.close();
}
void modify()//修改数据函数
{
    fstream file_io;//输入输出流对象
    file_io.open("D:\\grade.dat",ios::in|ios::out|ios::binary|ios::app);
    if(!file_io)
        cout<<"打开文件失败!"<<endl;
    else
    {
        cout<<"请输入要修改学生的学号:"<<endl;
        cin>>n;
        for(i=0;file_io.read((char *)&s1,sizeof s1);i++){
        if(strcmp(s1.GetNum(),n)==0)
        {
            s1.input();
        }
        }
    }
    if(strcmp(n,s1.GetNum())!=0)
        cout<<"你查找的学生学号不存在!"<<endl;     
    file_io.close();
}
void del()//删除数据函数
{
    fstream dell;
    dell.open("D:grade.dat",ios::in|ios::out|ios::binary|ios::app);
    dell.close();
}
修改和删除函数我都还没有做,请各位指导指导!
搜索更多相关主题的帖子: 对象 删除 
2008-12-15 19:22
快速回复:怎么删除对象的内容重新给个值
数据加载中...
 
   



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

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