m0_53989813 2021-08-24 09:22 采纳率: 73.7%
浏览 63
已结题

学生通讯录,当我创新一个新通讯录并且输入后,一直循环输出是否继续,哪里有问题?

学生通讯录程序:用结构体和数组实现学生通讯录程序。基本信息包括学号、姓名、班级、籍贯、手机、QQ号、电子邮箱等信息。具体实现的管理功能如下:(1)输入并显示多个学生信息;(2)可实现学生信息的添加;(3)查询(至少提供按姓名和手机查询两种方式);(4)修改(只提供按照姓名进行修改);(5)删除(依据指定的信息删除如姓名、学号等);(6)文件的导入和导出(从文件中读取若干条通讯录,或者将通讯录输出到文件中)。


#include<iostream>
#include<string>
#include <fstream>
using namespace std;
struct Student
    {
        int num;
        char name[20];
        char Class[20];
        char addr[30];
        int phone;
        int QQ;
        char mail[50];
        Student *next;
    };
Student *creat()
{
    int i=1;
    char n='y';
    Student *head= new Student();
    if(head==NULL)
    {
        cerr<<"内存空间分配失败"<<endl;
        exit(1);
    }
    head->next==NULL;
    while(n=='y')
    {
        Student *newStudent= new Student();
        if(head==NULL)
        {
            cerr<<"内存空间分配失败"<<endl;
            exit(1);
        }
        cin>>newStudent->num>>newStudent->name>>newStudent->Class>>newStudent->addr>>newStudent->phone>>newStudent->QQ>>newStudent->mail;
        newStudent->next=NULL;
        newStudent->next=head->next;
        head->next=newStudent;
        i++;
        cout<<"是否继续?";
        cin>>n;
    }
    cout<<"是否继续?";
        cin>>n;
    return head;
}
bool Add(Student *head)
{
    Student *newStudent=new Student();
    if(newStudent==NULL)
    {
        cerr<<"内存空间分配失败"<<endl;
        exit(1);
    }
    cin>>newStudent->num>>newStudent->name>>newStudent->Class>>newStudent->addr>>newStudent->phone>>newStudent->QQ>>newStudent->mail;
    newStudent->next=head->next;
    head->next=newStudent;
    return true;
}
bool Search(Student *head,char name[20],int phone)
{
    Student *p=head->next;
    while(p!=NULL&&(p->name!=name||p->phone!=phone))
    {
        p=p->next;
    }
    if(p==NULL) return false;
    return true;
}
bool Change(Student *head,int num)
{
    Student *p=head->next;
    while(p!=NULL&&(p->num!=num))
    {
        p=p->next;
    }
    int p1;char p2[20];char p3[20];char p4[30];int p5; int p6;char p7[50];
    cin>>p1>>p2>>p3>>p5>>p5>>p6>>p7;
    p->num=p1;
    p->name[20]=p2[20];p->Class[20]=p3[20];p->addr[30]=p4[30];
    p->phone=p5;p->QQ=p6;p->mail[50]=p7[20];
    if(p==NULL) return false;
    return true;
}
bool Delete(Student *head,int num)
{
    Student *pre=head;
    Student *p=head->next;
    while(p!=NULL&p->num!=num)
    {
        pre=p;
        p=p->next;
    }
    if(p==NULL) return false;
    delete p;
    p=NULL;
    return true;
}
void Print(Student *head)
{
    Student *p=head->next;
    while(p!=NULL)
    {
        cout<<"学号:"<<p->num<<endl;
        cout<<"姓名:"<<p->name<<endl;
        cout<<"班级:"<<p->Class<<endl;
        cout<<"地址:"<<p->addr<<endl;
        cout<<"电话:"<<p->phone<<endl;
        cout<<"QQ:"<<p->QQ<<endl;
        cout<<"邮箱:"<<p->mail<<endl;
        p=p->next;
    }
}
Student *Read()
{
    Student *head= new Student();
    if(head==NULL)
    {
        cerr<<"内存空间分配失败"<<endl;
        exit(1);
    }
    ifstream inFile;
    inFile.open("student.txt",ios::in);
    if(!inFile.is_open())
    {
        cout<<"Error:opening file fail"<<endl;
        exit(1);
    }
    Student *newStudent =new Student();
    if(NULL==newStudent)
    {
        cerr<<"内存空间分配失败"<<endl;
        exit(1);
    }
    while(!inFile.eof())
    {
        Student *newStudent= new Student();
        if(NULL==newStudent)
        {
            cerr<<"内存空间分配失败"<<endl;
            exit(1);
        }

        inFile>>newStudent->num>>newStudent->name>>newStudent->Class>>newStudent->addr>>newStudent->phone>>newStudent->QQ>>newStudent->mail;
        newStudent->next=head->next;
        head->next=newStudent;
    }
    inFile.close();
    return head;
}
bool Write( Student *head)
{
    Student *p=head->next;
    ofstream outFile;
    outFile.open("student.txt",ios::out);
    if(!outFile)
    {
        cout<<"Error:opening file fail"<<endl;
        exit(1);
    }
    while(p!=NULL)
    {
        outFile<<p->num<<" "<<p->name<<" "<<p->Class<<" "<<p->addr<<" "<<p->phone<<" "<<p->QQ<<" "<<p->mail<<endl;
        p->next;
    }
    return true;
}

int main() 
{
    cout<<"             请选择下面的菜单:"<<endl;
    cout<<"1 创建"<<endl;
    cout<<"2 添加"<<endl;
    cout<<"3 查询"<<endl;
    cout<<"4 修改"<<endl;
    cout<<"5 删除"<<endl;
    cout<<"6 显示"<<endl;
    cout<<"7 从文件导入"<<endl;
    cout<<"8 导出到文件"<<endl;
    cout<<"9 退出"<<endl;
    cout<<"请选择: ";
    int m;
    int num,phone;
    char name[20];
    bool finished=false;
    Student *head=NULL;
    cin>>m;
    switch(m)
    {
    case 1:
        head=creat();
        break;
    case 2:
        if(Add(head))
            cout<<"添加学生信息成功!"<<endl;
        else
            cout<<"添加学习信息失败!"<<endl;
        break;
    case 3:
        if(Search(head,name,phone))
            cout<<"搜索成功!"<<endl;
        else
            cout<<"搜索失败!"<<endl;
        break;
    case 4:
        cout<<" 请输入要修改的学生通讯录信息的学号:";
        cin>>num;
        if(Search(head,name,phone))
            cout<<"修改成功!"<<endl;
        else
            cout<<"修改失败!"<<endl;
        break;
    case 5:
        if(Delete(head,num))
            cout<<"删除成功!"<<endl;
        else
            cout<<"删除失败!"<<endl;
        break;
    case 6:
        Print(head);
    case 7:
        Read();
        break;
    case 8:
        Write(head);
        break;
    case 9:
        finished=true;
        break;
    default:
        cout<<"输入选择错误,请重新输入!"<<endl;
    }
    return 0;
}

  • 写回答

1条回答 默认 最新

  • 六年级初中生 2021-08-24 10:17
    关注

    这样不就好了吗

     
    #include<iostream>
    #include<string>
    #include <fstream>
    using namespace std;
    struct Student
        {
            int num;
            char name[20];
            char Class[20];
            char addr[30];
            int phone;
            int QQ;
            char mail[50];
            Student *next;
        };
    Student *creat()
    {
        int i=1;
        char n='y';
        Student *head= new Student();
        if(head==NULL)
        {
            cerr<<"内存空间分配失败"<<endl;
            exit(1);
        }
        head->next==NULL;
        while(n=='y')
        {
            Student *newStudent= new Student();
            if(head==NULL)
            {
                cerr<<"内存空间分配失败"<<endl;
                exit(1);
            }
            cin>>newStudent->num>>newStudent->name>>newStudent->Class>>newStudent->addr>>newStudent->phone>>newStudent->QQ>>newStudent->mail;
            newStudent->next=NULL;
            newStudent->next=head->next;
            head->next=newStudent;
            i++;
            cout<<"是否继续?";
            cin>>n;
        }
      //  cout<<"是否继续?";
        //    cin>>n;
        return head;
    }
    bool Add(Student *head)
    {
        Student *newStudent=new Student();
        if(newStudent==NULL)
        {
            cerr<<"内存空间分配失败"<<endl;
            exit(1);
        }
        cin>>newStudent->num>>newStudent->name>>newStudent->Class>>newStudent->addr>>newStudent->phone>>newStudent->QQ>>newStudent->mail;
        newStudent->next=head->next;
        head->next=newStudent;
        return true;
    }
    bool Search(Student *head,char name[20],int phone)
    {
        Student *p=head->next;
        while(p!=NULL&&(p->name!=name||p->phone!=phone))
        {
            p=p->next;
        }
        if(p==NULL) return false;
        return true;
    }
    bool Change(Student *head,int num)
    {
        Student *p=head->next;
        while(p!=NULL&&(p->num!=num))
        {
            p=p->next;
        }
        int p1;char p2[20];char p3[20];char p4[30];int p5; int p6;char p7[50];
        cin>>p1>>p2>>p3>>p5>>p5>>p6>>p7;
        p->num=p1;
        p->name[20]=p2[20];p->Class[20]=p3[20];p->addr[30]=p4[30];
        p->phone=p5;p->QQ=p6;p->mail[50]=p7[20];
        if(p==NULL) return false;
        return true;
    }
    bool Delete(Student *head,int num)
    {
        Student *pre=head;
        Student *p=head->next;
        while(p!=NULL&p->num!=num)
        {
            pre=p;
            p=p->next;
        }
        if(p==NULL) return false;
        delete p;
        p=NULL;
        return true;
    }
    void Print(Student *head)
    {
        Student *p=head->next;
        while(p!=NULL)
        {
            cout<<"学号:"<<p->num<<endl;
            cout<<"姓名:"<<p->name<<endl;
            cout<<"班级:"<<p->Class<<endl;
            cout<<"地址:"<<p->addr<<endl;
            cout<<"电话:"<<p->phone<<endl;
            cout<<"QQ:"<<p->QQ<<endl;
            cout<<"邮箱:"<<p->mail<<endl;
            p=p->next;
        }
    }
    Student *Read()
    {
        Student *head= new Student();
        if(head==NULL)
        {
            cerr<<"内存空间分配失败"<<endl;
            exit(1);
        }
        ifstream inFile;
        inFile.open("student.txt",ios::in);
        if(!inFile.is_open())
        {
            cout<<"Error:opening file fail"<<endl;
            exit(1);
        }
        Student *newStudent =new Student();
        if(NULL==newStudent)
        {
            cerr<<"内存空间分配失败"<<endl;
            exit(1);
        }
        while(!inFile.eof())
        {
            Student *newStudent= new Student();
            if(NULL==newStudent)
            {
                cerr<<"内存空间分配失败"<<endl;
                exit(1);
            }
            inFile>>newStudent->num>>newStudent->name>>newStudent->Class>>newStudent->addr>>newStudent->phone>>newStudent->QQ>>newStudent->mail;
            newStudent->next=head->next;
            head->next=newStudent;
        }
        inFile.close();
        return head;
    }
    bool Write( Student *head)
    {
        Student *p=head->next;
        ofstream outFile;
        outFile.open("student.txt",ios::out);
        if(!outFile)
        {
            cout<<"Error:opening file fail"<<endl;
            exit(1);
        }
        while(p!=NULL)
        {
            outFile<<p->num<<" "<<p->name<<" "<<p->Class<<" "<<p->addr<<" "<<p->phone<<" "<<p->QQ<<" "<<p->mail<<endl;
            p->next;
        }
        return true;
    }
    int main() 
    {
        cout<<"             请选择下面的菜单:"<<endl;
        cout<<"1 创建"<<endl;
        cout<<"2 添加"<<endl;
        cout<<"3 查询"<<endl;
        cout<<"4 修改"<<endl;
        cout<<"5 删除"<<endl;
        cout<<"6 显示"<<endl;
        cout<<"7 从文件导入"<<endl;
        cout<<"8 导出到文件"<<endl;
        cout<<"9 退出"<<endl;
        cout<<"请选择: ";
        int m;
        int num,phone;
        char name[20];
        bool finished=false;
        Student *head=NULL;
        cin>>m;
        switch(m)
        {
        case 1:
            head=creat();
            break;
        case 2:
            if(Add(head))
                cout<<"添加学生信息成功!"<<endl;
            else
                cout<<"添加学习信息失败!"<<endl;
            break;
        case 3:
            if(Search(head,name,phone))
                cout<<"搜索成功!"<<endl;
            else
                cout<<"搜索失败!"<<endl;
            break;
        case 4:
            cout<<" 请输入要修改的学生通讯录信息的学号:";
            cin>>num;
            if(Search(head,name,phone))
                cout<<"修改成功!"<<endl;
            else
                cout<<"修改失败!"<<endl;
            break;
        case 5:
            if(Delete(head,num))
                cout<<"删除成功!"<<endl;
            else
                cout<<"删除失败!"<<endl;
            break;
        case 6:
            Print(head);
        case 7:
            Read();
            break;
        case 8:
            Write(head);
            break;
        case 9:
            finished=true;
            break;
        default:
            cout<<"输入选择错误,请重新输入!"<<endl;
        }
        return 0;
    }
     
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月1日
  • 已采纳回答 8月24日
  • 创建了问题 8月24日

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog