Feb 22, 2014 at 7:29am Feb 22, 2014 at 7:29am UTC
I am trying to teach myself C++ through the use of e-books and videos and I have a question on classes.
I am using a basic text based pokemon game as an example and I am trying to add a battle sequence.
The issue i am having is I am unable to reference my class in a new function as it seems its on an entire different scope. When I go to use it such as starter.move1 it will not recognize the class in a new function.
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
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class pokemon
{
public :
string name;
int hp;
int level;
string move1;
string move2;
string move3;
string move4;
void attack();
};
void battle();
void firstpokemon();
int main()
{
firstpokemon();
char answer;
cout << "Do you want to make your way to Viridian City? (y/n) " ;
cin >> answer;
switch (answer)
{
case 'y' :
battle();
break ;
case 'n' :
cout << endl << "You stay at proffesor oaks lab!\n" ;
break ;
}
}
void firstpokemon()
{
pokemon starter;
starter.hp = 10;
starter.level = 5;
int starternum = 0;
do {
cout << "Would you like to pick Charmander(1), Squirtel(2), or Bulbasaur(3): " ;
cin >> starternum;
}while (starternum < 1 || starternum > 3);
switch (starternum)
{
case 1:
cout << "You chose charmander!\n" ;
starter.name = "Charmander" ;
starter.move1 = "scratch" ;
starter.move2 = "leer" ;
break ;
case 2:
cout << "You chose squirtel!\n" ;
starter.name = "Squirtel" ;
starter.move1 = "Tackle" ;
starter.move2 = "Tail Whip" ;
break ;
case 3:
cout << "You chose bulbasaur!\n" ;
starter.name = "Bulbasaur" ;
starter.move1 = "Tackle" ;
starter.move2 = "Growl" ;
break ;
}
cout << endl << endl << "Your starter pokemon is: " << starter.name << ". It is level: " << starter.level << endl;
}
void battle()
{
cout << "A wild pidgy has appeared!\n" ;
cout << "What move would you like to use\n" ;
}
Last edited on Feb 22, 2014 at 7:32am Feb 22, 2014 at 7:32am UTC
Feb 22, 2014 at 9:40am Feb 22, 2014 at 9:40am UTC
You mean the pokemon object created on line 41? Class objects follow the same scoping rules as any other type (int, char, std::string, etc.). If you declare it in one function it will not be in scope in other functions.
Consider passing the object by reference to the functions that use them, or if a function creates a new pokemon as firstpokemon() do then you could make it the return value.
Feb 22, 2014 at 3:31pm Feb 22, 2014 at 3:31pm UTC
I think I understand what you mean by passing it through by reference...
the issue I am having is how can I return all of those class values at once if I do that?
I use the funtion starterpokemon and in that function it declares 3 variables, how could I return all three of those
this is what I have
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 "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class pokemon
{
public :
string name;
int hp;
int level;
string move1;
string move2;
string move3;
string move4;
void attack();
};
pokemon firstpokemon(pokemon starter);
int main()
{
pokemon starter;
firstpokemon(starter);
cout << endl << endl << "Your starter pokemon is: " << starter.name << ". It is level: " << starter.level << endl;
char answer;
cout << "Do you want to make your way to Viridian City? (y/n) " ;
cin >> answer;
switch (answer)
{
case 'y' :
break ;
case 'n' :
cout << endl << "You stay at proffesor oaks lab!\n" ;
break ;
}
}
pokemon firstpokemon(pokemon starter)
{
starter.hp = 10;
starter.level = 5;
int starternum = 0;
do {
cout << "Would you like to pick Charmander(1), Squirtel(2), or Bulbasaur(3): " ;
cin >> starternum;
}while (starternum < 1 || starternum > 3);
switch (starternum)
{
case 1:
cout << "You chose charmander!\n" ;
starter.name = "Charmander" ;
starter.move1 = "scratch" ;
starter.move2 = "leer" ;
break ;
case 2:
cout << "You chose squirtel!\n" ;
starter.name = "Squirtel" ;
starter.move1 = "Tackle" ;
starter.move2 = "Tail Whip" ;
break ;
case 3:
cout << "You chose bulbasaur!\n" ;
starter.name = "Bulbasaur" ;
starter.move1 = "Tackle" ;
starter.move2 = "Growl" ;
break ;
}
return starter;
}
This is my output:
1 2 3 4 5 6 7 8 9
Would you like to pick Charmander(1), Squirtel(2), or Bulbasaur(3): 1
You chose charmander!
Your starter pokemon is: . It is level: -858993460
Do you want to make your way to Viridian City? (y/n) n
You stay at proffesor oaks lab!
Press any key to continue . . .
I am not sure how I can return the level and name.
Last edited on Feb 22, 2014 at 4:24pm Feb 22, 2014 at 4:24pm UTC
Feb 22, 2014 at 5:20pm Feb 22, 2014 at 5:20pm UTC
Either :
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
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class pokemon
{
public :
string name;
int hp;
int level;
string move1;
string move2;
string move3;
string move4;
void attack();
};
pokemon firstpokemon();
int main()
{
pokemon starter;
starter=firstpokemon();
cout << endl << endl << "Your starter pokemon is: " << starter.name << ". It is level: " << starter.level << endl;
char answer;
cout << "Do you want to make your way to Viridian City? (y/n) " ;
cin >> answer;
switch (answer)
{
case 'y' :
break ;
case 'n' :
cout << endl << "You stay at proffesor oaks lab!\n" ;
break ;
}
}
pokemon firstpokemon()
{
pokemon starter;
starter.hp = 10;
starter.level = 5;
int starternum = 0;
do {
cout << "Would you like to pick Charmander(1), Squirtel(2), or Bulbasaur(3): " ;
cin >> starternum;
}while (starternum < 1 || starternum > 3);
switch (starternum)
{
case 1:
cout << "You chose charmander!\n" ;
starter.name = "Charmander" ;
starter.move1 = "scratch" ;
starter.move2 = "leer" ;
break ;
case 2:
cout << "You chose squirtel!\n" ;
starter.name = "Squirtel" ;
starter.move1 = "Tackle" ;
starter.move2 = "Tail Whip" ;
break ;
case 3:
cout << "You chose bulbasaur!\n" ;
starter.name = "Bulbasaur" ;
starter.move1 = "Tackle" ;
starter.move2 = "Growl" ;
break ;
}
return starter;
}
or
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
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class pokemon
{
public :
string name;
int hp;
int level;
string move1;
string move2;
string move3;
string move4;
void attack();
};
void firstpokemon(pokemon &starter);
int main()
{
pokemon starter;
firstpokemon(starter);
cout << endl << endl << "Your starter pokemon is: " << starter.name << ". It is level: " << starter.level << endl;
char answer;
cout << "Do you want to make your way to Viridian City? (y/n) " ;
cin >> answer;
switch (answer)
{
case 'y' :
break ;
case 'n' :
cout << endl << "You stay at proffesor oaks lab!\n" ;
break ;
}
}
void firstpokemon(pokemon &starter)
{
starter.hp = 10;
starter.level = 5;
int starternum = 0;
do {
cout << "Would you like to pick Charmander(1), Squirtel(2), or Bulbasaur(3): " ;
cin >> starternum;
}while (starternum < 1 || starternum > 3);
switch (starternum)
{
case 1:
cout << "You chose charmander!\n" ;
starter.name = "Charmander" ;
starter.move1 = "scratch" ;
starter.move2 = "leer" ;
break ;
case 2:
cout << "You chose squirtel!\n" ;
starter.name = "Squirtel" ;
starter.move1 = "Tackle" ;
starter.move2 = "Tail Whip" ;
break ;
case 3:
cout << "You chose bulbasaur!\n" ;
starter.name = "Bulbasaur" ;
starter.move1 = "Tackle" ;
starter.move2 = "Growl" ;
break ;
}
}
Last edited on Feb 22, 2014 at 5:22pm Feb 22, 2014 at 5:22pm UTC