Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <conio.h>
- #include <fstream>
- #if __has_include(<regex>)
- #define has_regex true
- #include <regex>
- #endif
- using namespace std;
- struct Friends {
- string familiq;
- string telefon;
- };
- auto input_friends(Friends *, size_t) -> void;
- auto output_friends(Friends *, size_t) -> void;
- auto search_friends(Friends *, size_t ) -> void;
- auto change_number(Friends *, size_t) -> void;
- int main() {
- size_t choice, count(0);
- Friends *friends = nullptr;
- do {
- system("cls");
- cout << "Menu: \n\n";
- cout << "[1] Input friends\n";
- cout << "[2] Search in file\n";
- cout << "[3] Change telephone number\n";
- cout << "[4] Output information from the file\n\n";
- cout << "[0] Exit\n";
- cout << "Your choice: ";
- cin >> choice;
- switch (choice) {
- case 1:
- system("cls");
- cout << "Enter number of friends: ";
- cin >> count;
- if (!cin || !count)
- cout << "Error: invalid number of friends";
- friends = new Friends[count];
- input_friends(friends, count);
- cout << "\n\nPress any key to get back to the menu!";
- _getch();
- break;
- case 2:
- system("cls");
- search_friends(friends, count);
- cout << "\n\nPress any key to get back to the menu!";
- _getch();
- break;
- case 3: {
- system("cls");
- change_number(friends, count);
- cout << "\n\nPress any key to get back to the menu!";
- _getch();
- break;
- }
- case 4:
- system("cls");
- output_friends(friends, count);
- cout << "\n\nPress any key to get back to the menu!";
- _getch();
- break;
- }
- } while (choice != 0);
- return 0;
- }
- auto input_friends(Friends *friends, size_t count) -> void {
- fstream file("friends.dat", ios::binary | ios::out);
- cin.ignore();
- for (size_t i = 0; i < count; i++) {
- bool isCorrect(false);
- cout << "Enter Surname of Friend #" << i << " : ";
- getline(cin, friends[i].familiq);
- do {
- cout << "Enter telephone number: ";
- getline(cin, friends[i].telefon);
- #if has_regex
- if (regex_match(friends[i].telefon, static_cast<regex> ("[0-0]{1}[8-8]{1}[0-9]{8}")))
- for (size_t j = 0; j < count - 1; j++)
- if (friends[j].telefon == friends[i].telefon && j != i)
- cout << "\n\nThe telephone number must be 10 characters and UNIQUE!\n\n";
- else
- isCorrect = true;
- #else
- if (friends[i].telefon.length() == 10) {
- for(size_t j = 0; j < count - 1; j++)
- if(friends[j].telefon == friends[i].telefon && j != i)
- isCorrect = true;
- else
- cout << "\n\nThe telephone number must be 10 characters and UNIQUE!\n\n";
- }
- #endif
- } while (!isCorrect);
- }
- file.write(reinterpret_cast<char *> (&friends), count * sizeof(Friends));
- file.close();
- }
- auto output_friends(Friends *friends, size_t count) -> void {
- fstream file("friends.dat", ios::binary | ios::in);
- file.read(reinterpret_cast<char *> (&friends), count * sizeof(Friends));
- for (size_t i = 0; i < count; i++)
- cout << "Familiq: " + friends[i].familiq + " | Telefon: " + friends[i].telefon << endl;
- file.close();
- }
- auto search_friends(Friends *friends, size_t count) -> void {
- fstream file("friends.dat", ios::binary | ios::in);
- file.read(reinterpret_cast<char *> (&friends), count * sizeof(Friends));
- size_t counter(0);
- for (size_t i = 0; i < count; i++) {
- if (strstr(friends[i].telefon.c_str(), "0876")) {
- cout << "Familiq: " + friends[i].familiq + " | Telefon: " + friends[i].telefon << endl;
- }
- else
- counter++;
- }
- if (counter == 0)
- cout << "There are no friends with number that starts with \"0876\"";
- file.close();
- }
- auto change_number(Friends *friends, size_t count) -> void {
- fstream file("friends.dat", ios::binary | ios::in);
- file.read(reinterpret_cast<char *> (&friends), count * sizeof(Friends));
- file.close();
- file.open("friends.dat", ios::binary | ios::out);
- cin.ignore();
- size_t counter(0);
- string telephone_number;
- bool isCorrect(false);
- cout << "Enter telephone number to change: ";
- getline(cin, telephone_number);
- for (size_t i = 0; i < count; i++) {
- if (telephone_number == friends[i].telefon) {
- counter++;
- cout << "Current telephone number: " << friends[i].telefon << " | Friend Surname: " << friends[i].familiq << "\n\n";
- do {
- cout << "Enter the new telephone number: ";
- getline(cin, friends[i].telefon);
- #if has_regex
- if (regex_match(friends[i].telefon, static_cast<regex> ("[0-0]{1}[8-8]{1}[0-9]{8}")))
- for (size_t j = 0; j < count - 1; j++)
- if (friends[j].telefon == friends[i].telefon && j != i)
- cout << "\n\nThe telephone number must be 10 characters and UNIQUE!\n\n";
- else
- isCorrect = true;
- #else
- if (friends[i].telefon.length() == 10) {
- for (size_t j = 0; j < count; j++)
- if (friends[j].telefon == friends[i].telefon && j != i)
- isCorrect = true;
- else
- cout << "\n\nThe telephone number must be 10 characters and UNIQUE!\n\n";
- }
- #endif
- } while (!isCorrect);
- }
- }
- if (counter == 0)
- cout << "There is no friend with that telephone number!";
- file.write(reinterpret_cast<char *> (&friends), count * sizeof(Friends));
- file.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement