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

Started by
8 comments, last by Happyman2003 20 years, 11 months ago
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
Advertisement
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.
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]
Thanks for the replies. Quick questions. How would I go about returning values to main(). For example of my characters HP goes down by 50, how do I bring this back.

Thanks
Jason
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.
So, does that mean, if I have say 4 values that are declared in int main(), and need the values returned, that I have to do int battl3 (int 1, int 2, int 3, int 4)?

There''s gotta be an easier way.
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
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);
I''ve had nothing to do with structs before. Do you know of any good online tutorials for working with structs?
http://www.cs.cf.ac.uk/Dave/C/node9.html#SECTION00910000000000000000


isn''t too bad....

This topic is closed to new replies.

Advertisement