Problem with structure function

This is a discussion on Problem with structure function within the C++ Programming forums, part of the General Programming Boards category; hi, it's me again. This time I am having problems writing a function that alters the data of the structure ...

  1. #1
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46

    Problem with structure function

    hi, it's me again. This time I am having problems writing a function that alters the data of the structure type. this is what i have can anyone tell me why it doesn't work. Thanks
    Code:
    #include <iostream.h>
    #include <string>
    
    using namespace std;
    
    struct individualtype ///////includes monsters and characters
    {
    	string mainname;
    	string name;
    	int level;
    	float maxhp;
    	float hp;
    	float maxmp;
    	float mp;
    	float luck;
    	float vitality;
    	float strength;
    	float accuracy;
    	float speed;
    	float defense;
    	float blackmagic;
    	float whitemagic;
    	float greenmagic;
    	float skillpoints;
    }; 
    
    individualtype adventurer;
    individualtype monster;
    
    int setpoints(int level, individualtype x)
    {
    	//if level one
    	x.maxhp=10;
    	x.maxmp=6;
    	x.luck=2;
    	x.vitality=2;
    	x.strength=7;
    	x.accuracy=5;
    	x.speed=5;
    	x.defense=7;
    	x.blackmagic=2;
    	x.whitemagic=2;
    	x.greenmagic=2;
    	x.skillpoints=0;
    	////if past level one
    	for(int b=1;b<x.level;b++)
    	{
    		x.maxhp=x.maxhp*3/2;
    		x.maxmp=x.maxmp*4/3;
    		x.luck=x.luck*5/4;
    		x.vitality=x.vitality*5/4;
    		x.strength=x.strength*9/7;
    		x.accuracy=x.accuracy*6/5;
    		x.speed=x.speed*6/5;
    		x.defense=x.defense*9/7;
    		x.blackmagic=x.blackmagic*5/4;
    		x.whitemagic=x.whitemagic*5/4;
    		x.greenmagic=x.greenmagic*5/4;
    	}	
    	return 0;
    }
    
    int main();
    {
              adventurer.level=1;
              monster.level=1;
              cout<<"Hello"<<endl;
              setpoints(adventurer.level, adventurer);
              setpoints(monster.level, monster);
              cout<<monster.maxhp<<endl;
              return 0;
    }
    all the values remain 0 they do not change when i try to display them. I think it should work, someone please correct my reasoning I think i may be off.

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    [QUOTE=gell10]
    Code:
    for(int b=1;b<x.level;b++)
     
     
    int main();
    {
    adventurer.level=1;
    monster.level=1;
    setpoints(adventurer.level, adventurer);
    setpoints(monster.level, monster);
    I see that you are initilising adventurer.level and monster.level to 1 in the for lopp you are using the value of x.level to control the loop.. when it is 1 the loop will not run since the condion is b=1;b<1;b++ .. It will not run because b is not less than 1....

    to make this work use

    [QUOTE]
    Code:
    for(int b=1;b<=x.level;b++)
    or


    Code:
    for(int b=0;b<x.level;b++)

  3. #3
    Registered User electRONix's Avatar
    Join Date
    Jun 2004
    Posts
    13
    couple pointers..
    int main(); ... semicolon =d u probably caught that yourself... and instead of doing something like
    x=x*3/2;
    u can do this
    x*=3/2;
    ...
    and in addition to help debug your program... try just changing one attribute and then checking to see if was changed... in other words, start small, make sure it works, and then add on...
    For example, you might want to start with a simpler structure that has just has strength and level, and then see if you can increase strength according to the level.

    if you still have problems email me. your code looks easy enough to debug i can work on it with you. i'd take a closer look now but its almost 2 am and im gettin a lil tired =d
    my spider-sense be tingling.

    Ron - SCU Math/CS Major

  4. #4
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46
    hehe ok but my problem is i want to be able to alter the data in my structures through a generic function
    for example (this is code i am making up right now so bear with me) =)
    Code:
    #include <iostream.h>
    
    int businessnumbergenerator(business x);
    
    struct business
    {
            float income;
            float expenses;
    };
    
    business mcdonalds;
    business dennys;
    
    int main()
    {
            cout<<"check businesses"<<endl;
            businessnumbergenerator(mcdonalds);
            businessnumbergenerator(dennys);
            return 0;
    }
    
    int businessnumbergenerator(business x);
    {
            x.income=50;
            x.expenses=34;
            return 0;
    }

  5. #5
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46
    i guess my question is, is that code possible because it doesn't seem to work for me

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,410
    Are you doing this for a hobby?

    It might be easier to manage this using classes in an objected oriented model instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46
    yeah i'm a beginner, i'm trying to learn about functions and stuff before i move onto graphics. is there a way to perform the function i said above or would i have to type out each structure individually? am i making sense? do i have to use classes instead?

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Currently, you are passing the structure by value. This means that a copy of the structure is passed to the function. Changes made to the copy will not be reflected in the original.

    You need to pass the structure by reference. This means that a reference to the original is passed to the function instead of a copy.

    Code:
    int main()
    {
            cout<<"check businesses"<<endl;
            businessnumbergenerator(mcdonalds);
            businessnumbergenerator(dennys);
            return 0;
    }
    
    // The & specifies that the argument should be passed by reference
    int businessnumbergenerator(business & x);
    {
            x.income=50;
            x.expenses=34;
            return 0;
    }
    Alternatively, you can pass a pointer to the original.
    Code:
    int main()
    {
            cout<<"check businesses"<<endl;
            // Pass the address of each structure to the function
            businessnumbergenerator(&mcdonalds);
            businessnumbergenerator(&dennys);
            return 0;
    }
    
    // The asterisk signifies that this function takes a pointer to a business structure
    int businessnumbergenerator(business * x);
    {
            // We use the -> operator instead of . This dereferences the structure
            // pointer and accesses the specified member.
            x->income=50;
            x->expenses=34;
            return 0;
    }
    Last edited by anonytmouse; 06-06-2004 at 04:39 AM.

  9. #9
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46
    Thank you that works wonderfully :-D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. problem getting structure to work in my function
    By Tom Bombadil in forum C Programming
    Replies: 18
    Last Post: 05-28-2009, 09:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 04:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21