Thread: Function Help

  1. #1
    Games!? pujuman's Avatar
    Join Date
    Dec 2004
    Location
    Idaho, U.S.A.
    Posts
    14

    Function Help

    Alright, I need help with making a function. I have tried to comprehend what the tutorial is talking about, but, unfortunately, the tutorial does a function different than what I want to do with my function.

    I would paste most of my code here, but it has that 'newbie' look to it (jumpled code, because of a lack of functions). I will give a "snip-it" of the most basic thing I want to make a function of.

    Code:
    cout<<"It's a goblin, hurry attack."<<endl;
    			while (gob > 0){	//Battle Loop
    				attack = rand() % (HIGH - LOW + 1) + LOW;
    				cout<<"***Slash***"<<endl;
    				Sleep(1000);
    				cout<<"The Goblin has "<<gob - attack<<" life left!"<<endl;
    				gob = gob - attack;
    				Sleep(1500);
    				gobattack = rand() % (gobhi - goblo +1) + goblo;
    				if(gob > 0){
    				cout<<"***Whack***"<<endl;
    				Sleep(1000);
    				cout<<"You have "<<player - gobattack<<" life left!"<<endl;
    				player = player - gobattack;
    				Sleep(1500);
    				}
    			}
    			if(gob <= 0){
    				cout<<"Congratulations, you killed the goblin."<<endl;
    			}
    This is basically what I want to be a function. I have this loop being called right after the selection of a weapon (since there are 3 different choices, and I can't make functions, I have 3 humongous if statements all stating the same thing, only slightly different.).

    The code before the loop:
    Code:
    if(strcmp (answer, "2") == 0){    //First Drop --- To Buy
    		cout<<"Well, choose your weapon."<<endl;
    		cout<<"1--Sword - Damage is 4-10---"<<endl;
    		cout<<"2--Bow & Arrow - Damage 6-9---"<<endl;
    		cout<<"3--Axe - Damage 1-12---"<<endl;
    		cin.getline (buyanswer, 50);
    		if(strcmp (buyanswer, "1") == 0){   //Second Drop --- To Swd
    			const int LOW = 4;
    			const int HIGH = 10;
    			time(&seconds);
    			srand((unsigned int) seconds);
    			cout<<"You got a Sword!"<<endl;
    			Sleep(3000);
    			cout<<"Lets fight our first monster now. Get your sword ready!"<<endl;
    			Sleep(3000);
    I hope I didn't give too much information, I just wanted to give as much information as I though prudent to help with my function problem.

    Thanks in advance to anyone who helps me with my problem. (If anyone needs to see the ENTIRE code, I can give it in a private message, but a word of caution: it's a mess.)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should, in words, actually state what the purpose of your code is. Because reading the above twice, I still don't know what it is you're trying to do. You want a function to handle fighting? To handle weapon selection? What exactly?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Games!? pujuman's Avatar
    Join Date
    Dec 2004
    Location
    Idaho, U.S.A.
    Posts
    14
    Well, it's to handle the fight, as I stated, I want the first code I listed to be the function (which contains a comment stating it's a fight loop).

    Sorry, I thought I stated it clearly, obviously I didn't.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I think you want functions like ChooseWeapon().

    Then you'd have something like this:

    Code:
    if(strcmp (answer, "2") == 0)    //First Drop --- To Buy
        ChooseWeapon();
    For each function, you'll need:
    1. A function prototype
    2. A Function definition
    3. One or more function calls

    You can pass-in (into your function) as many values as you need, but a function can only return one value. If you need to affect more than one variable (which I think you do), you'll need to pass-in references or pointers.

    One function can call one or more other functions. Your program will probably do this when you're done.

  5. #5
    Games!? pujuman's Avatar
    Join Date
    Dec 2004
    Location
    Idaho, U.S.A.
    Posts
    14
    Ugh... Well, I need an example of how to do this, since I know no one is supposed to do it for me, but I usually need an example, so if you could use an example of calling more than one function and pointers, then that would help me a lot (be sure to explain, please).

    I read the tutorials but.. well.. as I stated about the function tutorial, it's not doing what I'm looking for.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, so you want a fight function. What does the function do? Break it down. Do you pass it to creatures who are going to fight? Do you pass it the weapons to be used in fighting? It really depends on how complex you want to make it.

    Do you know what structures and classes are? If not, I'd suggest at least reading on the very basics of both. In short, they're a way to bundle variables (and functions, but ignore that for now) together. Like a suit case.

    Then you could do something like this:
    Code:
    struct weapon
    {
        char name[20];
        int mindam, maxdam;
        char damagestring[40];
    };
    Then you could make an array of these items...
    Code:
    struct weapon theWeapons[] =
    {
        { "sword", 1, 10, "***slashes***" },
        { "club", 2, 6, "**bludgeons**" },
        { "Schildt Book", 10000, 10000, ">>>TEACHES WRONG THINGS<<<" }
    };
    You could then do something like:
    Code:
    void fightitout( struct weapon *attacker, struct weapon *victim )
    {
        ...do stuff with the weapons we pass...
    }
    
    ...stuff...
    
    /* Have a Schildt Book fight a club... */
    fightitout( theWeapons[2], theWeapons[1] );
    Now that's a crude example, and not just because I mention Schildt, but because I wouldn't really make a fight function take weapons as parameters. I'd make it take creatures instead...

    But you get the idea.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    A couple more thoughts...

    Start-out with a couple of simple functions. Maybe one that displays "You killed the goblin", and another that says "You're dead".

    Consider getting a C++ book. It looks like you're getting beyond these very limited tutorials. Everything you need to know is on the net, but finding it when you don't know exactly what you're looking for, and re-finding it the next time you need, it is not so easy!

    At this point, for your program, you really need to understand functions. Everything in C++ is done in functions. In big programs, just about the only thing in main() will be calls to other functions! You also need to understand pointers, and maybe references... hmmm... nothing on references in the tutorial.

    Like quzah, I was thinking about structures. Pointers to structures would be cool! I'm not suggesting that you use structures in this program... Just something to think about.... With data structures, your variables will look something like this:

    (psudo-code)
    PlayerDougDbug.Weapon = "Axe"
    PlayerDougDbug.Power = 50
    Playerpujuman.Weapon = "Sword"
    Playerpujuman.Power = 75

    Or, an array of structures like this:
    Player[0].Weapon = "Axe"
    Player[0].Power = 50
    Player[1].Weapon = "Sword"
    Player[1].power = 75

    Then, just when you really understand structures, it's time to learn about classes (and objects). A class/object is essentially a structure with built-in functions... Something like this:
    Playerpujuman.ChangePower()

    This type of game program is perfect for object oriented programming!

  8. #8
    Games!? pujuman's Avatar
    Join Date
    Dec 2004
    Location
    Idaho, U.S.A.
    Posts
    14
    Well, to my luck, my dad is a programmer and actually has 2-5 C/C++ books (along with Java and other things). I'll have to check those out. The only reason haven't asked him for help is because he doesn't do C/C++, he's a web programmer
    Last edited by pujuman; 01-07-2005 at 11:02 PM. Reason: typographical error

  9. #9
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    "Web-PROGRAMMER"...
    I dunno if I want to call Javacoders for programmers

  10. #10
    Banned
    Join Date
    Oct 2004
    Posts
    250
    You could do something like this....
    Code:
    class Weapon
    {
    public:
    	int GetDamage() const { return Weapon_Damage; }
    	void SetDamage(int Damage) { Weapon_Damage = Damage; }
    private:
    	int Weapon_Damage;
    };
    Code:
    Weapon Sword,Club,Knife;
    
    	Sword.SetDamage(10);
    	Club.SetDamage(20);
    	Knife.SetDamage(100);
    are inline functions still used or is the extra speed you gain not worth the increase in file size? or with the very efficient compilers thse days is thier any speed gain at all?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Da-Nuka
    "Web-PROGRAMMER"...
    I dunno if I want to call Javacoders for programmers
    There's a big difference between Java and JavaScript.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM