//******************************* //main.cpp #include<iostream> #include<string> #include "List_Bank.h" using namespace std; int main() { Account A; A.insert(); Node *p=A.checkid(); //运行到这里就会停止 A.show(p); return 0; } //************************************* //List_Bank.h #ifndef _LIST_BANK_H_ #define _LIST_BANK_H_ #include<iostream> #include<string> using namespace std; int Card=10000; struct Node { int card; string id; string name; string password; double money; bool flag; Node *next; }; class Account { public: Account(); void insert(); string getname(); string getid(); string getpassword(); int length(); void show(Node *p); Node* checkid(); private: Node *head; }; Account::Account() { head=new Node; head->next=NULL; } //创建新账户 void Account::insert() { int index=length(); Node *p=head; int count=0; while(p!=NULL && count<index-1) { p=p->next; count++; } if(p==NULL) { cout<<"wrong place"<<endl; return; } else { Node *q=new Node; q->id=getid(); q->name=getname(); q->password=getpassword(); q->card=Card++; q->flag=true; p->next=q; } } //显示 void Account::show(Node *p) { cout<<endl; cout<<"**************************"<<endl; cout<<"This is your account"<<endl; cout<<"card number:\t"<<p->card<<endl; cout<<"id:\t"<<p->id<<endl; cout<<"name:\t"<<p->name<<endl; cout<<"password:\t"<<p->password<<endl; cout<<"money:\t"<<p->money<<endl; cout<<"**************************"<<endl; cout<<endl; } //输入身份证 string Account::getid() { cout<<"*****************************"<<endl; cout<<"Please enter your id number"<<endl; string id; cin>>id; return id; } //返回长度 int Account::length() { Node *p=head; int count=0; while(p!=NULL) { count++; p=p->next; } return count; } //输入名字 string Account::getname() { cout<<"**************************"<<endl; cout<<"Please enter your name"<<endl; string name; cin>>name; return name; } //输入密码 string Account::getpassword() { string password1; string password2; while(1) { cout<<endl; cout<<"**************************************"<<endl; cout<<"Please enter your password twice"<<endl; cin>>password1; cout<<"**************************************"<<endl; cin>>password2; if(password1==password2) break; else cout<<"They are not the same!Write again!"<<endl; } return password1; } //身份证号查找 Node* Account::checkid() { int index=length(); Node *p=head; string id; cout<<"************************"<<endl; cout<<"Enter your id number"<<endl; cin>>id; while((id!=p->id ) &&(p->next!=NULL)) { p=p->next; } if(id==p->id) return p; else { cout<<"This id can not be found!"<<endl; return head; } } #endif
Node *q=new Node;
q->next=NULL; //加上这一句
q->id=getid();
q->name=getname();
不过还是要说几句,这么简单的问题也问人,而不是自己去解决,永远也无法提高自己的水平。
这问题也拿出来问,语法算法是基础,还是多看看,打个断点调试一下不就知道问题在哪儿了吗?