The problem is as follows. I created a program, or rather a list, which displays the elements from the file by pressing the "a" key. Question: how to make, that after the deduced element by pressing on "a", by pressing on "d" to delete the last displayed elements in turn?

include <fstream> #include <iostream> #include <windows.h> #include <conio.h> using namespace std; struct el { string data; el *adres; }; class List { private: el * fadr, *eadr; public: List() { fadr = NULL; eadr = NULL; } void fadd(string a) { el *v; v = new el; v->data = a; v->adres = fadr; fadr = v; } void print() { if (fadr == NULL) { cout << "List is emptyn"; return; } el *v; for (v = fadr; v != NULL; v = v->adres) cout << v->data << " "; cout << endl; } }; int main() { ifstream input("c:\f.txt"); string s; while (!input.eof()) { char v = getch(); if (v != 'a') continue; getline(input, s); cout << s << endl; if (v != 'd') continue; // Вот тут что писать, чтобы он убрал последний выведенный элемент? } system("PAUSE"); return 0; } 
  • @ navi1893, you probably after getch () need to do something like switch (v) {case 'a': // get, add-to-list ... break; case 'd': // delete-from-list ... break: ....} - avp
  • Yes, but what code is needed to delete the last displayed items one by one? - navi1893
  • one
    You have written fadd () for the list. Write now fdel (). If you want to erase the text from the console, then you can position to the (pre-stored) position of the beginning of the text on the screen and wipe the text with spaces, and then move the cursor to that position again. But, IMHO, you don't really think about doing it. Or do you write a text editor? - avp
  • one
    Well, @ navi1893, telepathy is a tricky thing. Ask a question about windows clear screen. - avp
  • one
    Is it possible to do something like this? char v = getch (); if (v! = 'a') continue; getline (input, s); cout << s << endl; because I then want to remove the address from the code. If possible, advise me the code corresponding to this - navi1893

1 answer 1

If you use the standard std::list container, then the last added item is removed from the list by the pop_back method, which you need to add to the “d” click handler.

 list<int> l; l.push_back(0); // Добавили l.pop_back(0); // Удалили 
  • I posted the code here and the desired comment. Can you look at it and say it already? Posted by: List.pop_back (); but gives errors. - navi1893
  • one
    You don’t even create a list in main, how can you display something? - fogbit
  • @fogbit it gets them from the file, note there ifstream input ("c: \ f.txt"); - navi1893
  • one
    @navi1893 from the file where the lines are read and are not put into any list. Decide exactly what you need: so that the string is not delivered to the list or so that the string output to the console is deleted from the console by pressing 'd'? - fogbit
  • so that the line displayed on the console is deleted from the console by pressing 'd', this is necessary - navi1893