#What should I modify?

1 messages · Page 1 of 1 (latest)

inner patrol
#

CLASS REPOSITORY

#ifndef REPO_H
#define REPO_H

#include "Entity.h"
#include <vector>
#include <algorithm>

class Repo
{
    protected:
        vector<Entity> entities;

    public:

Repo()
{
}

~Repo()
{
}

void addItem(Entity &e) {
    entities.push_back(e);
}


int getSize() {
    return entities.size();
}

void deletion(Entity &e)
{
    vector<Entity>::iterator it;
    it = std::find(entities.begin(), entities.end(), e);
    if (it!=entities.end())
    {
        entities.erase(it);
    }
}
};

#endif // REPO_H

CLASS REPOSITORYFILE

#ifndef REPOFILE_H
#define REPOFILE_H
#include "Repo.h"
#include <fstream>
#include <algorithm>

class RepoFile : public Repo
{
    private:
        const char *file_name;
        void saveToFile() {
    ofstream fo(file_name);
    for(Entity e: entities)
        fo << e.toString() << endl;
    fo.close();
}
    public:
        RepoFile(const char*);
        ~RepoFile();
        void addItem(Entity &);
        void deletion(Entity &);

};

RepoFile::RepoFile(const char *f_name) : Repo(){
    file_name = f_name;
    ifstream f(file_name);
    while(!f.eof()){
        int code;
        f>>code;
        Entity  e(code);
        entities.push_back(e);
    }
    f.close();
}


RepoFile::~RepoFile()
{
}

void RepoFile::addItem(Entity &e) {
    Repo::addItem(e);
    saveToFile();
}


void RepoFile::deletion(Entity &e)
{
    Repo::deletion(e);
    saveToFile();
}



#endif // REPOFILE_H

MAIN

#include <iostream>
#include "RepoFile.h"
using namespace std;
#include <assert.h>

int main()
{
    RepoFile rf("C:\\Users\\hp\\Desktop\txt.txt");
    assert(rf.getSize() == 3);
    Entity e(4);
    rf.addItem(e);
    assert(rf.getSize() == 4);
    rf.deletion(e);
    assert(rf.getSize() == 3);
    cout << "Tests have passed!" << endl;
    return 0;
}

OUTPUT: terminate called after throwing an instance of 'std::bad_alloc'

nova forgeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

sage lichen
#

Have you debugged?

inner patrol
round cipher
# inner patrol it fails

you are pushing an infinite number of elements to some vector, and you run out of memory, that is what the exception std::bad_alloc was thrown from

#
    while(!f.eof()){
        int code;
        f>>code;
        Entity  e(code);
        entities.push_back(e);
    }
    f.close();

here most likely

sage lichen
#

For future reference you can make the debugger break whenever an exception is triggered

#

And from there you can do a backtrace

inner patrol
sage lichen
#

Just make it while(f)

#

Should work, i havent looked at your code very closely

round cipher
#

I would use while(f.good())

inner patrol
# round cipher I would use `while(f.good())`

I've tried but it doesn't seem to work. The size of entities remains 0. I have 3 elements in the .txt file. I've wrote the code 3 times instead of the while and it does work. ++When using .eof() should the final of the text file be marked with a character or something like that?

inner patrol
sage lichen
inner patrol
sage lichen
#

Its probably because the file didnt open then

inner patrol
round cipher
#

you file was not opened successfully, doesn't matter what while you write

#

check the name

versed plume
#

@inner patrol i think you forgot an extra \ in the file name

#

instead of
C:\\Users\\hp\\Desktop\txt.txt
shouldnt it be
C:\\Users\\hp\\Desktop\\txt.txt

round cipher
#

^

sage lichen
inner patrol
versed plume
#

it does

round cipher
#

oh yes it does

#

\t is the tab character

versed plume
#

\\ is counted as a slash character, while "\t" is some special character

#

yea

#

try running it with the new file name

round cipher
#
C:\\Users\\hp\\Desktop  xt.txt

literally

versed plume
#

and also add a check to see if the file has been successfully opened

#

you can do it with a pretty basic is_open() check

round cipher
#

@inner patrol

inner patrol
round cipher
#

does that file exist on your Desktop?

#

can you hit right click -> Properties and show it?

inner patrol
round cipher
inner patrol
#

instead of 3

round cipher
#

I don't get it

versed plume
#

are you sure that the file is even open

#

there are no checks in your constructor or anything

inner patrol
round cipher
inner patrol
#

it adds*

inner patrol
inner patrol
versed plume
inner patrol
# round cipher <:sendcode:995401603165208577>
RepoFile::RepoFile(const char *f_name) : Repo(){
    file_name = f_name;
    ifstream f(file_name);
    while (!f.eof()){
        int code;
        f>>code;
        if(f.eof()) break;
        Entity e(code);
        entities.push_back(e);
        }
    f.close();
}
#

I added that break there

versed plume
#

oh

#

huh

round cipher
round cipher
nova forgeBOT
#

@inner patrol Has your question been resolved? If so, run !solved :)

inner patrol
#

!solved

nova forgeBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

round cipher