Так он же перезаписывать весь файл должен, а не добавлять еще раз весь массив.
beginer_38:
number = number[i];
что-то странное тут
Про ввод я так имел в виду:
#include<sstream>
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct Phone
{
string number;
string FIO;
string email;
};
int Add(Phone* Myphone, int size)
{
string name;
string ini;
string number;
string email;
cout << "Enter surname\n";
getline(cin, name);
cout << "Enter name or initials\n";
getline(cin, ini);
cout << "Enter phone number\n";
getline(cin, number);
cout << "Enter E-mail\n";
getline(cin, email);
Myphone[size].FIO = name + " " + ini;
Myphone[size].number = number;
Myphone[size].email = email;
size++;
return size;
}
void Rewrite(Phone* Myphone, int size)
{
string res;
ofstream fout("Phone_book.txt", ofstream::trunc);
if (!fout)
{
cout << "Sorry i cant do it\n";
return;
}
else
{
for (int i = 0; i < size; i++)
{
res = Myphone[i].FIO + " " + Myphone[i].number + " " + Myphone[i].email;
fout << res << "\n";
}
}
fout.close();
}
int Read(const char* filename, Phone* Myphone)
{
int size = 0;
ifstream fin(filename);
if (!fin)
{
cout << "Your Phone book is empty\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++;
size++;
}
}
fin.close();
return size;
}
void Delete(Phone* Myphone, int size, int index)
{
string res;
for (int i = index; i < size - 1; i++)
{
Myphone[i].FIO = Myphone[i + 1].FIO;
Myphone[i].number = Myphone[i + 1].number;
Myphone[i].email = Myphone[i + 1].email;
}
for (int i = 0; i < size - 1; i++)
{
res = Myphone[i].FIO + " " + Myphone[i].number + " " + Myphone[i].email;
}
cout << "Contact was succesfully deleted\n";
}
void Edit(Phone* Myphone, int size, int index)
{
string res;
string new_name;
string new_number;
string new_email;
for (int i = index; i < index + 1; i++)
{
Myphone[i].FIO = "";
Myphone[i].number = "";
Myphone[i].email = "";
cout << "Enter new name\n";
getline(cin, new_name);
cout << "Enter new number\n";
cin >> new_number;
cout << "Enter new E-mail\n";
cin >> new_email;
Myphone[i].FIO = new_name;
Myphone[i].number = new_number;
Myphone[i].email = new_email;
}
cout << "Contact was succesfully edited\n";
}
void Sort(const char* filename, Phone* Myphone, int size)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - 1; j++)
{
if (Myphone[j].FIO > Myphone[j + 1].FIO)
{
swap(Myphone[j], Myphone[j + 1]);
}
}
}
}
void Show(Phone* Myphone, int size)
{
cout << "#\tFIO\t\tPhone Number\t\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";
}
}
int Find(Phone* Myphone, int size)
{
bool found = false;
int ans;
int index = 0;
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;
cin.get();
switch (ans)
{
case 1:
cout << "Please enter Name: ";
getline(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;
index = i;
}
}
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;
index = i;
}
}
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;
index = i;
}
}
break;
default:
cout << "Wrong command\n";
break;
}
if (!found)
cout << "Sorry, there are no any data\n";
cout << "\n";
cin.clear();
return index;
}
void main()
{
int const size = 100;
Phone Myphone[size];
int index = 0;
int count = Read("Phone_book.txt", Myphone);
int ans;
do
{
cout << "Enter what you want to do\n";
cout << "1 - Find number\n";
cout << "2 - Show all numbers\n";
cout << "3 - Add number\n";
cout << "4 - Delete number\n";
cout << "5 - Edit number\n";
cout << "6 - Exit\n";
cin >> ans;
cin.get();
switch (ans)
{
case 1:
Find(Myphone, count);
break;
case 2:
Show(Myphone, count);
break;
case 3:
count = Add(Myphone, count);
Sort("Phone_book.txt", Myphone, count);
Rewrite(Myphone, count);
break;
case 4:
index = Find(Myphone, count);
Delete(Myphone, count, index);
count--;
Rewrite(Myphone, count);
break;
case 5:
index = Find(Myphone, count);
Edit(Myphone, count, index);
Sort("Phone_book.txt", Myphone, count);
Rewrite(Myphone, count);
break;
case 6:
cout << "Good bye!\n";
break;
default:
cout << "Wrong choice!\n";
break;
}
} while (ans != 6);
}
Добавил возврат size из Add на случай если захочется вводить больше 1 или проверять корректность ввода.