Poke game seg fault

Hey guys, hopw everyone is having a good night. I'm working on a class project and im having a bit of trouble with my code. Anyone see it?
Getting a segmentation fault.
Thanks a bunch

#include<iostream>
#include<cstring>
#include<string.h>
#include<stdlib.h>
#include <algorithm>
#include <vector>
#include<ctime>
using namespace std;
struct Monster
{
string Name;
int CombatPower;
};
struct Monster* setMonster()
{
struct Monster *monester;
string randomString;
int random_Number;
string names[25]={
"Charmander", "Bulbasaur", "Squrtile", "Pidgey", "Pikachu", "Sandshrew", "Zubat",
"Mankey", "Abra","Magikarp", "Eevee", "Rattata", "Vulpix", "Scyther", "Jigglypuff",
"Geodude", "Onix", "Staryu","Snorlax", "Mewtwo", "Oddish", "Caterpie","Spearow" , "Charizard", "Zapdos"};
srand(time(NULL));
randomString=names[rand()%25];
random_Number=(rand()%25)+1;

monester->Name=randomString;
monester->CombatPower=random_Number;
return monester;
}
int main()
{
struct Monster *monster;
string input;
bool didCatch=false;
int pokeballs=5;
for(int i=0;i<pokeballs;i++)
{
monster=setMonster();
cout<<"\nPlease input pokeman"<<endl;
cin>>input;
if(input==monster->Name)
{
cout<<"\nGotcha! You caught the monster name!"<<endl;
break;
}
else
{
cout<<"\nYou have remaining "<<pokeballs-1<<" chances to catch"<<endl;
cout<<"\nTryagain to catch"<<endl;
}

}
return 0;
}
You forgot to instantiate your Monster object, and therefore your 'Monster' pointer points to a haphazardly memory address.
Last edited on
> Getting a segmentation fault.

Avoid using pointers of any kind unless you have no other good alternative.

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

struct Monster
{
    std::string Name ;
    int CombatPower = 0 ; // this member is set, but not used
};

Monster makeMonster() // return a value of type Monster
{
    static const std::size_t N = 25 ;
    static const std::string names[N] = {
        "Charmander", "Bulbasaur", "Squrtile", "Pidgey", "Pikachu", "Sandshrew", "Zubat",
        "Mankey", "Abra","Magikarp", "Eevee", "Rattata", "Vulpix", "Scyther", "Jigglypuff",
        "Geodude", "Onix", "Staryu","Snorlax", "Mewtwo", "Oddish", "Caterpie","Spearow" ,
        "Charizard", "Zapdos"
    };

    return Monster{ names[ std::rand()%N ], int( std::rand()%N + 1 ) } ;
}

int main()
{
    std::srand( std::time(nullptr) ) ; // call once at the start of main

    const Monster m = makeMonster() ; // we do not need a pointer for this

    int pokeballs = 5;
    while( pokeballs > 0 )
    {
        std::string input ;
        std::cout << "Please input pokeman: " ;
        std::cin >> input;

        if( input == m.Name )
        {
            std::cout<<"\nGotcha! You caught the monster name!\n" ;
            return 0 ;
        }

        if( --pokeballs > 0 )
        {
          std::cout << "\nYou have remaining " << pokeballs <<" chances to catch\n"
                    << "Try again\n" ;
        }
    }
}
Thanks for the help guys.
@ JL i tried your code out and now im getting a different fault, No matching function to call. Any ideas why?
Please post the code that you tried and the full error diagnostic: line number etc.
Ah got it to work, okay, so lets say i wanted to take a game like this to the next step.
Say that I want to assign the monster a random power level, and doing so makes the pokemon harder to catch? I started off with this code to get a basic outline, and now I need to create a playable game in which the pokes have a power, and based on that power have a one in two or a one in four chance of being caught. With still just 5 poke balls
@JL
So for instance the psuedo would look like this:
A poke appeard

Poke
210 hp

you have 5 poke balls
catch? y/n

and loop until you catch it or have no balls left
Something like this, perhaps:

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

struct Monster
{
    std::string Name ;
    int CombatPower = 0 ;

    static const int NUM_MONSTERS = 25 ;
};

Monster makeMonster()
{
    static const std::string names[ Monster::NUM_MONSTERS ] = {
        "Charmander", "Bulbasaur", "Squrtile", "Pidgey", "Pikachu", "Sandshrew", "Zubat",
        "Mankey", "Abra","Magikarp", "Eevee", "Rattata", "Vulpix", "Scyther", "Jigglypuff",
        "Geodude", "Onix", "Staryu","Snorlax", "Mewtwo", "Oddish", "Caterpie","Spearow" ,
        "Charizard", "Zapdos"
    };

    return Monster{ names[ std::rand()%Monster::NUM_MONSTERS ],
                    int( std::rand()%Monster::NUM_MONSTERS + 1 ) } ;
}

bool caught( const Monster& m )
{
    // r == 0, 1, 2 or 3 depending on the Combat Power
    int r = 0 ;
    if( m.CombatPower > 20 ) r = 3 ;
    else if( m.CombatPower > 12 ) r = 2 ;
    else if( m.CombatPower > 6 ) r = 1 ;

    return ( std::rand() % 4 ) > r ;
}

int main()
{
    std::srand( std::time(nullptr) ) ; // call once at the start of main

    const Monster m = makeMonster() ;
    std::cout << "A monster appeared. name: " << m.Name << " power: " << m.CombatPower << '\n' ;

    int pokeballs = 5;
    while( pokeballs > 0 )
    {
        std::cout << "Press enter to try to catch monster: " ;
        std::cin.get() ;

        if( caught(m) )
        {
            std::cout<<"\nGotcha! You caught the monster!\n" ;
            return 0 ;
        }

        if( --pokeballs > 0 )
        {
          std::cout << "\nYou have remaining " << pokeballs <<" chances to catch. "
                    << "Try again.\n" ;
        }
    }
}
Yes exactly, so far I have this, as this is for a project im trying to keep it to the requirements.
I cant seem to call the same monster and I cant figure it how i should do it, any suggestions?
so far :
#include <iostream>

using namespace std;

struct Monster

{

string name;

int combatPower;

};

bool captureAttempt(Monster monster)

{

int chance = 0;

if(monster.combatPower < 100)

chance = rand()%2;

else if(monster.combatPower > 99)

chance = rand()%4;


if(chance == 0)

return true;

else return false;

}

string randomNameGenerator()

{

string names[25] = {"Charmander", "Bulbasor", "Squrtile", "Pidgey", "Pikachu",

"Sandshrew", "Zubat", "Mankey", "Abra", "Magikarp",

"Eevee", "Rattata", "Vulpix", "Scyther", "Jigglypuff",

"Geodude", "Onix", "Staryu", "Snorlax", "Mewtwo",

"Oddish", "Caterpie", "Spearow", "Charizard", "Zapdos",};

return names[rand()%24];

}

Monster setMonster(Monster monster)

{

monster.name = randomNameGenerator();

monster.combatPower = rand()%500 + 1;

return monster;

}

int main() {

srand(time(NULL));

Monster monster;

char input;

bool didCatch = false;

int pokeballs = 5;
while(true)

{

pokeballs;

monster = setMonster(monster);

didCatch = captureAttempt(monster);
cout << " A wild " << monster.name << "appeared" << endl;
cout << monster.combatPower << endl;
cout << "would you like to catch it (Y/N) " << endl;
cin >> input;

if(didCatch)

{

cout<<" You caught it!" << endl;
exit(0);

}

else

cout<< " You have " << pokeballs -1 << "left. try to catch again?" << endl;

cin>>input;

if(input == 'N' || input == 'n')

{

cout<<"\nGot away safely."<<endl;

exit(0);

}

if(pokeballs == 0)

{

cout<<"\nYou do not have any pokeballs.";

exit(0);

}

if(input == 'Y' || input == 'y')
{
monster;
}

else

{

cout<<"\nRemianing pokeballs: "<<pokeballs<<endl;

}

}

return 0;

}
my only problem now is when i fail to catch a monster the first time and attempt the seccond time its calling a new monster to be caught when I need to attempt the same monster. If that makes sense
Do this monster = setMonster(monster); just once, outside the loop.
got it. thanks guys!
Topic archived. No new replies allowed.