Class, vectors and pointers...?

I'm really trying to get all the pieces together, but this really confuses me.

What I want to accomplish:
I want a class to build objects and then store them in a vector. Then I'm going to create a meny to be able to add the objects from the consol, change the objects, search and list my created objects.

I guess it's not to complicated, but I really don't get it. I know how to create a class and use it to build an object, but only in the sourcecode. I don't know how to build new objects from user input in the consol.

Vectors, pointers and iterators are really something I do not get. I've read all about it, but still don't know how to use it.

I'm not expecting anyone to create this for me, but I would really appreciate if someone could explain to me how to make it myself.

I'm at this stage in my knowlage:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

class Example
{
    public:
            //This is where I'm supposed to create a blueprint for my objects
            //of the class Example.

    // Is this where the user input is supposed to take place?
}

int main()
{
    // Where is the vector supposed to be declared?

    do
    {
        // My meny

    }
    while();
}


I know how to create the meny and make it work with a switch depending on the users input. And the do-while loop is just an example, I know how to work it.

The meny choices calls functions, and one of this functions is supposed to create an object of the class Example and store it in the vector. I do not know how to create this function.

An other meny choice is supposed to let the user change data in a stored object. I have no idea of how to make this happen.

And least I want a search function to search for objects based on user input. And offcourse a list option to list all the objects in the vector.

Please don't leave me half-baked. I really want to learn how to do this.

This must be the longest post I've ever posted on this forum. Any help in any way is appreciated.

Thanks
Last edited on
I'm not sure where the problem is. If you know how to create an object, just do that and then push_back it into the vector. Which part are you having trouble with?

Where is the vector supposed to be declared?

Where it needs to be. You're aware of the scoping rules, right?

Is this where the user input is supposed to take place?

No, that belongs into main or a separate function (it can make sense to provide operator>> in some cases, though).
Last edited on
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
#include <iostream>
#include <vector>
using namespace std;

class Example
{
    public:
            //This is where I'm supposed to create a blueprint for my objects
            //of the class Example.

    // Is this where the user input is supposed to take place?
}

int main()
{
    // Where is the vector supposed to be declared?

    std::vector<Example> v;
    do
    {
         Example ex;
         // enter values for members of the object
         // for example
         int value;
         std::cin >> value;
         ex.setValue( value );
         v.push_back( ex );

    }
    while();
}
Last edited on
@vlad from moscow
I'm trying to add what you did on my own code, but I don't think I got it right...

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
#include <iostream>
#include <vector>
#include <windows.h>
using namespace std;

class Film
{
    public:

};

int Meny( char choice );
void nyFilm();
void raderaFilm();
void sokFilm();

int main()
{
    vector< Film > Titel;
    vector< Film > Typ;

    do
    {
        ///////////////////////////////
        //         HUVUDMENY         //

        cout << "\t[N]y             : 1\n"  ;
        cout << "\t[R]adera         : 2\n"  ;
        cout << "\t[S]ök            : 3\n"  ;
        cout << "\tOspecifierad     : 4\n"  ;
        cout << "\tOspecifierad     : 5\n\n";
        cout << "\tAvsluta          : 0\n"  ;
        cout << "\t__________________\n\n"  ;
        cout << "\tAnge ditt val: "         ;

        //                           //
        ///////////////////////////////

        Meny ( cin.get() );             // Skicka användarens inmatning till funktionen Meny.


    }
    while( choice !=0 );
}

int Meny( char choice )
{
    // Ta bort tidigare utskrifter till skärmen.
    system( "cls" );

    // Testa värdet på variabeln 'choice'.
    switch( choice )
        {
            case 1      :
            case 'n'    :
            case 'N'    :   nyFilm();
                            break;
            case 2      :
            case 'r'    :
            case 'R'    :   raderaFilm();
                            break;
            case 3      :
            case 's'    :
            case 'S'    :   sokFilm();
                            break;
        }
}

void nyFilm()
{
    Film namn;

    string angTitel;
    cin >> angTitel;
    namn.setValue( angTitel );
    Titel.push_back( namn );

}

void raderaFilm()
{

}

void sokFilm()
{

}


It's long from finished yet, but if I do not get this to work from the start there is no point in keep building it.

Anyhow... I get error on line 43, 75 and 76.

Line 43: error: 'choice' was not declared in this scope
Line 75: error: 'class Film' has no member named 'setValue'
Line 76: error: 'Titel' was not declared in this scope


Thanks for the help!
Last edited on
Topic archived. No new replies allowed.