在添加新记录和访问记录时遇到问题

时间:2017-12-11 06:14:16

标签: c++ linked-list

我一直在测试我的代码,我发现了一些我不确定如何修复的问题。似乎每当我添加一条新记录时,记录都没有被添加,我已经通过添加记录来测试这个,程序是显示所有记录,它看起来就像它只是显示我硬编码到程序中的记录。我正在使用链接列表,但我不确定我是否正确实施。

编辑:我做了一些更改,剩下的问题之一是添加。当我对主程序进行更改时

  
    

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;

1 个答案:

答案 0 :(得分:1)

您输入错误,不是因为您删除了错误的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()