Помогите пожалуйста с кодом. Нужно создать телефонную книгу. Поля: ФИО, номер телефона, email.
- Заполнение из файла и из консоли
- Сортировка по имени
- Добавление и удаление элементов
- Редактирование элементов
- Поиск элементов
- Вывод на экран
Я написал вот такой код, но он не работает.
#include<sstream>
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct Phone
{
string number;
string FIO;
string email;
};
void Write(const char* filename, Phone *Myphone, int size)
{
string name;
string ini;
string phone_number;
string email;
string rez;
bool end = true;
ofstream fout(filename, ofstream::app);
if (!fout)
{
cout << "Sorry i cant do it\n";
return;
}
else
{
do
{
for (int i = 0; i < size; i++)
{
cout << "Enter surname\n";
getline(cin, name);
cout << "Enter name or initials\n";
getline(cin, ini);
cout << "Enter phone number\n";
getline(cin, phone_number);
cout << "Enter E-mail\n";
getline(cin, email);
rez = name + " " + ini + " " + phone_number + " " + email;
fout << rez << "\n";
cout << "Do you want to continue 1 - Yes 0 - No\n";
cin >> end;
cin.get();
}
} while (end);
}
fout.close();
}
void Read(const char* filename, Phone* Myphone, int size)
{
ifstream fin(filename);
if (!fin)
{
cout << "File can not open\n";
}
else
{
string buff;
string name;
string ini;
int i = 0;
while (!fin.eof())
{
getline(fin, buff);
stringstream ss(buff);
ss >> name;
ss >> ini;
Myphone[i].FIO = name + " " + ini;
ss >> Myphone[i].number;
ss >> Myphone[i].email;
i++;
}
}
fin.close();
}
void Sort(Phone*Myphone , int size)
{
for (int i = 0; i < size; i++)
{
if (Myphone[i].FIO[0] < Myphone[i + 1].FIO[0])
{
swap(Myphone[i], Myphone[i+1]);
}
}
}
void Show(Phone* Myphone, int size)
{
cout << "#\tFIO\t\tPhone Number\tE-mail\n";
for (int i = 0; i < size; i++)
{
cout << i + 1 << "\t" << Myphone[i].FIO << "\t" << Myphone[i].number << "\t" << Myphone[i].email << "\n";
}
}
void Find(Phone* Myphone,int size)
{
bool found = false;
int ans;
string f_name, f_mail, f_number;
cout << "Find by: \n";
cout << "1 - Name\n";
cout << "2 - Number\n";
cout << "3 - E-mail\n";
cin >> ans;
switch (ans)
{
case 1:
cout << "Please enter Name: ";
cin >> f_name;
for (int i = 0; i < size; i++)
{
if (f_name == Myphone[i].FIO)
{
cout << "Found!\n";
cout << Myphone[i].FIO << ' ' << Myphone[i].number << ' ' << Myphone[i].email << "\n";
found = true;
}
}
break;
case 2:
cout << "Please enter Number: ";
cin >> f_number;
for (int i = 0; i < size; i++)
{
if (f_number == Myphone[i].number)
{
cout << "Found!\n";
cout << Myphone[i].FIO << ' ' << Myphone[i].number << ' ' << Myphone[i].email << "\n";
found = true;
}
}
break;
case 3:
cout << "Please enter E-mail: ";
cin >> f_mail;
for (int i = 0; i < size; i++)
{
if (f_mail == Myphone[i].email)
{
cout << "Found!\n";
cout << Myphone[i].FIO << ' ' << Myphone[i].number << ' ' << Myphone[i].email << "\n";
found = true;
}
}
break;
default:
cout << "Wrong command\n";
break;
}
if (!found)
cout << "Sorry, there are no any data\n";
cout << "\n";
cin.clear();
}
void main()
{
int const size = 100;
Phone Myphone[size];
int ans;
do
{
cout << "Enter what you want to do\n";
cout << "1 - Find\n";
cout << "2 - Print\n";
cout << "3 - Add\n";
cout << "4 - Exit\n";
cin >> ans;
cin.get();
switch (ans)
{
case 1:
Read("Phone_book.txt", Myphone, size)
Find(Myphone, size);
break;
case 2:
Read("Phone_book.txt", Myphone, size);
Sort(Myphone, size);
Show(Myphone, size);
break;
case 3:
Write("Phone_book.txt", Myphone, size);
break;
case 4:
cout << "Good bye!\n";
break;
default:
cout << "Wrong choice!\n";
break;
}
} while (ans!=4);
}