Здравствуйте, помогите пожалуйста. Есть код, создал меню, но почему после выбора пунка 1 ничего не происходит, остальные пункты работают, не могу понять, почему он не добавляет AddItem.
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class Firma
{
private:
string company_name;
string owner;
string phone;
string adress;
string occupation;
public:
Firma() = default;
Firma(const string &name, const string &own,
const string &nphone, const string &adr, const string &occup);
void Set(const string &name, const string &own,
const string &nphone, const string &adr, const string &occup);
void SetFirmName(const string &n);
void SetOwnerName(const string &on);
void SetPhone(const string &p);
void SetAdress(const string &a);
void SetOccupation(const string &o);
string GetFirmName() const;
string GetOwnerName() const;
string GetPhone() const;
string GetAdress() const;
string GetOccupation() const;
friend bool operator == (const Firma &lh, const Firma &rh);
friend ostream &operator << (ostream &os, const Firma &f);
};
Firma::Firma(const string &name, const string &own, const string &nphone, const string &adr, const string &occup)
{
Set(name, own, nphone, adr, occup);
}
void Firma::Set(const string &name, const string &own, const string &nphone, const string &adr, const string &occup)
{
company_name = name;
owner = own;
phone = nphone;
adress = adr;
occupation = occup;
}
void Firma::SetFirmName(const string &n) {
company_name = n;
}
void Firma::SetOwnerName(const string &on) {
owner = on;
}
void Firma::SetPhone(const string &p) {
phone = p;
}
void Firma::SetAdress(const string &a) {
adress = a;
}
void Firma::SetOccupation(const string &o) {
occupation = o;
}
string Firma::GetFirmName() const {
return company_name;
}
string Firma::GetOwnerName() const {
return owner;
}
string Firma::GetPhone() const {
return phone;
}
string Firma::GetAdress() const {
return adress;
}
string Firma::GetOccupation() const {
return occupation;
}
bool operator == (const Firma &lh, const Firma &rh)
{
return lh.company_name == rh.company_name
&& lh.owner == rh.owner
&& lh.phone == rh.phone
&& lh.adress == rh.adress
&& lh.occupation == rh.occupation;
}
ostream &operator << (ostream &os, const Firma &f)
{
return os
<< f.company_name << '\n'
<< f.owner << '\n'
<< f.phone << '\n'
<< f.adress << '\n'
<< f.occupation << '\n'
<< endl;
}
class Handbook
{
private:
vector<Firma> items;
public:
void AddItem(const Firma &i);
bool DeleteItem(const Firma &i);
void PrintAll(ostream &os);
bool FindByFirmName(const string &name, Firma &f);
bool FindByOwner(const string &owner, Firma &f);
bool FindByPhone(const string &phone, Firma &f);
bool FindByAdress(const string &adress, Firma &f);
bool FindByOccupation(const string &occupation, Firma &f);
Handbook FindByOwner(const string &owner);
void saveFirms(const string &filename);
void readFromFirms(const string &filename);
};
void Handbook::AddItem(const Firma &i) {
items.push_back(i);
}
bool Handbook::DeleteItem(const Firma &i)
{
for (vector<Firma>::iterator it = items.begin(); it != items.end(); ++it)
if (*it == i) {
items.erase(it);
return true;
}
return false;
}
void Handbook::PrintAll(ostream &os) {
if (items.empty()) {
os << "Handbook is empty";
return;
}
for (vector<Firma>::iterator it = items.begin(); it != items.end(); ++it) {
os << *it;
}
}
bool Handbook::FindByFirmName(const string &name, Firma &f) {
for (vector<Firma>::iterator it = items.begin(); it != items.end(); ++it)
if (it->GetFirmName() == name) {
f = *it;
return true;
}
return false;
}
bool Handbook::FindByOwner(const string &owner, Firma &f) {
for (vector<Firma>::iterator it = items.begin(); it != items.end(); ++it)
if (it->GetOwnerName() == owner) {
f = *it;
return true;
}
return false;
}
bool Handbook::FindByPhone(const string &phone, Firma &f) {
for (vector<Firma>::iterator it = items.begin(); it != items.end(); ++it)
if (it->GetPhone() == phone) {
f = *it;
return true;
}
return false;
}
bool Handbook::FindByAdress(const string &adress, Firma &f) {
for (vector<Firma>::iterator it = items.begin(); it != items.end(); ++it)
if (it->GetAdress() == adress) {
f = *it;
return true;
}
return false;
}
bool Handbook::FindByOccupation(const string &occupation, Firma &f) {
for (vector<Firma>::iterator it = items.begin(); it != items.end(); ++it)
if (it->GetOccupation() == occupation) {
f = *it;
return true;
}
return false;
}
Handbook Handbook::FindByOwner(const string &owner) {
Handbook t;
for (vector<Firma>::iterator it = items.begin(); it != items.end(); ++it)
if (it->GetOwnerName() == owner) {
t.AddItem(*it);
}
return t;
}
void Handbook::saveFirms(const string &filename)
{
ofstream fs;
fs.open(filename, fstream::out);
if (fs.is_open()) {
for (vector<Firma>::iterator it = items.begin(); it != items.end(); ++it) {
fs << "FirmName: " << it->GetFirmName() << endl
<< "OwnerName: " << it->GetOwnerName() << endl
<< "Phone: " << it->GetPhone() << endl
<< "Adress: " << it->GetAdress() << endl
<< "Occupation: " << it->GetOccupation() << endl
<< "---------------------------" << endl;
}
}
fs.close();
}
void Handbook::readFromFirms(const string &filename)
{
ifstream fs;
Firma firma;
string buf;
int j = 0;
items.clear();
fs.open(filename, ios::in);
if (fs.is_open())
{
do
{
fs >> buf >> buf;
firma.SetFirmName(buf);
fs >> buf >> buf;
firma.SetOwnerName(buf);
fs >> buf >> buf;
firma.SetPhone(buf);
fs >> buf >> buf;
firma.SetAdress(buf);
fs >> buf >> buf;
firma.SetOccupation(buf);
fs >> buf;
if (!fs.eof())
items.push_back(firma);
} while (!fs.eof());
}
fs.close();
}
int main() {
setlocale(LC_ALL, "Russian");
char FirmName[30], OwnerName[20], Phone[20], Occupation[20], choice;
string filename = "Proverka.txt";
Handbook h;
Firma f;
do {
cout << "Выберите требуемое действие:\n1-Добавить информацию ...\n2-Поиск по названию фирмы...\n";
cout << "3-Поиск по владельцу...\n4-Поиск по номеру телефона...\n5-Поиск по роду деятельности...\n6-Показать всю информацию...\n7-Выход...\n";
cin >> choice;
switch (choice) {
case '1':
h.AddItem(f);
break;
case '2':
cout << "Введите название фирмы\n";
cin >> FirmName;
h.FindByFirmName(FirmName,f);
break;
case '3':
cout << "Введите владельца\n";
cin >> OwnerName;
h.FindByOwner(OwnerName,f);
break;
case '4':
cout << "Введите телефон\n";
cin >> Phone;
h.FindByPhone(Phone,f);
break;
case '5':
cout << "Введите род деятельности \n";
cin >> Occupation;
h.FindByOccupation(Occupation,f);
break;
case '6':
h.PrintAll(cout);
break;
case '7':
exit(0);
break;
default:
cout << "Неправильный выбор\n";
break;
}
} while (choice != '7');
system("Pause");
return 0;
}
Изначально без меню было во так и все работало
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class Firma
{
private:
string company_name;
string owner;
string phone;
string adress;
string occupation;
public:
Firma() = default;
Firma(const string &name, const string &own,
const string &nphone, const string &adr, const string &occup);
void Set(const string &name, const string &own,
const string &nphone, const string &adr, const string &occup);
void SetFirmName(const string &n);
void SetOwnerName(const string &on);
void SetPhone(const string &p);
void SetAdress(const string &a);
void SetOccupation(const string &o);
string GetFirmName() const;
string GetOwnerName() const;
string GetPhone() const;
string GetAdress() const;
string GetOccupation() const;
friend bool operator == (const Firma &lh, const Firma &rh);
friend ostream &operator << (ostream &os, const Firma &f);
};
Firma::Firma(const string &name, const string &own, const string &nphone, const string &adr, const string &occup)
{
Set(name, own, nphone, adr, occup);
}
void Firma::Set(const string &name, const string &own, const string &nphone, const string &adr, const string &occup)
{
company_name = name;
owner = own;
phone = nphone;
adress = adr;
occupation = occup;
}
void Firma::SetFirmName(const string &n) {
company_name = n;
}
void Firma::SetOwnerName(const string &on) {
owner = on;
}
void Firma::SetPhone(const string &p) {
phone = p;
}
void Firma::SetAdress(const string &a) {
adress = a;
}
void Firma::SetOccupation(const string &o) {
occupation = o;
}
string Firma::GetFirmName() const {
return company_name;
}
string Firma::GetOwnerName() const {
return owner;
}
string Firma::GetPhone() const {
return phone;
}
string Firma::GetAdress() const {
return adress;
}
string Firma::GetOccupation() const {
return occupation;
}
bool operator == (const Firma &lh, const Firma &rh)
{
return lh.company_name == rh.company_name
&& lh.owner == rh.owner
&& lh.phone == rh.phone
&& lh.adress == rh.adress
&& lh.occupation == rh.occupation;
}
ostream &operator << (ostream &os, const Firma &f)
{
return os
<< f.company_name << '\n'
<< f.owner << '\n'
<< f.phone << '\n'
<< f.adress << '\n'
<< f.occupation << '\n'
<< endl;
}
class Handbook
{
private:
vector<Firma> items;
public:
void AddItem(const Firma &i);
bool DeleteItem(const Firma &i);
void PrintAll(ostream &os);
bool FindByFirmName(const string &name, Firma &f);
bool FindByOwner(const string &owner, Firma &f);
bool FindByPhone(const string &phone, Firma &f);
bool FindByAdress(const string &adress, Firma &f);
bool FindByOccupation(const string &occupation, Firma &f);
Handbook FindByOwner(const string &owner);
void saveFirms(const string &filename);
void readFromFirms(const string &filename);
};
void Handbook::AddItem(const Firma &i) {
items.push_back(i);
}
bool Handbook::DeleteItem(const Firma &i)
{
for (vector<Firma>::iterator it = items.begin(); it != items.end(); ++it)
if (*it == i) {
items.erase(it);
return true;
}
return false;
}
void Handbook::PrintAll(ostream &os) {
if (items.empty()) {
os << "Handbook is empty";
return;
}
for (vector<Firma>::iterator it = items.begin(); it != items.end(); ++it) {
os << *it;
}
}
bool Handbook::FindByFirmName(const string &name, Firma &f) {
for (vector<Firma>::iterator it = items.begin(); it != items.end(); ++it)
if (it->GetFirmName() == name) {
f = *it;
return true;
}
return false;
}
bool Handbook::FindByOwner(const string &owner, Firma &f) {
for (vector<Firma>::iterator it = items.begin(); it != items.end(); ++it)
if (it->GetOwnerName() == owner) {
f = *it;
return true;
}
return false;
}
bool Handbook::FindByPhone(const string &phone, Firma &f) {
for (vector<Firma>::iterator it = items.begin(); it != items.end(); ++it)
if (it->GetPhone() == phone) {
f = *it;
return true;
}
return false;
}
bool Handbook::FindByAdress(const string &adress, Firma &f) {
for (vector<Firma>::iterator it = items.begin(); it != items.end(); ++it)
if (it->GetAdress() == adress) {
f = *it;
return true;
}
return false;
}
bool Handbook::FindByOccupation(const string &occupation, Firma &f) {
for (vector<Firma>::iterator it = items.begin(); it != items.end(); ++it)
if (it->GetOccupation() == occupation) {
f = *it;
return true;
}
return false;
}
Handbook Handbook::FindByOwner(const string &owner) {
Handbook t;
for (vector<Firma>::iterator it = items.begin(); it != items.end(); ++it)
if (it->GetOwnerName() == owner) {
t.AddItem(*it);
}
return t;
}
void Handbook::saveFirms(const string &filename)
{
ofstream fs;
fs.open(filename, fstream::out);
if (fs.is_open()) {
for (vector<Firma>::iterator it = items.begin(); it != items.end(); ++it) {
fs << "FirmName: " << it->GetFirmName() << endl
<< "OwnerName: " << it->GetOwnerName() << endl
<< "Phone: " << it->GetPhone() << endl
<< "Adress: " << it->GetAdress() << endl
<< "Occupation: " << it->GetOccupation() << endl
<< "---------------------------" << endl;
}
}
fs.close();
}
void Handbook::readFromFirms(const string &filename)
{
ifstream fs;
Firma firma;
string buf;
int j = 0;
items.clear();
fs.open(filename, ios::in);
if (fs.is_open())
{
do
{
fs >> buf >> buf;
firma.SetFirmName(buf);
fs >> buf >> buf;
firma.SetOwnerName(buf);
fs >> buf >> buf;
firma.SetPhone(buf);
fs >> buf >> buf;
firma.SetAdress(buf);
fs >> buf >> buf;
firma.SetOccupation(buf);
fs >> buf;
if (!fs.eof())
items.push_back(firma);
} while (!fs.eof());
}
fs.close();
}
const char *indata[][5] = {
{ "GAZ", "Goga", "9518130488", "Moscow", "Buy" },
{ "Car", "John", "9518130481", "Kazan", "Sale" },
};
const char *succ = "Success!\n";
const char *fail = "Fail ((\n";
int main() {
setlocale(LC_ALL, "Russian");
string filename = "Proverka.txt";
Handbook h;
Firma f;
for (size_t i = 0; i < sizeof(indata) / sizeof(indata[0]); ++i) {
f.Set(indata[i][0], indata[i][1], indata[i][2], indata[i][3], indata[i][4]);
h.AddItem(f);
}
cout << "--- Initial data:\n";
h.PrintAll(cout);
h.saveFirms(filename);
h.readFromFirms(filename);
cout << "--- Data from file:\n";
h.PrintAll(cout);
cout << "--- Find by FirmName:\n";
if (h.FindByFirmName(indata[1][0], f))
cout << succ << "\"" << indata[1][0] << "\" found\n" << f << endl;
if (h.FindByFirmName(indata[1][1], f))
cout << succ << "\"" << indata[1][1] << "\" found\n" << f << endl;
if (h.FindByFirmName(indata[0][0], f))
cout << succ << "\"" << indata[0][0] << "\" found\n" << f << endl;
if (h.FindByFirmName(indata[0][1], f))
cout << succ << "\"" << indata[0][1] << "\" found\n" << f << endl;
cout << "--- Find by Owner:\n";
if (h.FindByOwner(indata[0][2], f))
cout << succ << "\"" << indata[0][2] << "\" found\n" << f << endl;
if (h.FindByOwner(indata[0][1], f))
cout << succ << "\"" << indata[0][1] << "\" found\n" << f << endl;
if (h.FindByOwner(indata[1][2], f))
cout << succ << "\"" << indata[1][2] << "\" found\n" << f << endl;
if (h.FindByOwner(indata[1][1], f))
cout << succ << "\"" << indata[1][1] << "\" found\n" << f << endl;
cout << "--- Find by Phone:\n";
if (h.FindByPhone(indata[0][3], f))
cout << succ << "\"" << indata[0][3] << "\" found\n" << f << endl;
if (h.FindByPhone(indata[0][2], f))
cout << succ << "\"" << indata[0][2] << "\" found\n" << f << endl;
if (h.FindByPhone(indata[1][3], f))
cout << succ << "\"" << indata[1][3] << "\" found\n" << f << endl;
if (h.FindByPhone(indata[1][2], f))
cout << succ << "\"" << indata[1][2] << "\" found\n" << f << endl;
cout << "--- Find by Adress:\n";
if (h.FindByAdress(indata[0][4], f))
cout << succ << "\"" << indata[0][4] << "\" found\n" << f << endl;
if (h.FindByAdress(indata[0][3], f))
cout << succ << "\"" << indata[0][3] << "\" found\n" << f << endl;
if (h.FindByAdress(indata[1][4], f))
cout << succ << "\"" << indata[1][4] << "\" found\n" << f << endl;
if (h.FindByAdress(indata[1][3], f))
cout << succ << "\"" << indata[1][3] << "\" found\n" << f << endl;
cout << "--- Find by Occupation:\n";
if (h.FindByOccupation(indata[0][5], f))
cout << succ << "\"" << indata[0][5] << "\" found\n" << f << endl;
if (h.FindByOccupation(indata[0][4], f))
cout << succ << "\"" << indata[0][4] << "\" found\n" << f << endl;
if (h.FindByOccupation(indata[1][5], f))
cout << succ << "\"" << indata[1][5] << "\" found\n" << f << endl;
if (h.FindByOccupation(indata[1][4], f))
cout << succ << "\"" << indata[1][4] << "\" found\n" << f << endl;
cout << "--- Find by Owner all occurrences of \"" << indata[1][1] << "\":\n";
Handbook found = h.FindByOwner(indata[1][1]);
found.PrintAll(cout);
cout << "--- Find by Owner all occurrences of \"" << indata[0][1] << "\":\n";
found = h.FindByOwner(indata[0][1]);
found.PrintAll(cout);
system("Pause");
return 0;
}