Segmentation fault (Core dumped) error

Hello,

I'm getting the segmentation fault (core dumped) error. All I know is that it was caused when I added this. Any feedback is appreciated. Thanks!


1
2
                else if (lootRandom==3) {
                    std::cout << "You found a " << lootList_Zombie[lootRandom] << "!" << std::endl;


Here is a bigger chunk if you need it.
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
        std::cout << "*What do you want to do now?*" << std::endl;
        std::cout << "\t >> Enter '1' to loot the body" << std::endl;
        std::cout << "\t >> Enter '2' to hide and find some cover" << std::endl;

        int choiceOne_Stay_LootOne=0;
        std::cin >> choiceOne_Stay_LootOne;

            if (choiceOne_Stay_LootOne==1) {
                system("clear");
                std::cout << "#You search the corpse" << std::endl;
                std::cout << std::endl;
                // ITEMS IN ZOMBIE'S BODY
                std::string lootList_Zombie[] = {"First Aid", "golf club", "pistol"};
                srand(time(0));
                int lootRandom = rand()%(3-0)+1;

                if (lootRandom==1) {
                    std::cout << "You found a " << lootList_Zombie[lootRandom] << "!" << std::endl;
                }

                else if (lootRandom==2) {
                    std::cout << "You found a " << lootList_Zombie[lootRandom] << "!" << std::endl;

                }

                else if (lootRandom==3) {
                    std::cout << "You found a " << lootList_Zombie[lootRandom] << "!" << std::endl;

                }


            }

1
2
else if (lootRandom==3)
 { std::cout << "You found a " << lootList_Zombie[lootRandom] ...


You are trying to access lootList_Zombie[3] That does not exist. The array contains elements 0, 1 and 2 only.
Yeah I just realized that. Forgot that it starts from 0. Thanks!
Topic archived. No new replies allowed.