Death before execution

I am trying to write a pokemon game but I can't do anything right now with it. Every time I try to execute my program it stops and tells me that the program has stopped running. I think my array is too big but my teacher says that that's not possible.

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
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;



int getnum(string pokeName[385], string playerPoke)
{
    int n = 0;
    while (playerPoke != pokeName [n])
    {
          n++;
          }
    return n;
}

int main()
{
	//declare variables
	string pokeName [385];
	string pokePic [385];
	string pokeType [385];
	string pokeType2 [385];
	int health [385];
	int attack [385];
	int defense [385];
	int spAttack [385];
	int spDefense [385];
	int speed [385];
	string moveName [348];
	string moveType [348];
	int movePower [348];
	int moveAccuracy [348];
	int movePP [348];
	string moveEffect [348];
	string moveEffect2 [348];
	int moveEffectPercent [348];
	int moveEffect2Percent [348];
	string p1names [5];
	string p1attack1 [5];
	string p1attack2 [5];
	string p1attack3 [5];
	string p1attack4 [5];
	string p2names [5];
	string p2attack1 [5];
	string p2attack2 [5];
	string p2attack3 [5];
	string p2attack4 [5];
	string playerPoke;
	int pokeNum;
	int currentChoice = 0;
	
	//pokemon names
    pokeName [0] = "Bulbasaur";

    pokeName [385] = "Deoxys";
    
    //test array
    cout << "Enter Pokemon name";
    getline (cin , playerPoke);
    pokeNum = getnum (pokeName, playerPoke);
    p1names [currentChoice] = pokeName [pokeNum];
    cout << p1names[currentChoice];
    
    return 0;
}


I deleted a lot of code to save space. When I edit out the pokemon in the array, I don't have any problems, but when I put it back in, it won't run. What should I do.
it's just a guess but I think your stack probably ran out of memory to allocate which makes the program crash? That's where local variables are stored if I remember correctly, and with all those arrays consuming memory I think that is a very likely source of the problem. It's just a hunch though.

In analogy it's probably just like a recursive function that doesn't end and it fills the stack until it runs out of space and makes the program crash, if that makes sense.

Edit:
Or line 59 maybe causing the error.

pokeName [385] = "Deoxys";

you're trying to access the index 385 which is invalid, the valid indices would be from 0 - 384. That would also lead to a program crash.
Last edited on
pokeName [385] = "Deoxys";

This is where it crashes. You allocate an array that has a size of 385, the first being 0, the last being 384.

pokeNum = getnum (pokeName, playerPoke);

After the first crash is repaired, you will get to here. When I comment this line out and initialize pokeNum to 0, it works fine.
while (playerPoke != pokeName [n]) there is no upper bound for n
I'm an idiot. I remembered that if you declared 5 in an array it would start at zero but I forgot that it would also end at four. I haven't checked the function yet but I'll make sure that it will stop repeating at the end.
Topic archived. No new replies allowed.