I need to create a directory of files through a linear list, each file must have a field named (name), what is "inside" the file (content), as well as the time it was created and the counter how many times this file was opened. Implemented the interface in the console through _getch ().
For some reason, when a user enters more than one word for the name or content fields through the console, as I implemented, they all, starting from the second, become new nodes in the list. Please help solve this problem. Here is my code:
#include <iostream> #include <iomanip> #include <conio.h> #include <windows.h> using namespace std; class file { public: char name[64]; char content[256]; int count; int day, month, year, min, hour; file *next; }; file *add(file *head) //добавление узла в список { file *pv = new file; pv->next = 0; cout << "Name of the file: "; cin >> pv->name; pv->count = 0; cout << "Input the content of the file: " << endl;; cin >> pv->content; SYSTEMTIME st; GetSystemTime(&st); pv->day = st.wDay; pv->month = st.wMonth; pv->year = st.wYear; pv->hour = st.wHour; pv->min = st.wMinute; if(head) { file *temp = head; while(temp->next) temp = temp->next; temp->next = pv; } else head = pv; return head; } void print(file *head) //печать списка { if(head == 0) { system("cls"); cout << "The catalog is empty!"; } else { while(head != 0){ cout << head->name << "\t" << "\t" << head->day << "." << head->month << "." << head->year << " " << head->hour << ":" << head->min << "\t" << "\t" << head->count << endl; head = head->next; } } } void open_cont(file *head) //вывод "внутренностей" файла при вводе имени файла { file *ptr = head; char buff[64]; cout << "Input the name of the file: "; cin >> buff; bool flag = true; while(head != NULL) { if(strstr(ptr->name, buff)) { system("cls"); ptr->count++; cout << ptr->content; flag = false; } head = head->next; } if(flag == true) cout << "No match!"; } void clean(file *head) //удаление всего списка { if(head != 0) { clean(head->next); delete head; } } int main() { file *head = 0; char ckey; do { system("cls"); cout << "1 - Open file\n"; cout << "2 - Add file to the catalog\n"; cout << "3 - Open the catalog\n"; cout << "5 - Exit\n"; ckey = _getch(); system("cls"); if(ckey == '1') open_cont(head); else if(ckey == '2') head = add(head); else if(ckey == '3') { cout << "Name" << "\t" << "\t" << "Date" << "\t" << "\t" << "\t" << "Count" << endl; print(head); } else if(ckey == '5') { clean(head); } else continue; _getch(); } while(ckey != '5'); system("pause"); return 0; }