首页 新闻 会员 周边

学习写的单链表,通过编译不可以运行,求大神帮个忙

0
悬赏园豆:20 [已解决问题] 解决于 2013-10-11 18:26
//*******************************

//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

兔子爱吃小白菜的主页 兔子爱吃小白菜 | 初学一级 | 园豆:184
提问于:2013-10-10 22:34
< >
分享
最佳答案
1

Node *q=new Node;

q->next=NULL; //加上这一句

 

q->id=getid();

q->name=getname();

 

不过还是要说几句,这么简单的问题也问人,而不是自己去解决,永远也无法提高自己的水平。

收获园豆:20
嗷嗷 | 小虾三级 |园豆:757 | 2013-10-11 11:08
其他回答(1)
0

这问题也拿出来问,语法算法是基础,还是多看看,打个断点调试一下不就知道问题在哪儿了吗?

空明流光 | 园豆:106 (初学一级) | 2013-10-11 08:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册