| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3489 人关注过本帖
标题:来看看吧,如何 将 char 赋值给char*
只看楼主 加入收藏
woshihsz
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-9-2
结帖率:50%
收藏
 问题点数:0 回复次数:6 
来看看吧,如何 将 char 赋值给char*
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <conio.h>
#include <fstream>
#include <iostream>
using namespace std;



//*****定义一个学生原子的的数据结构*****//
struct stuatom
{
    char *name;
    int  id;
    char sex;
    char *address;
    char *date;
    char *cons;
    char *ps;
    void show();
    void setup();
};

//*********定义一系列对学生的操作**********//
class student
{
private:
    stuatom ob[100];
    int stulen;
public:
    student();
    void input();
    void save();
    void check();
    void clean();
    void query();
    void read();
    void add();
    void del();
    void modify();
};

//********对学生数据的初始化(类的构造函数)**********//
student::student()
{
    //用for循环对全部数组中的数据初始化
    for(int i=0;i <100;i++)
    {
        ob[i].id =0;
        ob[i].sex =' ';
        ob[i].name =NULL;
        ob[i].address=NULL;
        ob[i].date=NULL;
        ob[i].cons=NULL;
        ob[i].ps=NULL;
    }
    this->stulen =0;
}

//********输入学生的数据,并判断是否在规定数据域内*******//
void stuatom::setup()
{
    char n[20];
    char ad[20];
    char da[10];
    char cs[20];
    char ss[20];
    char s;
    int b;  
    do
    {   
        cout<<" 学号(1001与1010之间): ";
        cin>>b;
        if(b>1010||b <1001)
            cout<<" 数据有误!!重新输入.."<<endl<<endl;     else break;
    }while (b <1010||b>1001);

    id=b;
    do
    {
        name=new char[strlen(n)+1];
        cout<<" 姓名: ";
        cin>>n;
        
    }while ( strlen(n)>6 && strlen(n) <4 );         
   
    strcpy(name,n);
    cout<<" 性别(m/f):" ;
    cin>>s;
   
    //如果输入性别在数据域内,跳出循环并且赋值。
    //如果不在数据域内,一直循环到输入数据符合数据域为止
    while (s!='m' && s!='f')
    {
        cout<<" 数据有误!!重新输入.."<<endl<<endl;
        cout<<" 性别(m/f):";
        cin>>s;
    }
    sex=s;

    cout<<" 工作部门(0-20): ";
    cin>>ad;
    while (strlen(ad) <0 || strlen(ad)>20)
    {
        cout<<" 数据有误!!重新输入.."<<endl<<endl;
        cout<<" 工作部门(0-20): ";
        cin>>ad;
    }
 strcpy(address,ad);
 cout<<" 工作日期(0-10): ";
    cin>>da;
    while (strlen(da) <0 || strlen(da)>10)
    {
        cout<<" 数据有误!!重新输入.."<<endl<<endl;
        cout<<" 工作日期(0-10): ";
        cin>>da;
    }
 strcpy(date,da);
 cout<<" 工作内容(0-20): ";
    cin>>cs;
    while (strlen(cs) <0 || strlen(cs)>20)
    {
        cout<<" 数据有误!!重新输入.."<<endl<<endl;
        cout<<" 工作内容(0-20): ";
        cin>>cs;
    }
 strcpy(cons,cs);
 cout<<" 备注(0-20): ";
    cin>>ss;
    while (strlen(ss) <0 || strlen(ss)>20)
    {
        cout<<" 数据有误!!重新输入.."<<endl<<endl;
        cout<<" 备注(0-20): ";
        cin>>ss;
    }
 strcpy(ps,ss);
 
 
}

//*******按照规定格式把该学生的数据显示在屏幕上******//
void stuatom::show()
{
    cout.setf(ios::left);
    cout.width(6);
    cout<<""<<id<<"     ";
    cout.width(8);
    cout<<name;
    cout.width(10);
    cout<<sex;
    cout.width(9);
    cout<<address;
    cout.width(9);
    cout<<date;
    cout.width(9);
    cout<<cons;
    cout.width(11);
    cout<<ps;
    cout.width(10);
    cout<<endl;
}

//**************输入学生的数据***********************//
void student::input()
{
    int n;
    cout <<endl <<" 输入将要录入的学生数目: ";
    cin>>n;
    int j,i;
   
    //通过循环输入要求输入学生个数的学生的数据。
    for(j=(this->stulen+1) ; j <=(this->stulen+n); j++)
    { cout <<" 输入学生信息 " <<j <<endl;
    ob[j-1].setup();}
   
    //检查学号是否有重复
    int a[100];
    int b=0,c=0,d;
    char Y;
    if(this->stulen!=0)
    {
        for (i=0;i<this->stulen ;i++)
        {
            for(j=this->stulen ;j<(this->stulen+n);j++)
            {
                if (ob[i].id==ob[j].id)
                {
                    if (c==0)       //c只是为了美观而已。。
                    {
                        cout<<" 错误!系统检测到第"<<(j+1 )<<"位学生学号与第"<<(i+1)<<"位学生学号相同\n";
                        a[b]=j;
                        b++;c++;
                    }
                    else
                    {
                        cout<<"                 第"<<(j+1 )<<"位学生学号与第"<<(i+1)<<"位学生学号相同\n";
                        a[b]=j;
                        b++;c++;
                    }
                }
            }
        }
        for(c=0;c<b;c++)
        {
            cout << " 是否重新输入第" << (a[c]+1)<< "位学生成绩?(Y/N--选N则第" << (a[c]+1)<<"位学生成绩将不保存)";
            cin>>Y;
            while(Y!='y'&& Y!='Y'&& Y!='N'&& Y!='n')
            {
                cout<<" 无效的命令,请重新输入..\n";
                cout<<" 是否重新输入第"<<(a[c]+1)<<"位学生成绩?(Y/N--选N则第"<<(a[c]+1)<<"位学生成绩将不保存)";
                cin>>Y;
            }
            if(Y=='y'||Y=='Y')
            {
                d=a[c];
                ob[d].setup();
            }
            else
            {
                n--;
                for(d=a[c];d<(this->stulen+n);d++)
                    ob[d]=ob[d+1];
                for(d=(c+1);d<b;d++)
                    a[d]--;
            }
        }
    }
    else
    {
        for(i=0;i<n-1;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if(ob[i].id==ob[j].id)
                {
                    if (c==0)
                    {
                        cout<<" 错误!系统检测到第"<<(j+1)<<"位学生学号与第"<<(i+1)<<"位学生学号相同\n";
                        a[b]=j;
                        b++;c++;
                    }
                    else
                    {
                        cout<<"                 第"<<(j+1)<<"位学生学号与第"<<(i+1)<<"位学生学号相同\n";
                        a[b]=j;
                        b++;c++;
                    }
                }
            }
        }
        for(c=0;c<b;c++)
        {
            cout << " 是否重新输入第" << (a[c]+1) << "位学生成绩?(Y/N--选N则第" << (a[c]+1)<<"位学生成绩将不保存)";
            cin>>Y;
            while(Y!='y'&& Y!='Y'&& Y!='N'&& Y!='n')
            {
                cout<<" 无效的命令,请重新输入..\n";
                cout<<" 是否重新输入第"<<(a[c]+1)<<"位学生成绩?(Y/N--选N则第"<<(a[c]+1)<<"位学生成绩将不保存)";
                cin>>Y;
            }
            if(Y=='y'||Y=='Y')
            {
                d=a[c];
                ob[d].setup();
            }
            else
            {
                n--;
                for(d=a[c];d<n;d++)
                    ob[d]=ob[d+1];
                for(d=(c+1);d<b;d++)
                    a[d]--;
            }
        }
    }
   
    //学生数据显示格式
    system("cls");
    cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
    cout <<endl <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;
   
    //通过循环输出所有学生数据。
    for(j=this->stulen ; j <(this->stulen+n); j++)
    {
        ob[j].show();
    }
    this->stulen+=n;     //学生个数赋值
}

void student::query()
{
    int x;
    cout <<endl <<" 输入要查询学生的学号: ";
    cin>>x;
   
    int i;
    for(i=0;i <this->stulen ;i++)
    {
        if (x==ob[i].id)
            break;
    }
    if(i<this->stulen )
    {
        cout <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注"  <<endl;
        cout.setf(ios::left);
        cout.width(6);
        cout <<"" <<ob[i].id <<"     ";
        cout.width(8);
        cout <<ob[i].name;
        cout.width(10);
        cout <<ob[i].sex;
        cout.width(9);
        cout <<ob[i].address;
        cout.width(9);
        cout <<ob[i].date;
        cout.width(11);
        cout <<ob[i].cons;
        cout.width(9);
        cout <<ob[i].ps;
        cout.width(10);
        cout<<endl;
    }
    else
        cout<<" 没有找到该学生信息...";
    _getch();
}



//*******************保存学生数据**************************//
void student::save()
{
    int i;
    ofstream outfile;
    outfile.open("list.txt",ios::trunc);
    if(!outfile)
    {
        cout <<" 无法打开数据文件!\n";
    }
   
    //通过循环把所有的学生数据保存在list.txt里边。
    for(i=0; i <this->stulen; i++)
    {
        outfile <<ob[i].id <<" " <<ob[i].name <<" " <<ob[i].sex <<" " <<
            ob[i].address <<" " <<ob[i].date <<" " <<ob[i].cons <<" " <<ob[i].ps <<endl;
    }
    outfile.close();
    ofstream outfile1;
    outfile1.open("stulen.txt",ios::trunc );
    outfile1<<this->stulen ;
    outfile1.close();
}

void student::clean()
{
    int i;
    char b[20];
    ifstream infile;
    infile.open("list.txt",ios::in);
    if(!infile)
    {
        for(int i=(this->stulen-1);i <100;i++)
        {
            
            ob[i].id =0;
            ob[i].sex =' ';
            ob[i].name =NULL;
            ob[i].address=NULL;
            ob[i].date=NULL;
            ob[i].cons=NULL;
            ob[i].ps=NULL;
        }
        this->stulen=0;
        ifstream infile1;
        infile1.open("stulen.txt",ios::in);
        infile1>>this->stulen ;
        infile1.close();
    }
    else
    {
        ifstream infile1;
        infile1.open("stulen.txt",ios::in);
        infile1>>this->stulen ;
        infile1.close();
        for(i=0;i<this->stulen ;i++)
        {
            infile>>ob[i].id;
            ob[i].name =new char[strlen(b)+1];
            infile>>b;
            strcpy(ob[i].name,b);
            infile>>ob[i].sex;
            infile>>ob[i].address;
            infile>>ob[i].date;
            infile>>ob[i].cons;
            infile>>ob[i].ps ;
        }
        infile.close ();
    }
}

//*************显示所有学生数据*********************//
void student::read()
{
    int i;  
    cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
    cout <<endl <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;
    for(i=0; i <this->stulen; i++)
    {
        ob[i].show();
    }  
    cout <<"\n\n\n";
}
//*******************一个学生的数据****************//
void student::add()
{
    int i, d=this->stulen ;
    int c=0;
    cout <<" 输入要添加学生的信息:" <<endl;
    do
    {
        cout<<"\n";
        ob[d].setup();
        for(i=0;i<d;i++)
        {
            if(ob[i].id==ob[d].id )
            {
                cout<<" 系统检测到此次输入的学号和已有学生学号相同,请核实再输..";
                _getch();
                ob[d].address=NULL;
                ob[d].date=NULL;
                ob[d].cons=NULL;
                ob[d].ps=NULL;
                ob[d].id =0;
                ob[d].sex =' ';
                ob[d].name =NULL;
                c=1;
                break;
            }
            else c=0;
        }
    }while(c!=0);
   
    cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
    cout <<endl <<"       学号     姓名    性别    工作部门    工作日期     工作内容       备注"  <<endl;
   
    for(i=0; i <=d; i++)
    {
        ob[i].show();
    }
    this->stulen++;
   
    ofstream fout("list.txt",ios::app);
   
    if(!fout)
    {
        cout <<" 无法打开数据文件!\n,";
    }
   
    //把添加的学生数据添加到list.txt里边去。
    fout <<ob[d].id <<" " <<ob[d].name <<" " <<ob[d].sex <<" " <<
        ob[d].address <<" " <<ob[d].date <<" " <<ob[d].cons <<" " <<ob[d].ps <<endl;
   
    fout.close();
    ofstream outfile1;
    outfile1.open("stulen.txt",ios::trunc );
    outfile1<<this->stulen ;
    outfile1.close();
    _getch();
}
//*********************删除指定名字学生的数据*******************//
void student::del()
{ char a;
int i,p;
int x;
p=-1;
if(this->stulen==0)
{
    cout<<" 目前没有学生数据!";
    _getch();
}
else
{
    cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
    cout <<endl <<"        学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;
   
    for(i=0; i <this->stulen; i++)
    {
        ob[i].show();
    }
   
    cout <<"\n\n 输入要删除的学生学号:" ;
    cin>>x;
   
    //通过for循环查找要删除学生的学号
    for(i=0; i <stulen; i++)
    {
        if(x==ob[i].id )
        {
            p=i;  
            cout <<endl <<"        学号     姓名    性别    工作部门    工作日期     工作内容       备注"  <<endl;
            cout <<"      " <<ob[i].id <<"       " <<ob[i].name <<"      " <<ob[i].sex <<"      " <<ob[i].address <<"      " <<ob[i].date <<"      " <<ob[i].cons <<"       " <<ob[i].ps<<endl;
            break;
        }
        else
            continue;
    }
    if(p==-1)
    {
        cout<<" 没有此学生数据!";
        _getch();
    }
    else
    {
        cout<<" 确定删除此学生数据?<Y/N>";
        cin>> a;
        if (a=='y'|| a=='Y')
        {
            cout <<" 删除成功!" <<endl;
            
            //删除了之后,应该把后面的数据往前移,把要删除的数据覆盖,并且学生长度减1
            stulen--;
            for(i;i <stulen;i++)
            {
                ob[i]=ob[i+1];
            }
            ofstream outfile;
            outfile.open("list.txt",ios::trunc);
            for(i=0; i <this->stulen; i++)
            {
                outfile <<ob[i].id <<" " <<ob[i].name <<" " <<ob[i].sex <<" " <<
                    ob[i].address <<" " <<ob[i].date <<" " <<ob[i].cons <<" " <<ob[i].ps <<endl;
            }
            outfile.close();
            ofstream outfile1;
            outfile1.open("stulen.txt",ios::trunc );
            outfile1<<this->stulen ;
            outfile1.close();
            this->read ();
        }
        else
        {
            cout<<" 回到主界面...";
            _getch();
        }
    }
}
}

//********修改学生信息********//
void student::modify()
{
    int a,i,p,b;
    p=-1;
    system("cls");
    cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
    cout <<endl <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;
   
    for(i=0; i <this->stulen; i++)
    {
        ob[i].show();
    }
    cout <<"\n\n 请输入要修改学生的学号:";
    cin>>a;
    for(i=0; i <stulen; i++)
    {
        if(a==ob[i].id)
        {
            p=i;  
            cout <<endl <<"       学号     姓名    性别    工作部门    工作日期     工作内容       备注"  <<endl;
            cout <<"      " <<ob[i].id <<"     " <<ob[i].name <<"      " <<ob[i].sex <<"      " <<ob[i].address <<"      " <<ob[i].date <<"      " <<ob[i].cons <<"       " <<ob[i].ps <<endl;
            break;
        }
        else
            continue;
    }
    if(p==-1)
    {
        cout<<" 没有此学生数据!";
        _getch();
    }
    else
    {
        cout<<"\n\n 请输入要修改的数据(1.工作部门, 2.工作日期,3.工作内容,4.备注):";
        cin>>b;
        while(b!=1 &&b!=2 &&b!=3 &&b!=4)
        {
            cout<<" 无效输入,请重新输入!\n";
            cout<<"\n\n 请输入要修改的数据(1.工作部门, 2.工作日期,3.工作内容,4.备注):";
            cin>>b;
        }
        switch(b)
        {
        case 1:
            {
                char m[40];
                cout<<" 工作部门(40以内): ";
                cin>>m;
               
                //如果输入成绩在数据域内,跳出循环并且赋值。
                //如果不在数据域内,一直循环到输入数据符合数据域为止
                while (strlen(m) <0 || strlen(m)>40)
                {
                    cout<<" 数据有误!!重新输入.."<<endl<<endl;
                    cout<<" 工作部门(40以内): ";
                    cin>>m;
                }
               
                ob[p].address=m;break;
            }
        case 2:
            {
            
                char e[40];
                cout<<" 工作日期(40以内): ";
                cin>>e;
               
                while (strlen(e) <0 || strlen(e)>40)
                {
                    cout<<" 数据有误!!重新输入.."<<endl<<endl;
                    cout<<" 工作日期(40以内): ";
                    cin>>e;
                }
               
                ob[p].date=e; break;
            }
        case 3:
            {
            
                char co[40];
                cout<<" 工作内容(40以内): ";
                cin>>co;
               
                while (strlen(co)<0 || strlen(co)>40)
                {
                    cout<<" 数据有误!!重新输入.."<<endl<<endl;
                    cout<<"  工作内容(40以内): ";
                    cin>>co;
                }
               
                ob[p].cons=co;
            }
        case 4:
            {
            
                char ss[40];
                cout<<" 备注(40以内): ";
                cin>>ss;
               
                while (strlen(ss) <0 ||strlen(ss)>40)
                {
                    cout<<" 数据有误!!重新输入.."<<endl<<endl;
                    cout<<"  备注(40以内): ";
                    cin>>ss;
                }
               
                ob[p].ps=ss;
            }  
        }
        ofstream outfile;
        outfile.open("list.txt",ios::trunc);  
        for(i=0; i <this->stulen; i++)
        {
            outfile <<ob[i].id <<" " <<ob[i].name <<" " <<ob[i].sex <<" " << ob[i].address <<" " <<ob[i].date <<" " <<ob[i].cons <<" " <<ob[i].ps <<endl;   
        }
        outfile.close();
        cout<<" 成绩修改成功!回车返回主界面...";
        _getch();
    }
 }
 //***************程序运行前先检查以前是否有数据存在*****************//
 void student::check()
 {
     int i;
     char b[20];
     ifstream infile;
     infile.open("list.txt",ios::in );
     if(!infile)
     {
         cout<<" 系统没有检测到以前输入的数据,此次操作将建立一个新的数据文件...";
         _getch();
     }
     else
     {
         ifstream infile1;
         infile1.open("stulen.txt",ios::in);
         infile1>>this->stulen ;
         infile1.close();
         for(i=0;i<this->stulen ;i++)
         {
             infile>>ob[i].id;
             ob[i].name =new char[strlen(b)+1];
             infile>>b;
             strcpy(ob[i].name,b);
             infile>>ob[i].sex ;
             infile>>ob[i].address ;
             infile>>ob[i].date;
             infile>>ob[i].cons;
             infile>>ob[i].ps;
         }
         infile.close ();
         
         system("cls");
         cout<<" 系统检测到以前输入过数据,数据内容如下:\n\n";
         cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
         cout <<endl <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;
         
         //通过循环输出所有学生数据。
         for(i=0; i<this->stulen; i++)
         {
             ob[i].show();
         }
         cout<<"\n\n\n";
         cout<<" 原始数据是否覆盖?(Y/N)";
         char q;
         cin>>q;
         while(q!='y'&& q!='Y'&& q!='N'&& q!='n')
         {
             cout<<" 无效的命令,请重新输入..\n";
             cout<<" 原始数据是否覆盖?(Y/N)";
             cin>>q;
         }
         if(q=='y'||q=='Y')
         {
             for(int i=0;i <100;i++)
             {
                 ob[i].address=NULL;
                 ob[i].date=NULL;
                 ob[i].cons=NULL;
                 ob[i].ps=NULL;
                 ob[i].id =0;
                 ob[i].sex =NULL;
                 ob[i].name =NULL;
             }
             this->stulen =0;
             ofstream outfile1;
             outfile1.open("stulen.txt",ios::trunc);
             outfile1<<this->stulen ;
             outfile1.close();
             system("del list.txt");
         }
     }
 }
 
 //**************按照一定格式显示所要查询学生的信息。**************//
 void menu()
 {
     cout <<"\n\n";
     cout <<"------------------ 学生成绩系统 -----------------" <<endl <<endl;
     cout <<"\t1.录入与保存学生信息:\n";
     cout <<"\t2.读取学生信息:\n";
     cout <<"\t3.删除学生信息:\n";
     cout <<"\t4.追加学生信息:\n";
     cout <<"\t5.查询学生信息:\n";
     cout <<"\t6.修改学生信息:\n";
     cout <<"\t7.退出系统......\n\n\n";
     cout <<"\t请选择要实现的功能: ";
 }
 
 
 void main()
 {
     student a;
     a.check();
     while(1)
     {
         int SEL;
         system("cls");
         menu();
         cin>>SEL;
         switch(SEL)
         {
         case 1:
             system("cls");
             a.input();
             cout <<endl;
             cout <<" 是否保存? (Y/N): ";
             char Y;
             cin>>Y;
             while(Y!='y'&& Y!='Y'&& Y!='N'&& Y!='n')
             {
                 cout<<" 无效的命令,请重新输入..\n";
                 cout<<" 是否保存? (Y/N)";
                 cin>>Y;
             }
             system("cls");
             if(Y=='y'||Y=='Y')
                 a.save();
             else/* if(Y=='n'||Y=='N')*/
                 a.clean();break;
         case 2:
             system("cls"); a.read(); break;
         case 3:
             system("cls"); a.del(); break;
         case 4:
             system("cls"); a.add();break;
         case 5:
             system("cls"); a.query();break;
         case 6:
             system("cls"); a.modify();break;
         case 7:
             exit(0);
         default:
             cout <<"        无效的命令!";
             _getch();
             break;
         }
     }
 }
搜索更多相关主题的帖子: 如何 private address include 
2011-09-02 23:46
woshihsz
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-9-2
收藏
得分:0 
程序代码:
void student::modify()
{ 
    int a,i,p,b;
    p=-1;
    system("cls"); 
    cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl; 
    cout <<endl <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl; 
    
    for(i=0; i <this->stulen; i++) 
    { 
        ob[i].show(); 
    } 
    cout <<"\n\n 请输入要修改学生的学号:";
    cin>>a;
    for(i=0; i <stulen; i++) 
    { 
        if(a==ob[i].id) 
        { 
            p=i;  
            cout <<endl <<"       学号     姓名    性别    工作部门    工作日期     工作内容       备注"  <<endl; 
            cout <<"      " <<ob[i].id <<"     " <<ob[i].name <<"      " <<ob[i].sex <<"      " <<ob[i].address <<"      " <<ob[i].date <<"      " <<ob[i].cons <<"       " <<ob[i].ps <<endl;
            break; 
        } 
        else 
            continue; 
    } 
    if(p==-1)
    {
        cout<<" 没有此学生数据!";
        _getch();
    }
    else
    {
        cout<<"\n\n 请输入要修改的数据(1.工作部门, 2.工作日期,3.工作内容,4.备注):";
        cin>>b;
        while(b!=1 &&b!=2 &&b!=3 &&b!=4)
        {
            cout<<" 无效输入,请重新输入!\n";
            cout<<"\n\n 请输入要修改的数据(1.工作部门, 2.工作日期,3.工作内容,4.备注):";
            cin>>b;
        }
        switch(b)
        {
        case 1:
            { 
                char m[40]; 
                cout<<" 工作部门(40以内): "; 
                cin>>m; 
                
                //如果输入成绩在数据域内,跳出循环并且赋值。 
                //如果不在数据域内,一直循环到输入数据符合数据域为止 
                while (strlen(m) <0 || strlen(m)>40) 
                { 
                    cout<<" 数据有误!!重新输入.."<<endl<<endl; 
                    cout<<" 工作部门(40以内): "; 
                    cin>>m; 
                } 
                
                ob[p].address=m;break;
            } 
        case 2:
            {
            
                char e[40];
                cout<<" 工作日期(40以内): "; 
                cin>>e; 
                
                while (strlen(e) <0 || strlen(e)>40) 
                { 
                    cout<<" 数据有误!!重新输入.."<<endl<<endl; 
                    cout<<" 工作日期(40以内): "; 
                    cin>>e; 
                } 
                
                ob[p].date=e; break;
            }
        case 3:
            {
            
                char co[40];
                cout<<" 工作内容(40以内): "; 
                cin>>co; 
                
                while (strlen(co)<0 || strlen(co)>40) 
                { 
                    cout<<" 数据有误!!重新输入.."<<endl<<endl; 
                    cout<<"  工作内容(40以内): "; 
                    cin>>co; 
                } 
                
                ob[p].cons=co; 
            }
        case 4:
            {
            
                char ss[40];
                cout<<" 备注(40以内): "; 
                cin>>ss; 
                
                while (strlen(ss) <0 ||strlen(ss)>40) 
                { 
                    cout<<" 数据有误!!重新输入.."<<endl<<endl; 
                    cout<<"  备注(40以内): "; 
                    cin>>ss; 
                } 
                
                ob[p].ps=ss; 
            }  
        }
        ofstream outfile;
        outfile.open("list.txt",ios::trunc);  
        for(i=0; i <this->stulen; i++) 
        { 
            outfile <<ob[i].id <<" " <<ob[i].name <<" " <<ob[i].sex <<" " << ob[i].address <<" " <<ob[i].date <<" " <<ob[i].cons <<" " <<ob[i].ps <<endl;   
        }
        outfile.close(); 
        cout<<" 成绩修改成功!回车返回主界面..."; 
        _getch();
    }

 }


这里应该怎么改啊?
2011-09-02 23:48
woshihsz
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-9-2
收藏
得分:0 
ob[p].address=m;break;像这个运行 会 直接崩溃 ,怎么办?
2011-09-02 23:49
玩出来的代码
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:河南新乡
等 级:贵宾
威 望:11
帖 子:742
专家分:2989
注 册:2009-10-12
收藏
得分:0 
基础问题。。你对指针还不了解啊。先看看指针吧。

离恨恰如春草,更行更远还生。
2011-09-03 18:01
woshihsz
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-9-2
收藏
得分:0 
回复 4楼 玩出来的代码
能不能说具体点啊~
2011-09-03 19:05
玩出来的代码
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:河南新乡
等 级:贵宾
威 望:11
帖 子:742
专家分:2989
注 册:2009-10-12
收藏
得分:0 
你说的错误的地点我没细看。
int *paddr=NULL;
strcpy(paddr,"asd");
你认为怎么样、

离恨恰如春草,更行更远还生。
2011-09-03 21:51
玩出来的代码
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:河南新乡
等 级:贵宾
威 望:11
帖 子:742
专家分:2989
注 册:2009-10-12
收藏
得分:0 
断点,单步,你看看崩溃是什么原因了。

离恨恰如春草,更行更远还生。
2011-09-03 21:53
快速回复:来看看吧,如何 将 char 赋值给char*
数据加载中...
 
   



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

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