在添加新记录和访问记录时有一个问题

Having an issue in adding new records and accessing records

本文关键字:记录 有一个 问题 访问记 访问 添加 新记录      更新时间:2023-10-16

我一直在测试我的代码,发现一些我不确定如何修复的问题。看来,每当我添加新记录时,并没有真正添加记录,我都会通过每当添加记录时就对此进行了测试,该程序就是显示所有记录显示了我硬编码为程序的记录。我正在使用链接列表来进行此操作,但我不确定我是否正确实施它。

编辑:我已经进行了一些更改,其余问题之一就是添加。当我更改主要程序

struct student 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

主要程序:

#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;

您有输入错误,不是因为您删除了错误的struct关键字

cin >> tempStudent.ID;如果输入非授权程序并导致无限环路失败。您必须检查输入是否成功,并清除输入是否失败。

//cin >> tempStudent.ID; <- remove this line
while(true) //<- replace with this
{
    cout << "id: ";
    if( (cin >> tempStudent.ID) )
        break;
    cin.clear();
    cin.ignore(0x1000, 'n');
}

您可能需要添加一个函数以执行其他整数输入检查相同的输入错误。

cin.ignore()的其他情况更改为

cin.clear();
cin.ignore(0x1000, 'n');

cin.clear()如果您期望整数输入并且用户意外进入文本。

相关文章: