Archived

This topic is now archived and is closed to further replies.

Happyman2003

Linking to another section of code like 'Int Main'?

Recommended Posts

Hi All I am fairly new to programming, and am having trouble figuring this out. How do I jump to another section of code like int main, and what is this called? I am trying to setup a text battle system. #include <stdlib.h> #include <ctime> #include <iomanip> #include <cstdlib> #include <iostream> using namespace std; int choice; int life = 100; int enemy_life = 100; int enemy_speed = 15; int enemy[5]; int round=0; int main() { system ("pause"); return 0; } int battle() { while (life>0 && enemy_life>0) { round++ cout<<"Life: " << setw(2) << life << " Enemy Life: " << setw(2) << enemy_life << endl << endl; if (round <=1) { cout <<"You have encountered an enemy" << endl <<"-----------------------------" << endl << endl; } cout <<"Please make your selection: " << endl << endl; cout <<"1. Attack enemy" << endl; cout <<"2. Cast spell (currently unavailable)" << endl; cout <<"3. Run" << endl; cout <<"-----------------------------" << endl; cout <<"Choice: "; cin >> choice; } } So, basically if a battle occurs in Int Main, how do jump to Int Battle, and then when the enemy dies, jump back again? Thanks Jason

Share this post


Link to post
Share on other sites
I would suggest that you learn the basics of C++ programming before you start on a game, but first off, if you want main() to initialize battle, you call battle from main, although you would want your battle to return void, so it should look like void battle(){.....}
That way, you can call it, without having to assign the outcome to an int. Perhasps clicky will help you a bit. also, if you wanted this to happen more than once and other stuff you may want a game loop, along the lines of
while(NOT_QUIT){
.......
}
with a key sequence/command to quit, then its flow of control would return to main.

Share this post


Link to post
Share on other sites
int main()
{
battle(); // <-- call battle();

system ("pause");
return 0;
}

int battle()
{
// if( you need to go back )
return 0;
}

and you need to prototype int battle() BEFORE main(), or main() wont recognize battle(). once you return you can't go back. you will need to call battle() again.

.lick


[edited by - Pipo DeClown on May 2, 2003 10:53:20 AM]

Share this post


Link to post
Share on other sites
ok, well, if you want to actually change your character''s life you can do it this way, change the signature of battle to look like this

int battle(int life){
....
return life;
}

then in main.....

life = battle(life);

or, you can ignore passing life anyway, because in your case, you are using a global variable, so you can take life down as far as you want, and not have to return anything to main. Generally this is a very bad practice, and I would suggest against it , and more define life and such in main() and pass the copies of the variables needed into battle. As it stands now, you would just have to use battle, and if your character''s life goes down to 50, the program already knows that.

Share this post


Link to post
Share on other sites
You could either pass in a pointer to a structure that you fill out in battle... (bit of code -> haven''t tested it at all)...


typedef struct
{
int player_hp;
} MyStruct;


battle(MyStruct * a_struct)
{
a_struct->player_hp =0;
}

int main(void)
{
MyStruct instance_of_struct;
battle(&instance_of_struct);
// here you now have ''instance_of_struct'' filled out.
printf("PLAYER HEALTH = %d \n",instance_of_struct.player_hp);

return 0;
}


////////////////
or alternatively if you need to return a single value from the function then you could just return it...

int battle(void)
{
int player_health=20;
return player_health;
}

//////////////
That''s all quite simple, I''m sure there are loads of neater ways to do this but I haven''t had nearly enough caffine yet :-)

Hope this helps

Share this post


Link to post
Share on other sites
With the struct method you could have loads of bits of data.

typedef struct
{
int health;
int weapon;
char player_name[50];
} MyStruct;

MyStruct another_structure;
so you''d only have to pass in the 1 variable...
battle(&another_structure);

Share this post


Link to post
Share on other sites