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