I'm trying to create a program that lets me save the user input and show it in a list. I don't want to write and read from a file, I only want it to save the input as long as the program is running.
This is what I've done, with some help from a manual:
You could store all the Film objects that you create in nyFilm() inside a container like std::vector. When you want to show them all you just have to iterate over the container and call skrivFilm() for each Film object.
You create Arkiv in main() so it will not be visible inside nyFilm(). One way to solve this is by passing Arkiv as a reference to nyFilm().
You have to decide what to store inside the vector. If you are going to store strings in the vector you will have to convert the Film to a string before adding it to the vector.
In nyFilm() you try to add a Film pointer to the vector. If that is what you want to do you should change the type of Arkiv to std::vector<Film*>. In that case you also don't want to delete the Film object at the end of nyFilm() because then the pointer in the vector will be useless, pointing to a deleted object.
Another possibility is to make Arkiv type std::vector<Film>. That is probably easier because you don't have to care about calling delete and such. In this case it's no point to create Film objects dynamically (with new) at all.
void nyFilm()
{
string angeTitel;
string angeMedia;
vector<Film*> Arkiv; // <- Pointer to my vector
// Skapa ett nytt objekt av klassen film
Film* f = new Film;
// Spara filmens titel i variabeln Titel
cout << "Ange filmens titel: ";
getline(cin, angeTitel);
f->Titel = angeTitel;
// Spara filmens media i variabeln Meida
cout << "Ange filmens media: ";
getline(cin, angeMedia);
f->Media = angeMedia;
Arkiv.push_back( f ); // <- Is this adding Media and Titel to the vector?
skrivFilm(f);
delete f; // <- Am I deleting the object? =)
}
vector<Film*> Arkiv; // <- Pointer to my vector No, that a vectors of pointers. delete f; // <- Am I deleting the object? =) Yes, delete will delete the object pointed. Arkiv.push_back( f );// <- Is this adding Media and Titel to the vector? Yes, it is. But to a local vector, that will perish when that function returns.
It's not a pointer to a vector. It's a vector of pointers. Problem is that you define the vector inside nyFilm(). There is no way to access the vector from other functions and the vector will be destroyed at the end of nyFilm() anyway. I think it is much better to define the vector in huvudMeny() or in main() as you did before. You then have to pass the vector as argument to the functions that needs it.
Is this adding Media and Titel to the vector?
It's adding a pointer to the vector.
Am I deleting the object? =)
Yes you are deleting the object but you shouldn't do that here. Wait until you no longer need the object before deleting it.