0

I got a problem, when im reading data from txt file into linked list queue program(main.cpp, queue.cpp, queue.h). So I tried two methods, but it doesn't work properly. In txt file I got some lines, for example four. Firstable, we read data from file, then call display() function in switch loop.

First number in line is the number by which we will call data in display().

Example:

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

Firstable I tried this, but with eof() it's a problem, when it reads last line two times:

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);
                    }
                }

Then I tried another method:

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);
                    }
                }

But it also has mistakes like doesn't show lines with number one when I call it in another function:

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;
            }
        }
  • Is this an academic exercise to re-implement a queue? There's an awful lot of pointers here. – tadman Mar 24 '21 at 12:52
  • "_But it also has mistakes like doesn't show lines with number one when I call it in another function_" Do you realize, that in your second attempt, you try to read twice, back-to-back, while ignoring the result from your first reading attempt? – Algirdas Preidžius Mar 24 '21 at 12:52
  • @AlgirdasPreidžius because how I told that Im calling function from queue.cpp in main.cpp by this way – one1three3 Mar 24 '21 at 12:57
  • @tadman I know;) – one1three3 Mar 24 '21 at 12:58
  • Well, pointers in C++ are generally something you want to avoid. They carry a *lot* of baggage. Any reason you can't use `std::vector` or `std::list`? – tadman Mar 24 '21 at 12:59
  • @tadman Problem is about that I'm only learning programming, so I dont't know many things. And also trying to do this exercise how I know – one1three3 Mar 24 '21 at 13:02
  • @one1three3 "_because how I told that Im calling function from queue.cpp in main.cpp by this way_" What? I wasn't asking about how you are calling function. `while (input >> key >> type >> quantity >> price) { input >> temp->key >> temp->type >> temp->quantity >> temp->price;` reads twice, and ignores the first read attempt. You are comparing this, with `while (!input.eof()) {`, while doing different things, and being surprised that they changed in a different way, than you expected. – Algirdas Preidžius Mar 24 '21 at 13:03
  • 1
    Well, the question is are you trying to learn how to implement a linked list, or do you just need a linked list to learn about something else? Just want to know what to focus on here. If you're just in need of a list, they already exist, so you can skip that part. If you want to learn about implementing one you need to show us more of your code. – tadman Mar 24 '21 at 13:03
  • Tip: Instead of variables like `item1`, `item2`, etc. use an array. Write code in terms of *data structures*, not variable names. – tadman Mar 24 '21 at 13:04
  • @AlgirdasPreidžius Understood this. Yes this is a mistake, but I'm just trying to read data and exploring ways how can I do this – one1three3 Mar 24 '21 at 13:10
  • @one1three3 `while (input >> key >> type >> quantity >> price)` should be sufficient read-and-check construct for you, as explained in: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Algirdas Preidžius Mar 24 '21 at 13:18
  • @AlgirdasPreidžius Yes I did this, but when im calling display() function, and give in option number 1 it writes that there is no sentense with one – one1three3 Mar 24 '21 at 14:58
  • @one1three3 Do some debugging by inspecting your linked list after every `enqueue` by calling a `display()`(or better using a debugger) on the corresponding list and verify if all the elements are being inserted properly. That would tell you whether there is a problem with your insertion logic. – Zoso Mar 24 '21 at 17:13
  • @Zoso I got two lines with number one, this is the reason why it doesnt work, but I tried to delete one, and when I have like in first place it write that queue is empty – one1three3 Mar 24 '21 at 18:37
  • @one1three3 Then there's a problem with your queue logic. You need to debug that. – Zoso Mar 24 '21 at 19:35

1 Answers1

0

I got this:

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);
                    }
                }
            }