Create an instance of the structure and update the values.
Create an instance of the structure and update the values.
Sent from my iPadŽ
heres what i came up with, still not working
the structure
then the attack functionCode://structure to return values struct att fn(); { int hpr; int mpr; int expr; };
Code://attack function int attackm(int hp,int mp,int atck,int def,int exp,int ehp,int emp,int eatck,int edef,int eexp){ int amenu; int cehp=ehp; int chp=hp; int cmp=mp; int cemp=emp; //attack menu while(cehp>0&&chp>0){ system("CLS"); printf("1.Attack\n"); printf("2.Magic\n"); printf("3.Items\n"); printf("4.Run (roll to run)\n"); printf("\n You Enemy"); printf("\n%d/%d HP %d/%d HP",chp,hp,cehp,ehp); printf("\n%d/%d MP %d/%d MP\n",cmp,mp,cemp,emp); scanf("%d",&amenu); switch(amenu){ case 1: system("CLS"); cehp=cehp-atck; if(cehp<=0){ ehp=0; } printf("You do %d damage, enemy has %d HP left\n",atck,cehp); chp=chp-eatck; printf("You take %d damage, you have %d HP left\n",eatck,chp); cont(); break; } if(ehp<=0){ printf("You win!\n"); printf("You gain %d EXP\n!",eexp); exp=exp+eexp; cont(); } } struct att returns; returns.hpr=chp; returns.mpr=cmp; returns.expr=exp; return (returns); }
I still havn't figured it out
//structure to return values
This is trying to be too much. You need to first define the structure, and then make a function that returns one. Like so:Code:struct att fn(); { int hpr; int mpr; int expr; };We have now define what our structure will look like. Now just like making any other function return one type, we make a function that returns this one type. The type of this structure is 'struct foo'. This gives us:Code:struct foo { int bar; int baz; };Now a call to this function might look something like:Code:struct foo myfunction( void ) { struct foo x; /* declare a 'struct foo' instance called x. */ /*...do stuff...*/ return x; }Code:struct foo abc; abc = myfunction( );
Quzah.
Hope is the first step on the road to disappointment.
Originally Posted by xxwerdxx
If you want to set the values of the integers that you are passing into your attack routine, you have to pass the address of the integer.
As your program is written now, you are passing by value:
The value of foo and bar will be placed on the stack and available to your function, but they just that -- values. Changing them in your attack routine will have no effect on the value back in your calling routine.Code:attack(int foo, int bar)
Doing something like this:
won't work either, because you still don't have the address of the ints in your calling routine.Code:attack(int foo, int bar) { int *pfoo = &foo; int *pbar = &bar; ... }
You have to pass by reference (at least that's what they used to call it when I started programming):
Instead of calling attack like this:Code:attack(int *foo, int *bar) { *foo = whatever value you're setting for foo *bar = whatever value you're setting for bar ... }
You'd call it like this:Code:int foo = 0; int bar = 100; attack(foo, bar);
You can apply the same technique to structures. Think of the structure as a container that will nicely hold all of your values. That way, you pass your attack routine a pointer to the structure, manipulate the values inside the structure, and return.Code:int foo = 0; int bar = 100; attack(&foo, &bar);
And finally, as someone said early, you probably want to take a more object oriented approach -- that way you can encapsulate methods (i.e. "routines") along with the data in an object. The objects will represent various entities in your game.
Hope this has helped somewhat....