Thread: Can anyone tell me why this doesn't work?

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    1

    Can anyone tell me why this doesn't work?

    MainGame.cpp
    Code:
    #include <iostream>
    #include "functions.h"
    using namespace std;
    
    
    int main()
    {
        int attack;
        Leviathan(attack);
        cout << "Attack: " <<attack << endl;
        system("pause");
        return 0;
    }

    functions.h
    Code:
    int Leviathan(int attack);

    MonsterList.cpp
    Code:
    int Leviathan(int attack)
    {
        attack = 5;
        return attack;
    }

    I'm trying to get attack to return from MosterList.cpp to MainGame.cpp as 5.
    But it is always 0.


    How do I get it to return what I changed in the MonsterList.cpp?

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    You are not assigning a value to attack.
    Leviathan should be
    Code:
    void Leviathan( int& attack )
    { attack = 5;
    }
    
     Or 
    int Leviathan ( )
    {
      return 5;
    } // assign with = i.e. int attack = Leviathan();

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    When you pass in your variable attack into Leviathan, a new local variable to the function Leviathan is created and the value you passed in (the value in the attacked variable) is copied over.
    Hence, any changes you make to the attack is lost when the function ends since local variables are then disposed.
    If you instead make it a reference (Raigne provided the syntax), you tell the compiler that the variable attack in Leviathan shall not be a local variable, but instead should be same variable that you pass in.
    So, since you pass in attack, the variable attack in Leviathan refers to the variable attack in main (they need not have the same name).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    I think the responses to his question have gotten far too complicated. (it's clear the original poster does not yet understand references &, etc etc, so I hope this explanation is much clearer) (Even though the OP is using the function as though it were accepting a reference)

    You called a function called 'Leviathan', it returned the number 5, but you did not save it anywhere.

    Replace the following line in MainGame.cpp:
    Code:
        Leviathan(attack);
    to
    Code:
        attack = Leviathan(attack);
    What this does is store the value that is 'returned' by the function Leviathan where you want it, which is in the variable 'attack'.
    Last edited by rodrigorules; 09-09-2012 at 12:54 AM.
    Check out my programming / algorithm blog @ http://www.swageroo.com

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by rodrigorules View Post
    I think the responses to his question have gotten far too complicated. (it's clear the original poster does not yet understand references &, etc etc, so I hope this explanation is much clearer) (Even though the OP is using the function as though it were accepting a reference)
    Which is precisely why attempts have been made to explain references since they're integral to the language...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-07-2010, 06:53 AM
  2. If doesn't work
    By rain-13 in forum C++ Programming
    Replies: 3
    Last Post: 12-24-2009, 09:11 AM
  3. Why doesn't it work?
    By fenikkusu in forum C Programming
    Replies: 4
    Last Post: 01-10-2009, 02:56 AM
  4. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM
  5. do-while doesn't work
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 12-25-2001, 07:05 PM

Tags for this Thread