current position:Home>Questions about student information management system C++

Questions about student information management system C++

2022-02-03 00:14:02 CSDN Q & A

  1. Why does this student management system only add correct functions , But can't insert and other functions be used ?
  2. How to modify
  3. How to enable initial information to be invoked from file , After updating, it is stored in a new file
#include<iostream>#include<stdlib.h>using namespace std;int k=1; struct student{    int num;    char name[30];    struct student *next;}; class stu{     private:         struct student *head,*tail;     public:         stu(){    head=new student;    head->num=0;    head->name[0]='\0';         };         void create(int n);        void print();        int Insert();        void del();        void destroy(); }; void stu:: create(int n){         struct student *p;    head->next=NULL;    tail=head;     for(int i=0;i<n;i++){        p=new student;        cout<<"input num,name"<<endl;        cin>>p->num>>p->name;        cout<<endl;        p->next=NULL;        tail->next=p;        tail=p;    }}void stu::print(){    struct student *p;    p=head->next;    if(p==NULL){        cout<<"kongbiao"<<endl;        return;    }     while(p!=NULL){        cout<<p->num<<'\t'<<p->name<<endl;        p=p->next;    }}int stu::Insert(){    system("cls");    cout<<" Please enter the information to be inserted after the student number "<<endl;    int sno;    cin>>sno;    struct student *newp=new student,*p;    cout<<" Please enter the information to insert "<<endl;     cin>>newp->num>>newp->name;    p=head->next;    while(p!=NULL){        if(p->num==sno){            newp->next=p->next;            p->next=newp;                        break;        }        p=p->next;    }      return 0;     }void stu::del(){    cout<<" Please enter the student number to delete "<<endl;    int sno;    cin>>sno;    struct student *p,*pre;    p=head->next;    pre=head;    while(p!=NULL){        if(p->num==sno){        pre->next=p->next;        delete p;        break;        }        pre=p;        p=p->next;    } }void stu::destroy(){    struct student *p,*ptr;    p=head;    while(p!=NULL){        ptr=p;        p=ptr->next;        delete ptr;    }}void menu()/*  Interface */{cout<<"1. add to "<<endl; cout<<"2. Print "<<endl;cout<<"3. Insert "<<endl;cout<<"4. Delete "<<endl;cout<<"5. The destruction "<<endl;cout<<"6. sign out "<<endl;int num;    stu STU;cout<<" Please enter your choice "<<endl;cin>>num;switch(num){case 1:    int n;    cout<<" Please enter the number "<<endl;    cin>>n;    STU.create(n);    STU.print();    break;case 2:    STU.print();    break;case 3:    STU.Insert();    STU.print();    break;case 4:    STU.del();    STU.print();    break;case 5:    STU.destroy();    STU.print();    break;case 6:    k=0;        cout<<" About to quit the program !"<<endl;        break;    default:    cout<<" Please be there. 1~6 Choose between "<<endl;    break;}}int main(){while(k){menu();}system("pause");return 0;}

img

img

    OK·1    Enter where you want to learn    Please enter the information to insert     wang           exited after 18.23 seconds with retul Evaluator                  Compile log                         V Debug according to the technical results                     error   close                     Evaluation time :                             0.55         Enter what you want to search here                    Have chosen                                 A line song 

1.




Refer to the answer 1:

hold void stu::create(int n) Function modification , Instead, load the linked list from the file , The remaining changes are as follows , For reference :

#include<iostream>#include<stdlib.h>using namespace std;int k = 1;struct student { // modify     int  num;    char name[30];    struct student* next;};class stu {private:    struct student* head, * tail;public:    stu() {        head = new struct student; // modify         head->num = 0;        head->name[0] = '\0';        head->next = NULL;  // modify     };    void create(int n);    void print();    int  Insert();    void del();    void destroy();};void stu::create(int n) {    struct student* p;             //head->next = NULL;  modify     tail = head;    for (int i = 0; i < n; i++) {        p = new struct student;  // modify         cout << "input num,name" << endl;        cin >> p->num >> p->name;        cout << endl;        p->next = NULL;        tail->next = p;        tail = p;    }}void stu::print(){    if (head->next == NULL) { // modify         cout << "kongbiao" << endl;        return;    }    struct student* p;    p = head->next;    while (p != NULL) {        cout << p->num << '\t' << p->name << endl;        p = p->next;    }}int stu::Insert() {    system("cls");    if (head->next == NULL) { // modify         cout << "kongbiao" << endl;        return -1;    }    cout << " Please enter the information to be inserted after the student number " << endl;    int sno;    cin >> sno;    struct student* p = head->next;    while (p != NULL){         if (p->num == sno) {            struct student* newp = new struct student; // modify              newp->next = NULL;                                  cout << " Please enter the information to insert :num,name" << endl;            cin >> newp->num >> newp->name;            newp->next = p->next;             p->next = newp;            break;        }        p = p->next;    }    if (p == NULL)                                   // modify         printf(" Student number :%d  Record not found , Insert failed !\n", sno);    return 0;}void stu::del() {    if (head->next == NULL) {       // modify         cout << "kongbiao" << endl;        return;    }    struct student* p, * pre;    p = head->next;    pre = head;    cout << " Please enter the student number to delete " << endl;    int sno;    cin >> sno;    while (p != NULL) {        if (p->num == sno) {            pre->next = p->next;            delete p;            printf(" Student number :%d  Record deleted successfully !\n", sno);// modify             return;  //break; // modify         }        pre = p;        p = p->next;    }    if (p == NULL)                                  // modify         printf(" Student number :%d  Record not found , Deletion failed !\n", sno);}void stu::destroy() {    struct student* p, * ptr;    //p = head; // modify     while (head->next != NULL) {        ptr = head->next;        head->next = ptr->next;        delete ptr;    }}void menu(stu STU)/*  Interface */   // modify {    cout << "1. add to " << endl;    cout << "2. Print " << endl;    cout << "3. Insert " << endl;    cout << "4. Delete " << endl;    cout << "5. The destruction " << endl;    cout << "6. sign out " << endl;    int num;    //stu STU; // modify     cout << " Please enter your choice " << endl;    cin >> num;    switch (num) {    case 1:        int n;        cout << " Please enter the number " << endl;        cin >> n;        STU.create(n);        STU.print();        break;    case 2:        STU.print();        break;    case 3:        STU.Insert();        STU.print();        break;    case 4:        STU.del();        STU.print();        break;    case 5:        STU.destroy();        STU.print();        break;    case 6:        k = 0;        cout << " About to quit the program !" << endl;        break;    default:        cout << " Please be there. 1~6 Choose between " << endl;        break;    }}int main() {    stu STU;    // modify     while (k) {        menu(STU);    }    system("pause");    return 0;}



Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030013587228.html

Random recommended