Wpisywanie adresatow ze struktury do wektora

0

Cześć, napisałem mały program do wpisywania adresatów (struktura) do wektora. Pytanie moje jest jedno - czy działa dobrze?

#include <iostream>
#include <vector>
#include <windows.h>

using namespace std;

struct Adresat
{
    int id = 0;
    string imie = "", nazwisko = "", nr_tel = "", email = "", adres = "";
};

int main()
{
    vector <Adresat> adresat;
    Adresat pobrane;
    string daneAdresata [6];
    cout << "Podaj: ";

    for(int i = 0 ; i < 6; i++)
    {
        for(int i = 0; i < 6; i++)
        {
        getline(cin,daneAdresata[i], '|');
        pobrane.id = atoi(daneAdresata[0].c_str());
        pobrane.imie = daneAdresata[1];
        pobrane.nazwisko = daneAdresata[2];
        pobrane.nr_tel = daneAdresata[3];
        pobrane.email = daneAdresata[4];
        pobrane.adres = daneAdresata[5];
        }
        adresat.push_back(pobrane);
    }
    for(int i = 0; i < adresat.size(); i++)
    {
         cout << "Adresat numer " << i << ": ";
         cout << adresat[i].id << " ";
         cout << adresat[i].imie << " ";
         cout << adresat[i].nazwisko << " ";
         cout << adresat[i].nr_tel << " ";
         cout << adresat[i].email << " ";
         cout << adresat[i].adres << " ";
    }
    return 0;
}

wydaje mi się, że działa bez problemu, jednak jak załączam go do większego programu to już niestety nie działa - różnica jest taka, że tutaj wpisuję bezpośrednio adresata (każdy typ danych czyli id, imie itd. oddzielone są "|") a w większym programie jest to funkcja, do której przesyłam stringa i ma zwrócić mi tak jak tutaj pobrane - funkcja wygląda tak:


Adresat pobierzDaneAdresata(string daneAdresata)
{
    Adresat pobrane;
    int id = 0;
    string imie, nazwisko, nr_tel, email, adres;
    string bufor [6] = daneAdresata;

    for(int i = 0; i < 6; i++)
    {
        getline(cin,bufor[i], '|');
        pobrane.id = atoi(bufor[0].c_str());
        pobrane.imie = bufor[1];
        pobrane.nazwisko = bufor[2];
        pobrane.nr_tel = bufor[3];
        pobrane.email = bufor[4];
        pobrane.adres = bufor[5];
    }
    return pobrane;
}

a większy program daję poniżej:

#include <iostream>
#include <windows.h>
#include <fstream>
#include <cstdlib>
#include <vector>

using namespace std;

struct Adresat
{
    int id = 0;
    string imie = "", nazwisko = "", nr_tel = "", email = "", adres = "";
};

void wprowadzanieDanych(vector <Adresat> &adresaci, int iloscAdresatow)
{
    Adresat ulatwienie;
    iloscAdresatow = adresaci.size();
    ulatwienie.id = iloscAdresatow + 1;

    string imie, nazwisko, nr_tel, email, adres;
    cout << "Podaj imie: ";
    cin >> ulatwienie.imie;
    cout << "Podaj nazwisko: ";
    cin >> ulatwienie.nazwisko;
    cout << "Podaj numer telefonu: ";
    cin.sync();
    getline(cin, ulatwienie.nr_tel);
    cout << "Podaj email: ";
    cin >> ulatwienie.email;
    cout << "Podaj adres: ";
    cin.sync();
    getline(cin, ulatwienie.adres);

    adresaci.push_back(ulatwienie);

    fstream plik;
    plik.open("KsiazkaAdresowa.txt", ios::out | ios::app);
    if (plik.good())
    {
        plik << adresaci[iloscAdresatow].id << "|";
        plik << adresaci[iloscAdresatow].imie << "|";
        plik << adresaci[iloscAdresatow].nazwisko << "|";
        plik << adresaci[iloscAdresatow].nr_tel << "|";
        plik << adresaci[iloscAdresatow].email << "|";
        plik << adresaci[iloscAdresatow].adres << "|";
        plik << endl;
        plik.close();

        cout << "Przyjaciel dopisany do ksiazki adresowej" << endl;
        Sleep(1000);
    }
    else
    {
        cout << "Nie mozna otworzyc pliku: KsiazkaAdresowa.txt" << endl;
    }
}

Adresat pobierzDaneAdresata(string daneAdresata)
{
    Adresat pobrane;
    int id = 0;
    string imie, nazwisko, nr_tel, email, adres;
    string bufor [6] = daneAdresata;

    for(int i = 0; i < 6; i++)
    {
        getline(cin,bufor[i], '|');
        pobrane.id = atoi(bufor[0].c_str());
        pobrane.imie = bufor[1];
        pobrane.nazwisko = bufor[2];
        pobrane.nr_tel = bufor[3];
        pobrane.email = bufor[4];
        pobrane.adres = bufor[5];
    }
    return pobrane;
}

void wczytajAdresatowZPliku(vector<Adresat> &adresaci)
{
    Adresat jedenAdresat;
    string daneAdresata = "";

    fstream plikTekstowy;
    plikTekstowy.open("KsiazkaAdresowa.txt", ios::in);

    if (plikTekstowy.good() == true)
    {
        while (getline(plikTekstowy, daneAdresata))
        {
            jedenAdresat = pobierzDaneAdresata(daneAdresata);

            adresaci.push_back(jedenAdresat);
        }
        plikTekstowy.close();
    }
}

/*void wyswietlAdresatow (vector <Adresat> &adresaci, int iloscAdresatow)
{
    for (int i = 0; i < iloscAdresatow; i++)
    {
        cout << endl;
        cout << "ID: " << adresaci[i].id << endl;
        cout << adresaci[i].imie << " " << adresaci[i].nazwisko << endl;
        cout << "Telefon: " << adresaci[i].nr_tel << endl;
        cout << "Email: " << adresaci[i].email << endl;
        cout << "Adres: " << adresaci[i].adres << endl;
        cout << endl;
    }
}

int szukajPoImieniu(vector <Adresat> &adresaci,int iloscAdresatow)
{
    string imie;
    cin >> imie;
    for( int i = 0; i < iloscAdresatow; i++)
    {
        if (adresaci[i].imie == imie)
        {
            cout << adresaci[i].id << ".";
            cout << adresaci[i].imie;
            cout << adresaci[i].nazwisko;
            cout << adresaci[i].nr_tel;
            cout << adresaci[i].email;
            cout << adresaci[i].adres;
            cout << endl;
        }
    }
    system("pause");
    return 0;
}

int szukajPoNazwisku(vector <Adresat> &adresaci,int iloscAdresatow)
{
    string nazwisko;
    cin >> nazwisko;
    for( int i = 0; i < iloscAdresatow; i++)
    {
        if (adresaci[i].nazwisko == nazwisko)
        {
            cout << adresaci[i].id << ".";
            cout << adresaci[i].nazwisko;
            cout << adresaci[i].imie;
            cout << adresaci[i].nr_tel;
            cout << adresaci[i].email;
            cout << adresaci[i].adres;
            cout << endl;
        }
    }
    system("pause");
    return 0;
}*/

int main()
{
    vector <Adresat> adresaci;
    int iloscAdresatow = 0;
    char wybor;

    while(1)
    {
        system("cls");
        cout << "1. Dodaj adresata" << endl;
        cout << "2. Wyszukaj po imieniu" << endl;
        cout << "3. Wyszukaj po nazwisku" << endl;
        cout << "4. Wyswietl wszystkich adresatow" << endl;
        cout << "5. Usun adresata" << endl;
        cout << "6. Edytuj adresata" << endl;
        cout << "9. Zakoncz program" << endl;
        cout << "Twoj wybor: ";
        cin >> wybor;
        cout << endl;

        if (wybor == '1')
        {
            wprowadzanieDanych(adresaci, iloscAdresatow);
            Sleep(1000);
        }
        else if (wybor == '2')
        {
            //szukajPoImieniu(adresaci,iloscAdresatow);
        }
        else if (wybor == '3')
        {
            //szukajPoNazwisku(adresaci,iloscAdresatow);
        }
        else if (wybor == '4')
        {
            //wczytajAdresatowZPliku(adresaci);
            wyswietlAdresatow(adresaci,iloscAdresatow);
            cout << iloscAdresatow;
        }
        else if (wybor == '5')
        {

        }
        else if (wybor == '6')
        {

        }
        else if (wybor == '9')
        {
            cout << "Dziekuje za skorzystanie z ksiazki adresowej Lista Schindlera" << endl;
            cout << endl;
            exit(0);
        }
    }
    return 0;
}
0

string bufor [6] = daneAdresata;
To się nie kompiluje

    for(int i = 0; i < 6; i++)
    {
        getline(cin,bufor[i], '|');
        pobrane.id = atoi(bufor[0].c_str());
        pobrane.imie = bufor[1];
        pobrane.nazwisko = bufor[2];
        pobrane.nr_tel = bufor[3];
        pobrane.email = bufor[4];
        pobrane.adres = bufor[5];
    }

To nie ma sensu. Pomijając, że to UB, czemu za każdym razem zapisujesz wszystkie elementy? Pierw wczytaj, potem przypisuj.

to już niestety nie działa

Szczątkowy opis problemu

0

Przy kompilowaniu małego programu - wyświetla mi dobrze zawartość wektora - jak dorzucam go jako funkcję w dużym programie - wektor jest pusty :/
Może głupie pytanie ale co to jest UB?nie spotakłem się z tą nazwą.
Czyli najpierw wczytać do bufora a dopiero potem zapisywać do poszczególnych danych?przy sprawdzaniu coutem wypisywał jak trzeba :/ dopiero się uczę dlatego proszę o wyrozumiałość :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1