Crashing Program

So, I've made a program that shuffles a card deck, gives you thirteen random cards, and then sorts them. It does work, the problem is that whenever the program has done what is was supposed to do, it crashes. Can you help me in finding out why?

(The card names aren't in English, because I don't know what they're called in English, but I hope that does not bother you.)

Since I am a beginner, the code might not be very effective.

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

int main(){
    vector<string> kort = {
        "Hj\x84rter Ess", "Hj\x84rter 2", "Hj\x84rter 3", "Hj\x84rter 4", "Hj\x84rter 5", "Hj\x84rter 6", "Hj\x84rter 7", "Hj\x84rter 8", "Hj\x84rter 9", "Hj\x84rter 10", "Hj\x84rter Knekt", "Hj\x84rter Dam", "Hj\x84rter Kung",
        "Ruter Ess", "Ruter 2", "Ruter 3", "Ruter 4", "Ruter 5", "Ruter 6", "Ruter 7", "Ruter 8", "Ruter 9", "Ruter 10", "Ruter Knekt", "Ruter Dam", "Ruter Kung",
        "Kl\x94ver Ess", "Kl\x94ver 2", "Kl\x94ver 3", "Kl\x94ver 4", "Kl\x94ver 5", "Kl\x94ver 6", "Kl\x94ver 7", "Kl\x94ver 8", "Kl\x94ver 9", "Kl\x94ver 10", "Kl\x94ver Knekt", "Kl\x94ver Dam", "Kl\x94ver Kung",
        "Spader Ess", "Spader 2", "Spader 3", "Spader 4", "Spader 5", "Spader 6", "Spader 7", "Spader 8", "Spader 9", "Spader 10", "Spader Knekt", "Spader Dam", "Spader Kung"
    }, hand(13);
    srand(static_cast<unsigned int>(time(0)));
    int cards = 52, y;
    string pos;
    vector<int> taken(13, -1);
    for(int x=0;x<13;x++){
        int random = rand()%cards;
        hand.at(x) = kort.at(random);
        for(y=0;y<13;y++){
            if(random==taken[y]){
                x=0;
                for(int z=0;z<13;z++){
                    taken[z] = -1;
                }
                break;
            }
        }
        if(random==taken[y])
            continue;
        taken[x] = random;
    }
    for(int x=0;x<13;x++){
        for(int y=0;y<52;y++){
            if(hand[x]==kort[y]){
                taken[x] = y;
                break;
            }
        }
    }
    sort(taken.begin(),taken.end());
    cout << "Your cards:" << endl;
    for(int x=0;x<52;x++){
        cout << kort[taken[x]] << endl;
    }
    return(0);
}
It will crash on line 46 if taken[x] is -1.
Plus the size of taken is 13, but the loop x goes up to 52
Topic archived. No new replies allowed.