코드를 테스트 해본 결과 해결 방법이 확실하지 않은 몇 가지 문제점을 발견했습니다. 새 레코드를 추가 할 때마다 레코드가 실제로 추가되지는 않습니다. 레코드가 추가 될 때마다이를 테스트했습니다.이 프로그램은 모든 레코드를 표시하는 것입니다. 프로그램에 하드 코드 된 레코드를 보여줍니다. 그렇게하기 위해 연결된 목록을 사용하고 있지만 올바르게 구현하고 있는지 확신 할 수 없습니다.새 레코드를 추가하고 레코드에 액세스하는 데 문제가 있습니다.
수정 : 수정 : 나머지 사항 중 하나는 추가 사항입니다. 주 프로그램을 변경했을 때
구조 학생 student1, tempStudent; 추가를 선택하고 첫 번째 항목을 입력하면 프로그램이 무한 루프가됩니다.
.H 파일 :
#ifndef SLIST_H
#define SLIST_H
#include <string>
#include <iostream>
using namespace std;
class Student
{
public:
int ID;
string lastName;
string firstName;
string phoneNumber;
string major;
float GPA;
int year;
int month;
int date;
string address;
};
class Node
{
public:
struct Student data;
Node *next;
Node();
struct Student GetData();
void SetData(struct Student);
friend class LinkedList;
};
class LinkedList
{
public:
int length;
Node *currentPos;
Node *head;
Node *tail;
LinkedList();
~LinkedList();
int LengthIs();
void MakeEmpty();
void AddToTail(struct Student);
void AddToHead(struct Student);
int SearchByID(struct Student);
void DeleteFromHead();
void DeleteFromTail();
void Delete(int);
Node GetNext();
bool IsLast();
void Reset();
void PrintAll(int, string);
};
void LinkedList::AddToTail(struct Student item)
{
Node *ptr = new Node();
ptr->SetData(item);
if (length == 0)
{
tail = ptr;
head = ptr;
length++;
return;
}
tail->next = ptr;
tail = ptr;
length++;
}
void LinkedList::AddToHead(struct Student item)
{
Node *ptr = new Node;
ptr->SetData(item);
ptr->next = head;
head = ptr;
if (length == 0) tail = ptr;
length++;
}
#endif
주요 프로그램 : 잘못된 struct
키워드를 제거하기 때문에 당신은 입력 오류가
#include "slist.h"
#include <iostream>
using namespace std;
int main()
{
LinkedList students;
int choice = 1;
Student student1, tempStudent;
Node student1Node;
int ret;
student1.ID = 12345678;
student1.firstName = "Daenerys";
student1.lastName = "Targaryen";
student1.phoneNumber = "111-123-1234";
student1.major = "PScience";
student1.GPA = 3.9;
student1.year = 2000;
student1.month = 11;
student1.date = 30;
student1.address = "1234 Harpy Way Mereen, OK 74701";
students.AddToHead(student1);
while (choice != 6)
{
cout << "What would you like to do?" << endl;
cout << "1: Add a student record." << endl;
cout << "2: Remove a student record." << endl;
cout << "3: List all students." << endl;
cout << "4: List the student(s) by major or by ID." << endl;
cout << "5: Order the list." << endl;
cout << "6: Exit!" << endl << endl;
cout << "Make your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Warning: there's a lot of data entry. Try to keep up with what you're entering."
<< endl;
cout << "Please enter in the student's ID, first name, last name, phone number and major ON SEPARATE LINES";
cout << endl;
cin >> tempStudent.ID;
cin >> tempStudent.firstName;
cin >> tempStudent.lastName;
cin >> tempStudent.phoneNumber;
cin.ignore();
getline(cin, tempStudent.major);
cout << "Almost done! Enter the student's gpa, birth year, birth month (IN DIGITS!!!), birth date, and address ON SEPARATE LINES.";
cout << endl;
cin >> tempStudent.GPA;
cin >> tempStudent.year;
cin >> tempStudent.month;
cin >> tempStudent.date;
cin.ignore();
getline(cin, tempStudent.address);
students.AddToHead(tempStudent);
cout << "Student entry added!" << endl;
students.PrintAll(-1, "");
break;
'if (current-> data.major! = major);'세미콜론은 매우 나쁜 생각입니다. – user4581301
질문을 다시 작성하여 정확히 한 가지 문제에만 집중하십시오. 추가로 시작하여 추가 기능이 작동하지 않는다는 것과 관련없는 모든 코드를 제거하는 것이 좋습니다. 종종이 시점에서 당신은 답을 바로 잡을 수있는 코드가 거의 없지만 약간의 코드 만 남겨두면 다른 사람이 아마도 당신을위한 답을 훨씬 빨리 얻을 것입니다. 코드를 작성하고 모든 입력 내용을 복제하십시오. – user4581301
'Student'는 클래스이므로'struct Student student1; '을 선언하지 마십시오. 그냥 'Student student1;'을 입력하십시오. –