Hej,
mam problem z zadaniem poniżej , a mianowicie z tym że nie zapisuje mi danych do pliku txt. i nie potrafię dojść dlaczego?
stworzenie programu: „Książka adresowa”.
Dzień dobry,
Chciałbym zlecić Panu stworzenie programu o nazwie „Książka adresowa”, w której będę mógł zapisywać dane moich przyjaciół i znajomych.
Niech program zapisuje takie dane jak imię mojego przyjaciela, jego nazwisko, numer telefonu, email oraz adres
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <limits>
using namespace std;
struct Person
{
int id = 0;
string firstName, lastName, phoneNumber, email, address;
};
//
vector<Person> readFromFile()
{
vector<Person> people;
ifstream file ("address_book.txt");
if (file.is_open())
{
string line;
while (getline (file, line)) // ksi¹¿ka strona 309
{
Person person;
person.id = stoi(line.substr(0, line.find(','))); // podpowiedz ze stackoverflow
line.erase(0, line.find(',') + 1);
person.firstName = line.substr(0, line.find(','));
line.erase(0, line.find(',') + 1);
person.lastName = line.substr(0, line.find(','));
line.erase(0, line.find(',') + 1);
person.phoneNumber = line.substr(0, line.find(','));
line.erase(0, line.find(',') + 1);
person.email = line.substr(0, line.find(','));
line.erase(0, line.find(',') + 1);
person.address = line;
people.push_back(person);
}
file.close();
}
return people;
}
void saveToFile(vector<Person>& people)
{
ofstream file("address_book.txt");
if (file.is_open())
{
for (int i = 0; i < people.size(); i++)
{
file << "ID: " << people[i].id << endl;
file << "Imie: " << people[i].firstName << endl;
file << "Nazwisko: " << people[i].lastName << endl;
file << "Nr telefonu: " << people[i].phoneNumber << endl;
file << "Mail: " << people[i].email << endl;
file << "Adres: " << people[i].address << endl;
}
file.close();
cout << "Dane zostaly zapisane do pliku." << endl;
}
else
{
cout << "Blad: nie udalo sie otworzyc pliku." << endl;
}
}
void addPerson(vector<Person>& people, int &idCounter)
{
Person person;
person.id = idCounter++;
cout << "Podaj imie: ";
cin >> person.firstName;
cout << "Podaj nazwisko: ";
cin >> person.lastName;
cout << "Podaj numer telefonu: ";
cin >> person.phoneNumber;
cout << "Podaj e-mail: ";
cin.ignore(numeric_limits<streamsize>::max(),'\n');// forum pasja informatyki https://forum.pasja-informatyki.pl/183036/jak-zrobic-aby-nie-wczytywano-liter
getline(cin, person.email);
cout << "Podaj adres: ";
getline(cin, person.address);
cout << endl;
people.push_back(person);
cout << "Osoba dodana do ksiazki adresowej." << endl;
cout << endl;
}
void displayPeople(const Person &person) // const - definiowanie sta³ych
{
cout << "ID: " << person.id << endl;
cout << "Imie: " << person.firstName << endl;
cout << "Nazwisko: " << person.lastName << endl;
cout << "Numer telefonu: " << person.phoneNumber << endl;
cout << "Adres e-mail: " << person.email << endl;
cout << "Adres: " << person.address << endl;
}
void displayAllPeople( vector<Person>& people)
{
if (people.empty())
{
cout << "Ksiazka adresowa jest pusta." << endl;
return;
}
cout << "Oto lista Twoich znajomych:" << endl;
for (const auto& person : people)
{
displayPeople(person);
cout << endl;
}
}
void searchByFirstName(vector<Person>& people, const string& name)
{
vector<Person> results;
for (const auto& person : people)
{
if (person.firstName == name)
{
results.push_back(person);
}
}
if (results.empty())
{
cout << "Nie znaleziono osob o podanym imieniu." << endl;
return;
}
cout << "Oto wyniki wyszukiwania:" << endl;
for (const auto& Person : results)
{
displayAllPeople(results);
cout << endl;
}
}
void searchByLastName(vector<Person>& people, const string& lastName)
{
vector<Person> results;
for (const auto& Person : people)
{
if (Person.lastName == lastName)
{
results.push_back(Person);
}
}
if (results.empty())
{
cout << "Nie znaleziono osob o podanym nazwisku." << endl;
return;
}
cout << "Oto wyniki wyszukiwania:" << endl;
for (const auto& Person : results)
{
displayAllPeople(results);
cout << endl;
}
}
int main()
{
vector<Person> people = readFromFile();
int idCounter = people.empty() ? 1 : people.back().id+1; // forum pasja informatyki
char choice;
while (true)
{
cout << "Twoja Ksiazka Adresowa " << endl;
cout << "1. Dodaj nowa osobe " << endl;
cout << "2. Wyszukaj po imieniu" << endl;
cout << "3. Wyszukaj po nazwisku" << endl;
cout << "4 .Wyszystkie kontakty" << endl;
cout << "5. Zakoncz program " << endl;
cin >> choice;
switch (choice)
{
case '1':
addPerson(people, idCounter);
break;
case '2':
{
string firstName;
cout << "Podaj imie: ";
cin >> firstName;
searchByFirstName(people, firstName);
break;
}
case '3':
{
string lastName;
cout << "Podaj nazwisko: ";
cin >> lastName;
searchByLastName(people, lastName);
break;
}
case '4':
displayAllPeople(people);
break;
case '5':
cout << "Dziekujemy za skorzystanie z programu!" << endl;
return 0;
default:
cout << "Nieprawidlowy wybor. Sprobuj ponownie." << endl;
break;
}
}
return 0;
}
Chciałbym móc wyszukiwać dane mojego przyjaciela, po tym jak wpiszę jego imię lub jego nazwisko. Czyli program ma mieć 2 różne opcje. Pierwsza: podaj imię – mają zostać wyświetlone wszystkie osoby mające tak na imię. Druga: podaj nazwisko – program ma wyświetlić osoby mające takie nazwisko.
Chciałbym również, aby program miał opcję wyświetlania wszystkich moich przyjaciół.
Proszę również o to, aby program zapisywał te dane do pliku tekstowego oraz odczytywał je zaraz po uruchomieniu programu , abym nie musiał ich wpisywać przy każdym uruchomieniu programu.
wydawało mi się, że program działa poprawnie bo funkcje menu działają poprawnie wg mnie jednak nie widzę pliku txt w folderze :/