首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

高手帮看一下 c++ 编程中的段异常

2012-02-11 
高手帮看一下 c++ 编程中的段错误小弟是新手,在编一个银行用户的菜单时于不断创建用户时,程序报段错误。其

高手帮看一下 c++ 编程中的段错误
小弟是新手,在编一个银行用户的菜单时   于不断创建用户时,程序报段错误。其他功能完好,编译通过。自己反复察看无果,想请高手帮助,谢谢。

#include <iostream>
using   namespace   std;
//定义一个用户的类  
class   account{
private:
                            long   id;
                            char*   password;
                            double   balance;
public:
account();//无参  
~account();//析构  
account(const   account&   accp);//拷贝构造函数  
void   app();   //申请动态空间  
int   create   (char*   pass,double   balance);     //创造   新的用户  
int   save(double   sum);           //存钱  
int   withdraw(char*   pass,double   sum);       //取钱  
double   query(char*   pass);         //查询  
account&   operator=(const   account&   a);     //   赋值重载  
};
void   create();
void   save();
void   with();
void   query();

int   c;   //main   函数中   用户选择   几号菜单  
int   max_len=10;       /*   设置   初始存放用户数组的空间   */  
long   i;   //用于计数    
double   fsum;/*   区分   class   中的sum*/  
char   fpassword[50];  
double   fbalance;
long   fid;
account*   acc=new   account[10];           //用acc   来存   用户信息  

account::account(){
cout < < "hhhhhha " < <endl;
long   id=0;
password   =   new   char[50];
balance   =   0;
}
account::account(const   account&   a){
password   =   new   char[50];
id=a.id;
balance=a.balance;
strcpy(password,a.password);
}

account::~account(){
cout < < "delete " < <endl;
delete   []   password;
}
        int   account::create   (char*   fpassword,double   fbalance){
                if((i+8)==max_len){
                              app();
                }
 
  balance=fbalance;
                strcpy(password,fpassword);        
                          cout < < "create " < <max_len < <endl;
                  cout < < "you   win!!!   new   id " < <i < < "       password:     ";
                cout < <password;
  return   0;
}

                int   account::save(double   sum){
                balance=balance+sum;
                cout < <id < < "     acc   saved   money     " < <sum < <endl;


                return   0;
                }

                int   account::withdraw(char*   fpassword,double   sum){
                  if(strcmp(password,fpassword)!=0){
                                return   -1;
                    }
        }
                double   account::query(char*   fpassword){
                  if(strcmp(password,fpassword)==0){
                                return   balance;
                    }
                cout < < "wrong       " < <endl;
                return   -1;
                  }
account&   account::operator=(const   account&   a){
id=a.id;
balance=a.balance;
strcpy(password,a.password);
return   *this;
}
        void   account::   app(){
                    account*   str=new   account[max_len+10];
            cout < < "gagagagagagaga " < <endl;
                                for(int   i=0;i <max_len;i++){
                                                str[i]=acc[i];
                                }
                    delete   []acc;
                    acc=str;
                    max_len=max_len+10;
                    str=NULL;
                    delete   []   str;
                    cout < <max_len < < "app " < <endl;
          }
void   create(){
cin> > fpassword;
                cin> > fbalance;
                acc[i].create(fpassword,fbalance);
                i++;
}
void   save(){
cin> > fid;
                cin> > fsum;
                acc[fid].save(fsum);
}
void   with(){
cin> > fid;
                cin> > fpassword;
                cin> > fsum;


                int   ret=acc[fid].withdraw(fpassword,fsum);
                if(ret==0){
                cout < <fid < < "     acc   withdrawed   money     " < <fsum < <endl;
                }
                if(ret==-1){
                                  cout < < "id   or   password   wrong     " < <endl;
}
}
void   query(){
cin> > fid;
                cin> > fpassword;
double   ret=acc[fid].query(fpassword);
                                  if(ret==-1){
                                                      cout < < "id   or   password   wrong     " < <endl;
                                  }
                                  cout < <fid < < "     acc   have     " < <ret < <endl;

}


int   menu(int   c){
switch   (c){
case   1:{
if(c==1){
cout < < "========== " < <endl;
cout < < "enter   your   password   and   money   \n ";
cout < < "========== " < <endl;
create();
break;
}
}
case   2:{
if(c==2){
cout < < "========== " < <endl;
cout < < "enter   your   Id   and   money   \n ";
cout < < "========== " < <endl;
save();
break;

}
}
case   3:{
if(c==3){
cout < < "========== " < <endl;
cout < < "enter   your   Id     password   sum   \n ";
cout < < "========== " < <endl;
with();
break;
}
}
case   4:{
if(c==4){
cout < < "========== " < <endl;
cout < < "enter   your   Id   and   password   \n ";
cout < < "========== " < <endl;
query();
break;
}
}
case   5:{
if(c==5){
cout < < "bye   \n ";
break;
}
}
}
}

int   main(){
while(c!=5){
cout < < "========== " < <endl;
cout < < "1   ------>   create   " < <endl;
cout < < "2   -------->   save   " < <endl;
cout < < "3   ---->   withdraw   " < <endl;
cout < < "4   ------->   query   " < <endl;
cout < < "5   -------->   back   " < <endl;


cout < < "========== " < <endl;
cout < < "enter   the   num   " < <endl;
cin> > c;
if(!cin){
cout < < "gaga     \n ";
break;
}
menu(c);

}
return   0;
}


[解决办法]
#include <iostream>

using namespace std;
//定义一个用户的类
class account{
private:
long id;
char* password;
double balance;
public:
account();//无参
~account();//析构
account(const account& accp);//拷贝构造函数
//去掉
//void app(); //申请动态空间
int create (char* pass,double balance); //创造 新的用户
int save(double sum); //存钱
int withdraw(char* pass,double sum); //取钱
double query(char* pass); //查询
account& operator=(const account& a); // 赋值重载
};
void create();
void save();
void with();
void query();

int c; //main 函数中 用户选择 几号菜单
int max_len=10; /* 设置 初始存放用户数组的空间 */
long i; //用于计数
double fsum;/* 区分 class 中的sum*/
char fpassword[50];
double fbalance;
long fid;
account* acc=new account[10]; //用acc 来存 用户信息

account::account(){
cout < < "hhhhhha " < <endl;
long id=0;
password = new char[50];
balance = 0;
}
account::account(const account& a){
password = new char[50];
id=a.id;
balance=a.balance;
strcpy(password,a.password);
}

account::~account(){
cout < < "delete " < <endl;
delete [] password;
password = NULL;
}
int account::create (char* fpassword,double fbalance){
//这里要去掉
//if((i+8)==max_len){
//app();
//}
balance=fbalance;
strcpy(password,fpassword);
cout < < "create " < <max_len < <endl;
cout < < "you win!!! new id " < <i < < " password: ";
cout < <password;
return 0;
}

int account::save(double sum){
balance=balance+sum;
cout < <id < < " acc saved money " < <sum < <endl;
return 0;
}

int account::withdraw(char* fpassword,double sum){
if(strcmp(password,fpassword)!=0){
return -1;
}
}
double account::query(char* fpassword){
if(strcmp(password,fpassword)==0){
return balance;
}
cout < < "wrong " < <endl;
return -1;
}
account& account::operator=(const account& a){
//这里修改了,,不是 *this == a
if(this==&a) return *this;
delete [] password;
id=a.id;
balance=a.balance;
password=new char[50];
strcpy(password,a.password);
return *this;
}

//这里修改了,变为全局函数
void app(){
account* str=new account[max_len+10];
cout < < "gagagagagagaga " < <endl;
for(int x=0; x <i; x++){
str[x]=acc[x];
}
delete []acc;
acc=str;
max_len=max_len+10;
cout < <max_len < < "app " < <endl;
}

void create(){
cin> > fpassword;
cin> > fbalance;
//这里修改了,改为每输入10个就重新自动分配一次内存,自动调整帐号数量大小
if ( i == max_len)
{
app();
}
acc[i].create(fpassword,fbalance);
i++;
}
void save(){
cin> > fid;
cin> > fsum;
acc[fid].save(fsum);
}
void with(){
cin> > fid;
cin> > fpassword;


cin> > fsum;
int ret=acc[fid].withdraw(fpassword,fsum);
if(ret==0){
cout < <fid < < " acc withdrawed money " < <fsum < <endl;
}
if(ret==-1){
cout < < "id or password wrong " < <endl;
}
}
void query(){
cin> > fid;
cin> > fpassword;
double ret=acc[fid].query(fpassword);
if(ret==-1){
cout < < "id or password wrong " < <endl;
}
cout < <fid < < " acc have " < <ret < <endl;

}


int menu(int c){
switch (c){
case 1:{
if(c==1){
cout < < "========== " < <endl;
cout < < "enter your password and money \n ";
cout < < "========== " < <endl;
create();
break;
}
}
case 2:{
if(c==2){
cout < < "========== " < <endl;
cout < < "enter your Id and money \n ";
cout < < "========== " < <endl;
save();
break;

}
}
case 3:{
if(c==3){
cout < < "========== " < <endl;
cout < < "enter your Id password sum \n ";
cout < < "========== " < <endl;
with();
break;
}
}
case 4:{
if(c==4){
cout < < "========== " < <endl;
cout < < "enter your Id and password \n ";
cout < < "========== " < <endl;
query();
break;
}
}
case 5:{
if(c==5){
cout < < "bye \n ";
break;
}
}
}
}

int main(){
while(c!=5){
cout < < "========== " < <endl;
cout < < "1 ------> create " < <endl;
cout < < "2 --------> save " < <endl;
cout < < "3 ----> withdraw " < <endl;
cout < < "4 -------> query " < <endl;
cout < < "5 --------> back " < <endl;
cout < < "========== " < <endl;
cout < < "enter the num " < <endl;
cin> > c;
if(!cin){
cout < < "gaga \n ";
break;
}
menu(c);

}
return 0;
}

我重新把整个程序给修改了,这个在void create() 实现了自动调整帐号大小的功能

热点排行