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