Making a student management system or something
The files are main, student.cpp, student.h When I add student data, I want to use Link List to implement it I received the menu number through the main door, added the student information inside the switch door, and tried the verification code again at the switch door, and the value became empty... Is it because the memory is allocated within the function in the switch statement?>
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))); //Dummy
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 << "Please enter your gender." (Male: 0, Female: 1) : "; sin >> itempex;
cout << "Please enter your gender. : : "; cin >> chtempsex;
cout << "Please enter your age. : : "; cin >> tempage;
cout << "Please enter your cell phone number. (xxx-xxx-xxx) :"; sin >> tempphone;
cout << "Please enter a name. : : "; cin >> tempname;
cout <<endl<< "Check additional information" <<endl;
cout << "Name\t Gender\t Age\t Cell Phone Number" <<endl;
cout <<tempname <<< "\t"; if (itempex == 0)cout << "men\t"; else cout <<"<"<"\t;"<"<<<<<<<<<<<<<<<<<<<<<<<<<
cout << "Is the information correct? [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 name\t gender\t age\t cell phone number" << 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 << "Student Management System" <<< "Menu" <<< endl << "[1] Student Registration" <<< "[2] Student Deletion" << < "< < < < End; < < < < < < < < < < < < < <"] "3"
}
#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
I checked the modified part.
Kong_Student.h
#ifndefKONG_STUDENT_H
#define KONG_STUDENT_H
#pragma once
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
//Modify header
class Kong_Student
{
private: // public -> private
character name[20]; //modified from pointer to array
char chsex[10];
char phone[20];
int age;
int isex;
Kong_Student* next;
public:
Kong_Student();
~Kong_Student();
voidGetname(char*val) { strcpy(val, name); } //using strcpy
void Setname(char* val) { strcpy(name, val); }
void Getage(int &val) { val = age; }
void Setage(int val) { age = val; }
void Getisex(int &val) { val = isex; }
void Setisex(int val) { isex = val; }
void Getchsex(char* val) { strcpy(val, chsex); }
void Setchsex(char* val) { strcpy(chsex, val); }
void Getphone(char* val) { strcpy(val, phone); }
void Setphone(char* val) { strcpy(phone, val); }
Kong_Student* GetNext(void) { return next; }
void SetNext(Kong_Student* val) { next = val; }
};
Structure List // No need to type the structure in C++
{
Kong_Student* head;
Kong_Student* cur;
Kong_Student* before;
int numOfData;
};
void ListInit(List *plist);
void AddStudent(List *plist);
void StudentListShow(List *plist);
void menuout();
//int comparechar(char* str1, char*str2, int num); // replace with strncmp
#endif // KONG_STUDENT_H
student.cpp
#include "Kong_Student.h"
Kong_Student::Kong_Student()
{
//ctor
}
Kong_Student::~Kong_Student()
{
//dtor
}
void ListInit(List *plist)
{
//plist->head = (Kong_Student*)malloc(sizeof(Kong_Student))); //Dummy
plist->head = new Kong_Student();
plist->head->SetNext (NULL); //next is SetNext
plist->numOfData = 0;
}
/*
int comparechar (char* str1, char*str2, int num) // replace with strncmp
{
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));
char ok[2] = "N";
while (!strncmp(ok, "N", 1)) // If N is entered, continue to enter information
{
char chtempsex[10];
char tempname[20];
char tempphone[20];
int itempsex;
int tempage;
cout << "Please enter your gender." (Male: 0, Female: 1) : "; sin >> itempex;
cout << "Please enter your gender. : : "; cin >> chtempsex;
cout << "Please enter your age. : : "; cin >> tempage;
cout << "Please enter your cell phone number. (xxx-xxx-xxx) :"; sin >> tempphone;
cout << "Please enter a name. : : "; cin >> tempname;
cout <<endl<< "Check additional information" <<endl;
Cout < < "the name \ \ old t t t sex cell phone number" < <. ; endl
cout << tempname << "\t"; if (itempsex == 0)cout << "남\t"; else cout << "여\t"; cout << tempage << "\t" << tempphone << "\t" << endl << endl;
Cout < <. ; "Information? [n and y]"
cin >> ok;
If (! (ok strncmp, "y", 1)) after entering the information // y is entered, an object was created and add to the list.
{
Kong_Student* Newstudent = new Kong_Student;
Newstudent -> Setname(tempname);
Newstudent -> Setage(tempage);
Newstudent -> Setisex(itempsex);
Newstudent -> Setchsex(chtempsex);
Newstudent -> Setphone(tempphone);
- > newstudent setnext (- > plist head - > getnext,)) ; // next getnext, bringing.
plist -> head -> SetNext(Newstudent);
(plist -> numOfData)++;
plist -> cur = plist -> head -> GetNext();
break;
}
}
}
void StudentListShow(List *plist)
{
char name[20]; //Change from pointer to array
char gender[5];
char phone[20];
int age;
cout << "No\t name\t gender\t age\t cell phone number" << endl;
//cout << plist->numOfData;
Kong_Student *st = plist->cur;
For (inti = 0; st; i++) //st if NULL, exit
{
st->Getname(name);
cout<<i+1<<"\t"<<name;
st->Getchsex(gender);
cout<<"\t"<<gender;
st->Getage(age);
cout<<"\t"<<age;
st->Getphone(phone);
cout<<"\t"<<phone << endl;
st = st -> GetNext();
}
}
void menuout()
{
cout <<"\nStudent Management System" <<<< "Menu" <<< endl << "[1] Student Registration" <<< "[2] Student Deletion" <<<< < < < "< < < < < < < < < < < < < < < < <
}
The main function remains the same, only the grammar is modified, and the logic remains the same.
Oh, and the system function is cleared.
© 2024 OneMinuteCode. All rights reserved.