• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

przekazanie wektora przez referencje i brak danych w danym wektorze

Object Storage Arubacloud
0 głosów
243 wizyt
pytanie zadane 18 stycznia 2019 w C i C++ przez 87kelthuzad Obywatel (1,270 p.)

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;
        }
    }
 
}

 

1 odpowiedź

0 głosów
odpowiedź 18 stycznia 2019 przez 87kelthuzad Obywatel (1,270 p.)
Już wiem co było źle po prostu, były dwa obiekty Readable i dlatego nie wiedział skąd ma brać dane.

Podobne pytania

0 głosów
2 odpowiedzi 788 wizyt
pytanie zadane 28 stycznia 2019 w C i C++ przez Michał_Warmuz Mądrala (5,830 p.)
0 głosów
1 odpowiedź 288 wizyt
pytanie zadane 8 lutego 2018 w C i C++ przez robzon1916 Początkujący (310 p.)
0 głosów
2 odpowiedzi 1,278 wizyt
pytanie zadane 21 lutego 2016 w C i C++ przez dichloroetylobenzen Użytkownik (850 p.)

92,555 zapytań

141,403 odpowiedzi

319,554 komentarzy

61,939 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Kolejna edycja największej imprezy hakerskiej w Polsce, czyli Mega Sekurak Hacking Party odbędzie się już 20 maja 2024r. Z tej okazji mamy dla Was kod: pasjamshp - jeżeli wpiszecie go w koszyku, to wówczas otrzymacie 40% zniżki na bilet w wersji standard!

Więcej informacji na temat imprezy znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...