przekazanie wektora przez referencje i brak danych w danym wektorze

0

Witam mam problem z przekazanie wektora bo ja dodaje do niego dane to on cały czas wskazuje na size = 0. Wektora jest w klasie Readable

//
// Created by mateusz on 14.01.19.
//

#include "Readable.h"

Readable::Readable() {};
Readable::~Readable() {};

void Readable::addRecordToVectorLogin(const string &line, vector<recordLogin> & v) {
    vector<string> splitString = split(line, ',');
    size_t size = splitString.size();
    string nick;
    for (int index = 0; index < size ; ++index) {
        cout << "index " << index << endl;
        if (index%2 > 0) {
            cout << "rs" << endl;
            recordLogin r = {nick, splitString[index]};
            cout << "nick " << r.nick << endl;
            cout << "password " << r.password << endl;
            v.push_back(r);
        } else {
            nick = splitString[index];
        }
    }

    for (const auto elem : v) {
        cout << "test " << elem.nick << endl;
        cout << "test " << elem.password << endl;
    }

}

vector<string> Readable::split(const string& s, char delimiter)
{
    vector<string> tokens;
    string token;
    istringstream tokenStream(s);
    while (getline(tokenStream, token, delimiter))
    {
        tokens.push_back(token);
    }
    return tokens;
}

void Readable::test() {
    cout << "1" << endl;
    size_t s = getVector().size();
    cout << "size " << s << endl;
    for (int index = 0; index < s ; ) {
        cout << "wazne "<< getVector()[index].nick << endl;
        cout << "wazne " << getVector()[index+1].password << endl;
        index = index + 2;
    }
}
//
// Created by mateusz on 14.01.19.
//

#ifndef HOMEBUDGET_READABLE_H
#define HOMEBUDGET_READABLE_H

#include <string>
#include <vector>
#include <sstream>
#include <iostream>

using namespace std;

class Readable {
private:
    string delimeter = ",";
public:
    Readable();
    ~Readable();

    struct recordLogin {
        string nick;
        string password;
    };

    vector<recordLogin> vRecordLogin;

    void addRecordToVectorLogin(const string & line, vector<recordLogin> & v);
    vector<recordLogin> & getVector() { return vRecordLogin; }
    vector<string> split(const std::string& s, char delimiter);
    void test();
};




#endif //HOMEBUDGET_READABLE_H
//
// Created by mateusz on 12.01.19.
//

#ifndef HOMEBUDGET_FILECSV_H
#define HOMEBUDGET_FILECSV_H

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

#include "Readable.h"

using namespace std;

class FileCSV {
private:
    string pathToFile;
    string delimeter = ",";
    fstream file;
    Readable readable;
public:

    FileCSV();
    ~FileCSV();

    void setPathToFile(const string & pathToFile) { this->pathToFile = pathToFile; };
    inline string getPathToFile() { return pathToFile; };
    inline string getDelimeter() { return delimeter; };

    bool open(const string & pathToFile);
    void read(vector<string> &v);
    void save(const string & pathToFile, const string & record);
};


#endif //HOMEBUDGET_FILECSV_H
//
// Created by mateusz on 12.01.19.
//

#include "FileCSV.h"

FileCSV::FileCSV() {};
FileCSV::~FileCSV() {};

bool FileCSV::open(const string & pathToFile) {
    setPathToFile(pathToFile);
    file.open(getPathToFile(), ios::in | ios::out);
    if (!file.is_open()) {
        cout << "Error: Can't open the file or File doesn't exist" << endl;
        return false;
    }
    return true;
}

void FileCSV::read(vector<string> &v) {
    string line;
    while ( getline (file,line) ) {
        readable.addRecordToVectorLogin(line, readable.vRecordLogin);
    }
    file.close();
}

void FileCSV::save(const string & pathToFile, const string & record) {
    bool a = open(pathToFile);
}
//
// Created by mateusz on 12.01.19.
//

#ifndef HOMEBUDGET_CONTROLLER_H
#define HOMEBUDGET_CONTROLLER_H

#include "Login.h"
#include "Ui.h"
#include "FileCSV.h"
#include "Readable.h"

class Controller {
private:
    Login login;
    Ui ui;
    Readable readable;
    string nick;
    string password;

public:
    Controller();
    ~Controller();

    void mainLoop();
    void loopLogin();

};


#endif //HOMEBUDGET_CONTROLLER_H
//
// Created by mateusz on 12.01.19.
//

#include "Controller.h"

Controller::Controller() {};
Controller::~Controller() {};

void Controller::loopLogin() {
    readable.vRecordLogin;
    FileCSV fileCSV;
    bool isOpenFile = fileCSV.open(login.getPathToLoginsFile());
    if (!isOpenFile) {
        exit(EXIT_FAILURE);
    }

    fileCSV.read(login.getVectorAllLoginCSV());
    cout << "ok" << endl;
    readable.test();

    ui.showLogin();

    cout << "login: ";
    login.setNick(cin);
    cout << "\npassword: ";
    login.setPassword(cin);
    cout << endl;
    
    if (login.comparisonOfNickAndPasswordWithCVS(login.getNick(), login.getPassword())) {
        ui.successLogin();
    } else {
        ui.failLogin();
        char option;
        cin >> option;
        option = toupper(option);
        switch (option) {
            case 'Y':
                cout << "tak" << endl;
            case 'N':
                cout << "nie" << endl;
            default:
                cout << "Wrong answer" << endl;
        }
    }

}

0

Już wiem co było źle po prostu, były dwa obiekty Readable i dlatego nie wiedział skąd ma brać dane.

1

Proponuję zapoznać się z debuggerem - wtedy stracisz o wiele mniej czasu na poszukiwanie tego typu problemów i nie będziesz musiał wypisywać stanu programu w różnych miejscach (technika ta czasami nazywana metodą echo dupa :)).

0

Ciężko pojąć ten kod.
Funkcje robią x rzeczy naraz i mają mało intuicyjne argumenty
klasa Login trzyma wektor wszystkich loginów
Readable to powinien być interfejs a nie klasa "końcowa"

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