Add to vector

The last post I made was awful, I'm sorry. But I will not make the same mistake twise... I hope =)

Now this is what I got:
A class named Film.
A vector with the data-type Film (I hope) called Arkiv
A function allowing the user to create a new object from the class Film,

at least this is what I think I have.
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
#include <iostream>
#include <vector>
using namespace std;

class Film
{
    public:
        string Titel;
        string Media;

        void skrivFilm()     // This function prints the Film
        {
            cout << "Titel: " << Titel << endl;
            cout << "Media: " << Media << endl;
        }
};

int main()
{
    // This is a vector of the data-type Film, named Arkiv
    // or this is what I think this is
    vector<Film> Arkiv;     

    Film nyFilm;     // I create a new object and let the user define it below

    cout << "Ange filmens Titel: ";
    getline(cin, nyFilm.Titel);
    cout << endl;

    cout << "Ange filmens Media: ";
    getline(cin, nyFilm.Media);
    cout << endl;

    nyFilm.skrivFilm();     // I print the new object with this function.

return 0;
}


This works just as it's supposed to.

Now I want to add the new Film object in my vector Arkiv. But I don't know how?

Thanks in advance!
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
#include <iostream>
#include <vector>
using namespace std;

class Film
{
    public:
        string Titel;
        string Media;

        void skrivFilm()     // This function prints the Film
        {
            cout << "Titel: " << Titel << endl;
            cout << "Media: " << Media << endl;
        }
};

int main()
{
    // This is a vector of the data-type Film, named Arkiv
    // or this is what I think this is
    vector<Film> Arkiv;     

    Film nyFilm;     // I create a new object and let the user define it below

    cout << "Ange filmens Titel: ";
    getline(cin, nyFilm.Titel);
    cout << endl;

    cout << "Ange filmens Media: ";
    getline(cin, nyFilm.Media);
    cout << endl;
     
    Arkiv.push_back(nyFilm);
    nyFilm.skrivFilm();     // I print the new object with this function.

return 0;
}

Just use the member function push_back() to 'push' the object into the vector.
Last edited on
Arkiv.push_back( myFilm );
It works, now I can add objects to my vector... but now I want to show all objects 'pushed' into the vector.

This is my class and main function:
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
#include <iostream>
#include <vector>
#include <windows.h>//För att använda system "cls" och sleep
#include <limits>   //För att använda numeric_limits
using namespace std;

class Film
{
    public:

        // Deklarerar variablerna för de data som behövs
        string Titel;
        string Media;


        // Denna funktion skriver ut filmen på skärmen
        void skrivFilm()
        {
            cout << endl;
            cout << "Titel: " << Titel << endl; // Skriver ut filmens titel
            cout << "Media: " << Media << endl; // Skriver ut filmens media
        }
};

int main()
{
    setlocale(LC_ALL, "swedish");   // Denna funktion låter mig använda svenska

    vector<Film> Arkiv;             // Skapa en vector av typen Film

    char menyVal;                   // Denna variabel håller reda på vilket val användaren anger i huvudmenyn
    Film nyFilm;                    // För att skapa en film behöver vi ett objekt att spara den i

    do
    {

    system("cls"); // Se till att rensa skärmen

    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':
                {
                    system("cls");
                    cout << "\n\n1: Ny film";
                    Sleep(1000);
                    cout << endl << endl;
                    // Vi ger objektets titel-variabel ett värde
                    cout << "Ange filmens Titel: ";
                    getline(cin, nyFilm.Titel);
                    cout << endl;

                    // Vi ger objektets media-variabel ett värde
                    cout << "Ange filmens Media: ";
                    getline(cin, nyFilm.Media);
                    cout << endl;

                    // Vi lägger vårt filmobjekt i vektorn
                    Arkiv.push_back( nyFilm );

                    break; // Undvik fall-through
                }
            case '2':
                {
                    break;
                }
            case '3':
                {
                                     //    <---- I WANT A SEARCHFUNCTION HERE
                    break;        //
                }
            case '4':
                {
                    system("cls");

                    for(int i=0; i<Arkiv.size(); i++)   // Räkna upp alla filmer i Arkiv
                    {
                        Arkiv[i].skrivFilm();           // Skriv ut dem med hjälp av funktionen skrivFilm();
                    }

                    cout << "\n\nTryck på ENTER för Huvudmeny.";
                    cin.ignore();
                    break;
                }
            case '0':
                {
                    break;
                }
            default :   cout << "Felaktigt val, försök igen." << endl;
                        Sleep(1000);
                        system("cls");
        }
    }
    while( menyVal != '0');

}


Can I print all the objects in the vector using the function void skrivFilm() declared in the class Film?

Thanks for all the help, you are great!!

EDIT:

I added the new functions suggested in this topic
Last edited on
I was just about to tell you how to do that. :)

In this case, you should use an iterator to call the member function 'skrivFilm()' for each element of the vector. You would use a const_iterator if your member function 'skrivFilm()' was a constant function (which it should be).
1
2
3
    vector<Film>::iterator iter;
    for(iter = Arkiv.begin(); iter != Arkiv.end(); ++iter)
         iter->skrivFilm(); // I print the new object with this function. 

This is optional, you could use array subscripts to access the elements of a vector.
1
2
    for(int i = 0; i < Arkiv.size(); ++i)
         cout << Arkiv[i].skrivFilm(); // I print the new object with this function. 

Why use iterators when you could just use array subscripts?
http://www.fredosaurus.com/notes-cpp/stl-containers/vector/iter-vector.html
Last edited on
If your compiler supports the range-based for statement then you can write

for ( auto x : Arkiv ) x.skrivFilm();
It works! It works!!! It's wounderful! I love it! Thank you all!!! =)

I will be back ;)


Now the hard work begins, making it look good =)
Removed by user
Last edited on
Topic archived. No new replies allowed.