Vector problem: Program crash

I'm new to using vectors, and in all descriptions I read, there is a lot of mind-blogging information about pointers. I do really not get it, doesn't matter how many times I read about it, I can not get my head around it.

What is pointers and how do I use them, in plain english?

Now to the problem with my program. I created a vector to store movie titels and media, and then later on I want to be able to search, add/remove and list the titels. Best way to do this is by using a vector?

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

int main()
{
    // Vilken datatyp ska vår vector ha? Och vad ska den heta?
    // Datatyp vektornamn;
    vector<string> Titel;
    string fnamn;

    vector<string> Typ;
    string ftyp;

    // Be användaren mata in namnet på filmtiteln
    cout << "Ange filmtitel: ";
    getline(cin, fnamn);

    // Lägg till namnet på filmtiteln längst bak i listan över filmer
    Titel.push_back(fnamn);

    cout << "Ange filmtyp: ";
    getline(cin, ftyp);

    // Skriv ut första posten i vektorn Titel
    // vektornamn.front()
    cout << "\nTitel    : " << Titel.front();
    cout << "\nAv typen : " << Typ.front();
}


When I compile, no problem. When I run the program, it gets stuck at Av typen: and windows tells me the program has stoped running.

Sorry for the bad english and thanks in advance for the help.
I would assume it has to do with the fact that there is nothing being held in the vector 'Typ'. You are basically trying to access something that isn't there.

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

int main()
{
    // Vilken datatyp ska vår vector ha? Och vad ska den heta?
    // Datatyp vektornamn;
    vector<string> Titel;
    string fnamn;

    vector<string> Typ;
    string ftyp;

    // Be användaren mata in namnet på filmtiteln
    cout << "Ange filmtitel: ";
    getline(cin, fnamn);

    // Lägg till namnet på filmtiteln längst bak i listan över filmer
    Titel.push_back(fnamn);

    cout << "Ange filmtyp: ";
    getline(cin, ftyp);

    Typ.push_back(ftyp);

    // Skriv ut första posten i vektorn Titel
    // vektornamn.front()
    cout << "\nTitel    : " << Titel.front();
    cout << "\nAv typen : " << Typ.front();
}
Last edited on
Thank you xhtmlx!
I did forget to put Typ.push_back(ftyp); in my code. Horrible misstake, I'm ashamed.
By the way you could have one vector for names and types

std::vector<std::pair<std::string, std::string>> movies;
Topic archived. No new replies allowed.