How to save and show during runtime?

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <limits>   //För att använda numeric_limits
#include <windows.h>//För att använda system "cls" och sleep
#include <vector>
using namespace std;

class Film
{
    public:
    string Titel;
    string Media;
};

void huvudMeny();
void nyFilm();
void skrivFilm(const Film* f);

int main()
{
    setlocale(LC_ALL, "swedish");

    vector<string> Arkiv;            // <- UPDATED LINE

    huvudMeny();
}

void huvudMeny()
{
    char menyVal;

    do
    {

    cout << "    ** ARKIV **    " << endl;
    cout << "*******************" << endl;
    cout << "1: Ny film"          << endl;
    cout << "2: Radera film"      << endl;
    cout << "3: Sök efter film"   << endl;
    cout << "4: Visa alla"        << endl;
    cout << "0: Avsluta"          << endl;
    cout << "___________________" << endl;
    cout << "Ange val: ";




        cin >> menyVal;
        cin.ignore( numeric_limits <streamsize>::max(), '\n' );

        switch(menyVal)
        {
            case '1':   nyFilm();
                        Sleep(1000);
                        system("cls");
                        break;
            case '2':   cout << "Radera film." << endl;
                        Sleep(1000);
                        system("cls");
                        break;
            case '3':   cout << "Sök efter film." << endl;
                        Sleep(1000);
                        system("cls");
                        break;
            case '4':
                        break;
            case '0':   cout << "Programmet kommer nu att avslutas." << endl;
                        Sleep(1000);
                        system("cls");
                        break;
            default :   cout << "Felaktigt val, försök igen." << endl;
                        Sleep(1000);
                        system("cls");
        }
    }
    while( menyVal != '0');
}

void nyFilm()
{
    string angeTitel;
    string angeMedia;

    // 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 );            // <- UPDATED LINE

    skrivFilm(f);
    delete f;
}

void skrivFilm(const Film* f)
{
    cout << "Filmens titel: ";
    cout << f->Titel << endl;

    cout << "Filmens media: ";
    cout << f->Media << endl;
    cout << "\n*************************" << endl;
}


I want the case 1 to create a new object of the class film. And I want the case 4 to show all the objects of that class allready created.

Edit:
After the suggestion by Peter87 I've created a vector, but get error message:
Line 96|error: 'Arkiv' was not declared in this scope|
Last edited on
Hej!

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.
I've updated the code but get an error....
Please... Can someone help me, I need to get this done by tonight...
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.
Last edited on
Is this the correct way of doing it? Or am I deleting my objects? I don't want that... =)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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? =)
}


Is this to badly explained or is it to much? Please, I can break it down a bit if it make it easier to answer... I really need help with this.
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.

You have a really big issue with scope.
Pointer to my vector

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.
Last edited on
Topic archived. No new replies allowed.