将txt文件读入链表队列

Reading txt file into linked list queue

当我从 txt 文件读取数据到链表队列程序(main.cpp、queue.cpp、queue.h)时遇到问题。所以我尝试了两种方法,但都不能正常工作。 在 txt 文件中,我得到了一些行,例如四行。首先,我们从文件中读取数据,然后在 switch 循环中调用 display() 函数。

行中的第一个数字是我们将在 display() 中调用数据的数字。

示例:

1 B 14 15.68
2 B 3 23.54 
1 S 5 13.2
3 B 4 9.99

Firstable 我试过了,但是对于 eof() 这是个问题,当它读取最后一行两次时:

while (!input.eof()) {
                    input >> temp->key >> temp->type >> temp->quantity >> temp->price;
                    if (temp->key == 1) {
                        item1.enqueue(temp->key, temp->type, temp->quantity, temp->price);
                    }
                    else if (temp->key == 2) {
                        item2.enqueue(temp->key, temp->type, temp->quantity, temp->price);
                    }
                    else {
                        item3.enqueue(temp->key, temp->type, temp->quantity, temp->price);
                    }
                }

然后我尝试了另一种方法:

while (input >> key >> type >> quantity >> price) {
                    input >> temp->key >> temp->type >> temp->quantity >> temp->price;
                    if (temp->key == 1) {
                        item1.enqueue(temp->key, temp->type, temp->quantity, temp->price);
                    }
                    else if (temp->key == 2) {
                        item2.enqueue(temp->key, temp->type, temp->quantity, temp->price);
                    }
                    else {
                        item3.enqueue(temp->key, temp->type, temp->quantity, temp->price);
                    }
                }

但它也有错误,比如在另一个函数中调用它时不显示第一行:

main.cpp

if (option == 1) {
                item1.display();
            }
            else if (option == 2) {
                item2.display();
            }
            else if (option == 3) {
                item3.display();
            }

queue.cpp

while (ptr != NULL) {
            if (ptr->key == 1) {
                //cout << key << type << quantity ....
                cout << ptr->key << " " << ptr->type << " " << ptr->quantity << " " << ptr->price << endl;
                ptr = ptr->next;
            }
            else if (ptr->key == 2) {
                cout << ptr->key << " " << ptr->type << " " << ptr->quantity << " " << ptr->price << endl;
                ptr = ptr->next;
            }
            else if (ptr->key == 3) {
                cout << ptr->key << " " << ptr->type << " " << ptr->quantity << " " << ptr->price << endl;
                ptr = ptr->next;
            }
        }

我知道了:

while (!input.eof()) {
                if (input >> temp->key >> temp->type >> temp->quantity >> temp->price) {
                    if (temp->key == 1) {
                        item1.enqueue(temp->key, temp->type, temp->quantity, temp->price);
                    }
                    else if (temp->key == 2) {
                        item2.enqueue(temp->key, temp->type, temp->quantity, temp->price);
                    }
                    else if (temp->key == 3) {
                        item3.enqueue(temp->key, temp->type, temp->quantity, temp->price);
                    }
                }
            }