Pokemon Game - functions help needed

Edit: I realized i'm needing help on more than just one function. I think I may have fixed my original issue with the help given.
My next problem is now working on the main()function. My formatting i think is off and I'm getting confused with how to appropriate call some of the functions.

Here is the instruction for main():
main()

Variables:
monster of type Monster
input of type char
didCatch of type bool initialized and assigned to false
pokeballs of type int initialized and assigned to 5

This is your required starting function. Inside main, you will create an instance of Monster called “monster”. Do not initialize the monster variable. On the next line, call setMonster() and assign it to monster. This function will go and populate name and combat power into the instance of monster. Main should contain a loop that checks to see if we did not catch a pokemon and we have at least 1 pokeball available. Inside the loop, cout to the user how many pokeballs they have remaining and ask if the user wants to attempt to capture with possible options . Be sure to take one pokeball away for each attempt to capture the pokemon. Call and use the captureAttempt() function and assign its return to didCatch. Check didCatch to see if the user caught the pokemon and if the user did cout "Gotcha! You caught the monster name!". else, the user did not catch the pokemon and you should cout " broke free! Attempt to catch again?”. If the user did not choose to attempt to catch the pokemon, cout “Got away safely” and quit the program.  If the user does not have enough pokeballs, cout "You do not have any pokeballs, so you ran and got away safely." And end the program

Any help/tips/suggestions with this will be greatly appreciated. I feel like i'm getting close but still feel very far from the actual completion of this project.
Below is my code:
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <string>
#include <iostream>
#include <ctime>

using namespace std;
bool captureAttempt(Monster);
string setMonster(Monster[]);

struct Monster
{
	string Name [25];
	int CombatPower;

};

int main()
{
	Monster monster;
	char input;
	bool didCatch = false;
	int pokeballs = 5;

	cout << "\t\t\tWelcome to Pokemon CatchEm" << endl << endl << endl;

	// create instance of Monster called "monster": how?

	String setMonster(Monster monster); // call setMonster() and assign it to monster.am i doing this wrong?


	cout << " A wild " << monster << " appeared!" << endl << endl;
	cout << monster.Name;
	cout << monster.CombatPower;

	for (int i = 0; i < 5; i--)
	{
		i = pokeballs;

		{
			cout << "You have " << pokeballs << "left";
			cout << "Attempt to capture?  <y/n>";
			cin >> input;
		}

		if (input == 'y' || 'Y')
		{
			pokeballs--;
			return pokeballs;
		}

		else
		{
			cout << "Got away safely" << endl;
		}
	}

	bool	captureAttempt(didCatch);
	{
	if (didCatch == false)
	{
		cout << "Gotcha! You caught the " << monster << " !" << endl;
	}
	else
	{

		cout << monster << "broke free! Attempt to catch again? <y/n>" << endl;
		cin >> input;
	}

	return didCatch;
	}

	return 0;
}

bool captureAttempt(Monster)
{
	
	int chance, CombatPower;

	if (CombatPower > 200)
	{
		chance = rand() % 8;
	}

	else if (CombatPower > 99)
	{
		chance = rand() % 4;
	}

	else if (CombatPower < 100)
	{
		chance = rand() % 2;
	}

	if (chance == 0)
	{
		chance = true;
	}
		
		return chance;
}

string setMonster(Monster[])
{
	srand(time(NULL));
	Monster 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";
	}

	Monster randomNameGenerator(Monster x);
	{
		Monster x;
		{
			names[i] = randomNameGenerator(names[i]);
			i = x;
		}
		return x;
	}
}
Last edited on
¿why do you have a loop?

1
2
3
4
if(combatPower>200)
   dice = rand() % 8;
//...
return dice == 0;

ne555

I wasn't sure how to start this and thought I may need a loop. I have made the following changes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool captureAttempt(Monster)
{
	int chance, combatPower;

	if (combatPower > 200)
	{
		chance = rand() % 8;
	}

	else if (combatPower > 99)
	{
		chance = rand() % 4;
	}

	else if (combatPower < 100)
	{
		chance = rand() % 2;
	}

	return chance == 0;
}


Thank you for your help (and i'm sorry if I'm not getting it, beginner here) but, this is a boolean function. Shouldn't it return a true or false? would the code below be how I should have the return statement?
1
2
3
4
5
6
if (chance == 0)
	{
		chance = true;
	}
		
		return chance;

I edited the rand() function so now the random numbers start at 1 instead of 0. I think this makes more sense.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool captureAttempt(Monster)
{
	int chance, combatPower;

	if (combatPower > 200)
	{
		chance = rand() % 8 + 1;
	}

	else if (combatPower > 99)
	{
		chance = rand() % 4 + 1;
	}

	else if (combatPower < 100)
	{
		chance = rand() % 2 + 1;
	}

	return chance == 0;
}
rhphares thank you but I do need the rand() to return 0 to return a true statement
I think you could change the return statement to
return chance == 1 or one of the other numbers. But you should do what you think is best.
thank you again, I do need some help or assistance with the rest of my code. Would you like to help me with it? I keep going back and making changes that I think are needed but I feel like i'm making a bigger mess than actual progress.

I posted my entire code in the main topic.
Sure.
// create instance of Monster called "monster": how?
is
Monster monster
http://www.cplusplus.com/doc/tutorial/structures/
I think are objects and instances are used interchangeably.
Then you can use the monster members such as monster.Name[0-24] and monster.CombatPower


Last edited on
I'm really struggling with the setMonster() function, I'm getting super mixed and messed up.

Instruction:
setMonster()
This function takes the Monster variable passed and will assign the monster’s name to the name property in the Monster struct. This is done by calling randomNameGenerator() and assigning the function’s string return to the monster’s name. The monster’s combatPower is assigned as a random number between 1 – 450. When these two properties are assigned, return the entire instance of monster with a return statement.

randomNameGenerator()
Variables:
An array of names of type string initialized to a constant 25 elements and the following string list (feel free to copy and paste this list below into your code):

{
"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"
};
This function returns a string. It starts by seeding the random number generator with “srand(time(NULL)) and declare an array of type string called “names” set  to 25 elements initialized with the above list. It will then randomly return an element containing a string, so that each time this function is called a random pokemon name appears. Please return a random number using “rand()” from 0 – 24 inside the element to return, which is a string. This function should be called from “setMonster()” to set the monster’s name.

Here's what I got so far:
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

string setMonster(Monster[])
{
	srand(time(NULL));
	Monster 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";
	}


	string Monster randomNameGenerator(Monster x);
	{
		Monster x;
		{
			randomNameGenerator(names[25]);
			names[x]  = rand() % 25;
		}
		return x;
	}

	Monster int CombatPower;
	//CombatPower needs to be a rand() between 1-450 but how do I build the return statement to combine CombatPower and randomNameGenerator?
}
http://www.cplusplus.com/reference/cstdlib/rand/
I'm also a beginner. I'm not exactly sure what you are wanting. The setMonster function is still imcomplete but the rand() function works.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string setMonster(Monster x)
{
        int randomNumber = 0;
	srand(time(NULL));
	x.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";
	}


	randomNumber = rand() % 25;
        //x.Name[randomNumber];
        int CombatPower = rand() %450 + 1;
	//CombatPower needs to be a rand() between 1-450 but how do I build the return statement to combine CombatPower and randomNameGenerator?

        return x.Name[randomNumber] //not sure what to return??
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string setMonster(Monster x)
{
	int randomNumber = 0;
	srand(time(NULL));
	x.Name[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";
	randomNumber = rand() % 25;
	cin >>	x.Name [randomNumber];

	int CombatPower;
	for (int i=0; i <450; i++)
	{
	CombatPower = srand() % 450 + 1;
	cin >> x.CombatPower;
	}
		return x.Name[randomNumber];
}


srand() is lighting up with 2 errors - after CombatPower = srand() &450 +1;

Error: "Expression must have integral or unscoped enum type"
Error: "too few arguments in function call"
What does the whole code look like?
srand() should be rand() in this case (line 16).
Last edited on
this is where I'm at now:

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <string>
#include <iostream>
#include <ctime>
using namespace std;

struct Monster
{
	string Name[25];
	int CombatPower;
};

bool captureAttempt(Monster);
string setMonster(Monster[]);

int main()
{
	Monster monster, x;
	char input;
	bool didCatch = false;
	int pokeballs = 5;

	cout << "\t\t\tWelcome to Pokemon CatchEm" << endl << endl << endl;

	Monster monster;    // create instance of Monster called monster

	setMonster(x); // call setMonster() and assign it to monster   - am i doing this wrong?
	{
		cout << monster.Name;
		cout << monster.CombatPower;
	}

	cout << " A wild " << monster.Name << " appeared!" << endl << endl;
	cout << monster.Name;
	cout << monster.CombatPower;

	for (int i = 0; i < 5; i--)
	{
		i = pokeballs;

		{
			cout << "You have " << pokeballs << "left";
			cout << "Attempt to capture?  <y/n>";
			cin >> input;
		}

		if (input == 'y' || 'Y')
		{
			pokeballs--;
			return pokeballs;
		}

		else
		{
			cout << "Got away safely" << endl;
		}
	}

	bool	captureAttempt(didCatch);
	{
	if (didCatch == false)
	{
		cout << "Gotcha! You caught the " << monster.Name << " !" << endl;
	}
	else
	{

		cout << monster.Name << "broke free! Attempt to catch again? <y/n>" << endl;
		cin >> input;
	}

	return didCatch;
	}

	return 0;
}

bool captureAttempt(Monster)
{
	
	int chance, CombatPower;

	if (CombatPower > 200)
	{
		chance = rand() % 8;
	}

	else if (CombatPower > 99)
	{
		chance = rand() % 4;
	}

	else if (CombatPower < 100)
	{
		chance = rand() % 2;
	}

	if (chance == 0)
	{
		chance = true;
	}
		
		return chance;
}
	
string setMonster(Monster x)
{
	int randomNumber = 0;
	srand(time(NULL));
	x.Name[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";
	randomNumber = rand() % 25;
	cin >>	x.Name[randomNumber];

	int CombatPower;
	for (int i=0; i <450; i++)
	{
	CombatPower = rand() % 450 + 1;
	cin >> x.CombatPower;
	}
		return x.Name[randomNumber];
}


Im getting the following errors regarding these guys:

Monster monster; // 'Monster monster': redefinition
setMonster(x); // 'std::string setMonster(Monster[])': cannot convert argument 1 from 'Monster' to 'Monster []'

I'm not sure want to do with bool captureAttempt function. But I made a couple changes. I deleted the Monster monster (line 24) and I changed setMonster's prototype to match the definition. I also added a tempString variable.


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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <string>
#include <iostream>
#include <ctime>
using namespace std;

struct Monster
{
	string Name[25];
	int CombatPower;
};

bool captureAttempt(Monster);
string setMonster(Monster monster);

int main()
{
	Monster monster, x;		// create instance of Monster called monster
	char input;
	bool didCatch = false;
	int pokeballs = 5;
	string tempString;

	cout << "\t\t\tWelcome to Pokemon CatchEm" << endl << endl << endl;

	tempString  = setMonster(x); // call setMonster() and assign it to monster   - am i doing this wrong?
	//{
		//cout << monster.Name;
		//cout << monster.CombatPower;
	//}

	cout << " A wild " << monster.Name << " appeared!" << endl << endl;
	cout << monster.Name;
	cout << monster.CombatPower;

	for (int i = 0; i < 5; i--)
	{
		i = pokeballs;

		{
			cout << "You have " << pokeballs << "left";
			cout << "Attempt to capture?  <y/n>";
			cin >> input;
		}

		if (input == 'y' || 'Y')
		{
			pokeballs--;
			return pokeballs;
		}

		else
		{
			cout << "Got away safely" << endl;
		}
	}

	bool	captureAttempt(didCatch);
	{
		if (didCatch == false)
		{
			cout << "Gotcha! You caught the " << monster.Name << " !" << endl;
		}
		else
		{

			cout << monster.Name << "broke free! Attempt to catch again? <y/n>" << endl;
			cin >> input;
		}

		return didCatch;
	}

	return 0;
}

bool captureAttempt(Monster)
{

	int chance, CombatPower = 0;

	if (CombatPower > 200)
	{
		chance = rand() % 8;
	}

	else if (CombatPower > 99)
	{
		chance = rand() % 4;
	}

	else if (CombatPower < 100)
	{
		chance = rand() % 2;
	}

	if (chance == 0)
	{
		chance = true;
	}

	return chance;
}

string setMonster(Monster x)
{
	int randomNumber = 0;
	srand(time(NULL));
	x.Name[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";
	randomNumber = rand() % 25;
	cin >> x.Name[randomNumber];

	int CombatPower;
	for (int i = 0; i <450; i++)
	{
		CombatPower = rand() % 450 + 1;
		cin >> x.CombatPower;
	}
	return x.Name[randomNumber];
}
Topic archived. No new replies allowed.