0

So, I'm making a structure that works with videos that is supposed to import them from a txt file and I get an error on line 42(uninitialized local variable filme use) and I don't know how to fix it. I can't change Main function.

Any help please ?

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

using namespace std;

struct Filme {
public:
    //string name;
    int anu;
    string denumire;
    //int ani;
    //vector<string>den;

    
};
void Import(int n, Filme* film,string den) {
    
    ifstream ifs(den);
    string denumire;
    int anu;
    while (!ifs.eof()) {
        ifs >> denumire >> anu;
        film->denumire = denumire;
        film->anu = anu;
    }
}
void Afisare(Filme *filme)
{
    cout << filme->denumire<< endl;
    cout << filme->anu << endl;
}
int main()
{
    Filme* filme;
    int n;
    cout << "Cate filme doriti sa importati din fisier: ";
    cin >> n;
    Import(n, filme, "filme.txt");
    //Sort(filme);
    Afisare(filme);

    return 0;
}


3 Answers3

1

you should not do this

film->denumire = denumire;

in

void Import(int n, Filme* film,string den) {

because film is a non initialized pointer you created in main...

Filme* filme;
....
Import(n, filme, "filme.txt");

you have to init it properly

Filme* filme = new Filme...
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

You just define a pointer. Where is its memory allocation?! ;)

Filme* filme = new Filme();

pay attention to delete the pointer at the end.

delete filme;
filme = nullptr; //for c++11
alireza
  • 58
  • 5
0

You may refers to the topic C++ how to pass an uninitialized pointer to a function

void Import(int n, Filme* &film, string den) {

    ifstream ifs(den);
    string denumire;
    int anu;
    while (!ifs.eof()) {
        ifs >> denumire >> anu;
        film = new Filme();
        film->denumire = denumire;
        film->anu = anu;
    }
}
pvc
  • 1,070
  • 1
  • 9
  • 14
YvonneY
  • 11
  • 1