Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <conio.h>
- #include <windows.h>
- #include "fstream"
- #include "string"
- using namespace std;
- int num = 0;
- char** name = new char*[num];
- char** phone = new char*[num];
- bool yes = true;
- int taskNumber;
- //struct cell
- //{
- // int num = 0;
- //};
- //
- //struct NamePhone
- //{
- // cell number;
- // char** name = new char*[number.num];
- // char** phone = new char*[number.num];
- //};
- HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
- void list();
- int newList();
- void addList();
- void deleteList();
- void Edit(int& num);
- void searchList();
- void saveList();
- char* getString();
- void addName(char**& names, char**& phone, char* name, char* phone2, int& num);
- void addName2(char**& names, char**& phone, char* name, char* phone2);
- void main()
- {
- /*if (!fin.is_open())
- {
- cout << "no" << endl;
- }
- else
- {
- cout << "yes" << endl;
- char ch;
- while (fin.get(ch))
- {
- cout << ch;
- }
- }
- Sleep(5000);*/
- /*getline(f, s);
- f.close();
- cout << s << endl;*/
- FILE* fileNum = fopen("data.bin", "rb");
- fscanf(fileNum, "%i", &num);
- fclose(fileNum);
- string file = "name.bin";
- ifstream fin;
- fin.open(file);
- string str;
- int i = 0;
- while (!fin.eof())
- {
- fin >> name[i];
- i++;
- }
- fin.close();
- /*for (int i = 0; i < num; i++)
- {
- fin >> name[i];
- }
- for (int i = 0; i < num; i++)
- {
- fin >> phone[i];
- }*/
- /*if (num != 0) {
- file = fopen("name.bin", "rb");
- for (int i = 0; i < num; i++)
- {
- fscanf(file, "%s\n", name[i]);
- }
- fclose(file);
- file = fopen("number.bin", "rb");
- for (int i = 0; i < num; i++)
- {
- fscanf(file, "%s\n", phone[i]);
- }
- fclose(file);
- }*/
- while (true)
- {
- int selection = newList();
- system("cls");
- switch (selection)
- {
- case 0: {
- cout << "List: " << endl << endl;
- list();
- cout << endl;
- break;
- }
- case 1: {
- addList();
- break;
- }
- case 2: {
- deleteList();
- break;
- }
- case 3: {
- Edit(num);
- break;
- }
- case 4: {
- searchList();
- break;
- }
- case 5: {
- saveList();
- break;
- }
- default:
- cout << "Error!" << endl;
- break;
- }
- system("pause");
- }
- }
- void list()
- {
- for (int i = 0; i < num; i++)
- {
- cout << i + 1 << ". " << name[i] << "\t" << phone[i] << endl;
- }
- }
- int newList()
- {
- char menu[6][30] =
- {
- " List ",
- " Add ",
- " Remove ",
- " Edit ",
- " Search ",
- " Save "
- };
- int selection = 0;
- while (true)
- {
- system("cls");
- for (int i = 0; i < 6; i++)
- {
- if (i == selection)
- {
- SetConsoleTextAttribute(h, 124);
- cout << menu[i] << endl;
- SetConsoleTextAttribute(h, 7);
- }
- else
- {
- cout << menu[i] << endl;
- }
- }
- int key = _getch();
- if (key == 224)
- {
- key = _getch();
- if (key == 72)
- {
- selection--;
- if (selection < 0)
- selection = 5;
- }
- if (key == 80)
- {
- selection++;
- selection = selection % 6;
- }
- }
- else if (key == 13)
- {
- return selection;
- }
- }
- }
- void addList()
- {
- cout << "Enter name: ";
- char* name2 = getString();
- cout << "Enter number: ";
- char* phone2 = getString();
- if (yes)
- addName(name, phone, name2, phone2, num);
- else
- addName2(name, phone, name2, phone2);
- }
- char* getString()
- {
- char* name2;
- char buffer[1000];
- cin.getline(buffer, 1000);
- name2 = new char[strlen(buffer) + 1];
- strcpy(name2, buffer);
- return name2;
- }
- void deleteList()
- {
- cout << "Remove: " << endl << endl;
- list();
- cout << endl;
- if (num == 0)
- cout << "You list is empty" << endl << endl;
- else
- {
- int taskNumber;
- cout << "Enter task number: ";
- cin >> taskNumber;
- taskNumber--;
- if (taskNumber >= num)
- cout << "Error!" << endl;
- else
- {
- for (int i = taskNumber; i < num; i++)
- {
- if (i + 1 < num)
- {
- strcpy(name[i], name[i + 1]);
- strcpy(phone[i], phone[i + 1]);
- }
- else if (i + 1 == num)
- {
- strcpy(name[i], " ");
- strcpy(phone[i], " ");
- }
- }
- num--;
- }
- cin.ignore();
- }
- }
- void addName(char**& name, char**& phone, char* name2, char* phone2, int& num)
- {
- char** temp = new char*[num + 1];
- for (int i = 0; i < num; i++)
- {
- temp[i] = name[i];
- }
- delete[] name;
- name = temp;
- name[num] = name2;
- char** temp2 = new char*[num + 1];
- for (int i = 0; i < num; i++)
- {
- temp2[i] = phone[i];
- }
- delete[] phone;
- phone = temp2;
- phone[num] = phone2;
- num++;
- }
- void addName2(char**& name, char**& phone, char* name2, char* phone2)
- {
- name[taskNumber] = name2;
- phone[taskNumber] = phone2;
- yes = true;
- }
- void Edit(int& num)
- {
- cout << "Edit: " << endl << endl;
- list();
- cout << endl;
- if (num == 0)
- cout << "You list is empty" << endl << endl;
- else
- {
- cout << "Enter task number: ";
- cin >> taskNumber;
- taskNumber--;
- if (taskNumber >= num || taskNumber < 0)
- cout << "Error!" << endl;
- else
- {
- strcpy(name[taskNumber], " ");
- strcpy(phone[taskNumber], " ");
- }
- cin.ignore();
- yes = false;
- addList();
- }
- }
- void searchList()
- {
- }
- void saveList()
- {
- FILE* file = fopen("data.bin", "wb");
- char buffer[10];
- _itoa(num, buffer, 10);
- fputs(buffer, file);
- fclose(file);
- file = fopen("name.bin", "wb");
- for (int i = 0; i < num; i++)
- {
- fprintf(file, "%s\n", name[i]);
- }
- fclose(file);
- file = fopen("number.bin", "wb");
- for (int i = 0; i < num; i++)
- {
- fprintf(file, "%s\n", phone[i]);
- }
- fclose(file);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement