편집 기록

편집 기록
  • 프로필 Ch.님의 편집
    날짜2018.04.20

    연결리스트로 학생정보 관리 프로그램 만드는데 할당된 메모리 값이 바껴버려요..


    학생관리 시스템 같은걸 만드는데

    파일은 main, student.cpp, student.h 이렇게 3개 쓰고있고 학생 데이터 추가할떄 링크드리스트이용해서 구현하려고 하는데 main 문을 통해서 menu 번호 받아서 switch문 안에서 학생정보추가 하고 다시 switch문에서 확인 코드 로 해보니 값이 비워져 버려요... 스위치문 안의 함수내부에서 메모리 할당을 해서 그런걸까요 ?>

    int main()
    {
        List list;
        ListInit(&list);
    
        int menunumber = 0;
    
    
        while (1)
        {
            menuout(); cin >> menunumber; system("cls");
            switch (menunumber)
            {
    
            case 1:AddStudent(&list); break;
            case 2:break;
            case 3:StudentListShow(&list); break;
            case 4: break;
            case 5: return 0;
            }
        }
    
    
        return 0;
    }
    
    "Kong_Student.cpp"
    #pragma once
    #include "Kong_Student.h"
    #include <iostream>
    
    using namespace std;
    
    Kong_Student::Kong_Student()
    {
        //ctor
    }
    
    Kong_Student::~Kong_Student()
    {
        //dtor
    }
    
    void ListInit(List *plist)
    {
        //plist->head = (Kong_Student*)malloc(sizeof(Kong_Student)); //더미
        plist->head = new Kong_Student();
        plist->head->next = NULL;
        plist->numOfData = 0;
    
    }
    
    
    
    
    
    //////////////////////////
    int comparechar(char* str1, char*str2, int num)
    {
        for (int i = 0; i<num; i++)
        {
            if (*(str1 + i) != *(str2 + i))return 0;
        }
        return 1;
    }
    void AddStudent(List *plist)
    {
        //Kong_Student* Newstudent = (Kong_Student*)malloc(sizeof(Kong_Student));
        Kong_Student* Newstudent = new Kong_Student;
        Newstudent->next = plist->head->next;
        plist->head->next = Newstudent;
        (plist->numOfData)++;
        plist->cur = plist->head->next;
    
        char ok[2];
        while (!comparechar(ok, "Y", 1))
        {
            int itempsex;
            char chtempsex[5];
            int tempage;
            char tempname[20];
            char tempphone[20];
    
            cout << "성별을 입력해주세요.(남 : 0 , 여 : 1) : ";     cin >> itempsex;
            cout << "성별을 입력해주세요. : ";                      cin >> chtempsex;
            cout << "나이을 입력해주세요. : ";                      cin >> tempage;
            cout << "휴대폰 번호를 입력해주세요.(xxx-xxxx-xxxx) :"; cin >> tempphone;
            cout << "이름을 입력해주세요. : ";                      cin >> tempname;
    
            cout << endl << "추가 정보 확인" << endl;
            cout << "이름\t성별\t나이\t휴대폰번호" << endl;
            cout << tempname << "\t"; if (itempsex == 0)cout << "남\t"; else cout << "여\t"; cout << tempage << "\t" << tempphone << "\t" << endl << endl;
            cout << "정보가 맞습니까 ? [Y/N] ";
            cin >> ok;
    
            if (comparechar(ok, "Y", 1))
            {
                Newstudent->Setname(tempname);
                Newstudent->Setage(tempage);
                Newstudent->Setisex(itempsex);
                Newstudent->Setchsex(chtempsex);
                Newstudent->Setphone(tempphone);
            }
    
            system("cls");
        }
    
    }
    
    void StudentListShow(List *plist)
    {
        cout << "No\t이름\t성별\t나이\t휴대폰번호" << endl;
        cout << plist->numOfData;
        for (int i = 0; i<plist->numOfData; i++)
        {
            char* tempname = plist->cur->Getname();
            cout<<i+1<<"\t"<<tempname;
        }
    
    }
    void menuout()
    {
        cout << "학생관리 시스템" << endl << "Menu" << endl << endl << "[1] 학생등록" << endl << "[2] 학생삭제" << endl << "[3] 학생리스트" << endl << "[4] 정보삭제" << endl << "[5] 종료" << endl << endl << "메뉴를 선택하여 주세요 : ";
    }
    
    #ifndef KONG_STUDENT_H
    #define KONG_STUDENT_H
    #pragma once
    #include "stdlib.h"
    
    
    class Kong_Student
    {
    public:
        Kong_Student();
        ~Kong_Student();
    
        char* Getname() { return name; }
        void Setname(char* val) { name = val; }
        int Getage() { return age; }
        void Setage(int val) { age = val; }
        int Getisex() { return isex; }
        void Setisex(int val) { isex = val; }
        char* Getchsex() { return chsex; }
        void Setchsex(char* val) { chsex = val; }
        char* Getphone() { return phone; }
        void Setphone(char* val) { phone = val; }
    
        Kong_Student* next;
    protected:
    
    public:
        char* name;
        int age;
        int isex;
        char* chsex;
        char* phone;
    };
    
    typedef struct _linkedLIst
    {
        Kong_Student* head;
        Kong_Student* cur;
        Kong_Student* before;
        int numOfData;
    }LinkedLIst;
    
    typedef LinkedLIst List;
    
    void ListInit(List *plist);
    
    
    int comparechar(char* str1, char*str2, int num);
    void AddStudent(List *plist);
    void StudentListShow(List *plist);
    void menuout();
    
    #endif // KONG_STUDENT_H
    
  • 프로필 알 수 없는 사용자님의 편집
    날짜2018.04.19

    연결리스트로 학생정보 관리 프로그램 만드는데 할당된 메모리 값이 바껴버려요..


    학생관리 시스템 같은걸 만드는데

    파일은 main, student.cpp, student.h 이렇게 3개 쓰고있고 학생 데이터 추가할떄 링크드리스트이용해서 구현하려고 하는데 main 문을 통해서 menu 번호 받아서 switch문 안에서 학생정보추가 하고 다시 switch문에서 확인 코드 로 해보니 값이 비워져 버려요... 스위치문 안의 함수내부에서 메모리 할당을 해서 그런걸까요 ?>

    int main()
    {
        List list;
        ListInit(&list);
    
        int menunumber = 0;
    
    
        while (1)
        {
            menuout(); cin >> menunumber; system("cls");
            switch (menunumber)
            {
    
            case 1:AddStudent(&list); break;
            case 2:break;
            case 3:StudentListShow(&list); break;
            case 4: break;
            case 5: return 0;
            }
        }
    
    
        return 0;
    }
    
    "Kong_Student.cpp"
    #pragma once
    #include "Kong_Student.h"
    #include <iostream>
    
    using namespace std;
    
    Kong_Student::Kong_Student()
    {
        //ctor
    }
    
    Kong_Student::~Kong_Student()
    {
        //dtor
    }
    
    void ListInit(List *plist)
    {
        //plist->head = (Kong_Student*)malloc(sizeof(Kong_Student)); //더미
        plist->head = new Kong_Student();
        plist->head->next = NULL;
        plist->numOfData = 0;
    
    }
    
    
    
    
    
    //////////////////////////
    int comparechar(char* str1, char*str2, int num)
    {
        for (int i = 0; i<num; i++)
        {
            if (*(str1 + i) != *(str2 + i))return 0;
        }
        return 1;
    }
    void AddStudent(List *plist)
    {
        //Kong_Student* Newstudent = (Kong_Student*)malloc(sizeof(Kong_Student));
        Kong_Student* Newstudent = new Kong_Student;
        Newstudent->next = plist->head->next;
        plist->head->next = Newstudent;
        (plist->numOfData)++;
        plist->cur = plist->head->next;
    
        char ok[2];
        while (!comparechar(ok, "Y", 1))
        {
            int itempsex;
            char chtempsex[5];
            int tempage;
            char tempname[20];
            char tempphone[20];
    
            cout << "성별을 입력해주세요.(남 : 0 , 여 : 1) : ";     cin >> itempsex;
            cout << "성별을 입력해주세요. : ";                      cin >> chtempsex;
            cout << "나이을 입력해주세요. : ";                      cin >> tempage;
            cout << "휴대폰 번호를 입력해주세요.(xxx-xxxx-xxxx) :"; cin >> tempphone;
            cout << "이름을 입력해주세요. : ";                      cin >> tempname;
    
            cout << endl << "추가 정보 확인" << endl;
            cout << "이름\t성별\t나이\t휴대폰번호" << endl;
            cout << tempname << "\t"; if (itempsex == 0)cout << "남\t"; else cout << "여\t"; cout << tempage << "\t" << tempphone << "\t" << endl << endl;
            cout << "정보가 맞습니까 ? [Y/N] ";
            cin >> ok;
    
            if (comparechar(ok, "Y", 1))
            {
                Newstudent->Setname(tempname);
                Newstudent->Setage(tempage);
                Newstudent->Setisex(itempsex);
                Newstudent->Setchsex(chtempsex);
                Newstudent->Setphone(tempphone);
            }
    
            system("cls");
        }
    
    }
    
    void StudentListShow(List *plist)
    {
        cout << "No\t이름\t성별\t나이\t휴대폰번호" << endl;
        cout << plist->numOfData;
        for (int i = 0; i<plist->numOfData; i++)
        {
            char* tempname = plist->cur->Getname();
            cout<<i+1<<"\t"<<tempname;
        }
    
    }
    void menuout()
    {
        cout << "학생관리 시스템" << endl << "Menu" << endl << endl << "[1] 학생등록" << endl << "[2] 학생삭제" << endl << "[3] 학생리스트" << endl << "[4] 정보삭제" << endl << "[5] 종료" << endl << endl << "메뉴를 선택하여 주세요 : ";
    }
    
    #ifndef KONG_STUDENT_H
    #define KONG_STUDENT_H
    #pragma once
    #include "stdlib.h"
    
    
    class Kong_Student
    {
    public:
        Kong_Student();
        ~Kong_Student();
    
        char* Getname() { return name; }
        void Setname(char* val) { name = val; }
        int Getage() { return age; }
        void Setage(int val) { age = val; }
        int Getisex() { return isex; }
        void Setisex(int val) { isex = val; }
        char* Getchsex() { return chsex; }
        void Setchsex(char* val) { chsex = val; }
        char* Getphone() { return phone; }
        void Setphone(char* val) { phone = val; }
    
        Kong_Student* next;
    protected:
    
    public:
        char* name;
        int age;
        int isex;
        char* chsex;
        char* phone;
    };
    
    typedef struct _linkedLIst
    {
        Kong_Student* head;
        Kong_Student* cur;
        Kong_Student* before;
        int numOfData;
    }LinkedLIst;
    
    typedef LinkedLIst List;
    
    void ListInit(List *plist);
    
    
    int comparechar(char* str1, char*str2, int num);
    void AddStudent(List *plist);
    void StudentListShow(List *plist);
    void menuout();
    
    #endif // KONG_STUDENT_H