Thread: Problems with functions

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    103

    Problems with functions

    I really really need to get this done urgently. I'm trying to do an agent based model on plants and humans on a spaceship here's the code:

    Code:
    //preprocessor directives
    #include "supercomputingMath.h"
    
    //plant structs
    struct Plant soybeans;
    struct Plant potatoes;
    struct Plant tomatoes;
    struct Plant strawberries;
    
    //human structs
    struct Human dhaivat;
    struct Human colin;
    struct Human matthew;
    struct Human michael;
    struct Human mrscooper;
    int isAlive;
    //define globals
    long time;
    
    //the amount of oxygen will be 20% of the spaceship size
    int spaceShipSize;
    int oxygenInSpaceShip;
    int CO2InSpaceShip;
    //number of plants
    int amountOfPlants; 
    int amountOfSoybeans;
    int amountOfPotatoes;
    int amountOfTomatoes;
    int amountOfStrawberries;
    int counterForPlants;
    int counterForPlants2;
    int caloriesForTheDay;
    //initialize the structs and other variables
    static void initVariables(){
    	soybeans.foodDays=60;
    	soybeans.maxAge=60;
    	soybeans.CO2UsedPerDay=1;
    	soybeans.caloriesPerDay=200;
    	
    	potatoes.foodDays=45;
    	potatoes.maxAge=45;
    	potatoes.CO2UsedPerDay=1;
    	potatoes.caloriesPerDay=30;
    	
    	tomatoes.foodDays=60;
    	tomatoes.maxAge=180;
    	tomatoes.CO2UsedPerDay=2;
    	tomatoes.caloriesPerDay=45;
    	
    	strawberries.foodDays=0;
    	strawberries.maxAge=90;
    	strawberries.CO2UsedPerDay=1.0;
    	strawberries.caloriesPerDay=0.4;
    	
    	int time=0;
    	
    	
    }
    
    //main function
    int main (void) {
    	initVariables();
    	//get spaceship size
    	printf("Please enter the size of the spaceship (any units, 20 percent of it will be oxygen):");
    	scanf("%ld",&spaceShipSize);
    	oxygenInSpaceShip=spaceShipSize*0.2;
    	
    	printf("Please enter the number of plants :");
    	scanf("%i", &amountOfPlants);
    	timer();
    	isItHarvestTime(time);
    	
    	//run functions
    	oxygenAbsorbedByHumans(time);
    	carbonDioxideReleasedByHumans(time);
        return 0;
    }
    
    //a function to keep time by hours
    long timer(void) {
    	
    	time++;
    	caloriesForTheDay=0;
    	return time;
    }
    
    //a function that keeps track of the oxygen the humans are able to absorb
    void oxygenAbsorbedByHumans (long currentDay) {
    	
    	if(isHumanAlive(dhaivat.oxygenAbsorbedForHuman)==1 && oxygenInSpaceShip-dhaivat.oxygenAbsorbedForHuman>0) 
    		dhaivat.oxygenAbsorbedForHuman=currentDay*120*24;
    	else {
    		printf("Dhaivat is dead");
    	}
    	if(isHumanAlive(colin.oxygenAbsorbedForHuman)==1 && oxygenInSpaceShip-colin.oxygenAbsorbedForHuman>0 ) 
    		colin.oxygenAbsorbedForHuman=currentDay*120*24;
    	else {
    		printf("Colin is dead");
    	}
    	if(isHumanAlive(matthew.oxygenAbsorbedForHuman)==1 && oxygenInSpaceShip-matthew.oxygenAbsorbedForHuman>0) 
    		matthew.oxygenAbsorbedForHuman=currentDay*120*24;
    	else {
    		printf("Matthew is dead");
    	}
    	if(isHumanAlive(michael.oxygenAbsorbedForHuman)==1 && oxygenInSpaceShip-michael.oxygenAbsorbedForHuman>0) 
    		michael.oxygenAbsorbedForHuman=currentDay*120*24;
    	else {
    		printf("Michael is dead");
    	}
    	
    	if(isHumanAlive(mrscooper.oxygenAbsorbedForHuman)==1 && oxygenInSpaceShip-mrscooper.oxygenAbsorbedForHuman>0) 
    		mrscooper.oxygenAbsorbedForHuman=currentDay*120*24;
    	printf("Day %ld: \n", currentDay);
    	printf("Dhaivat has absorbed %d units of oxygen. \n", dhaivat.oxygenAbsorbedForHuman);
    	printf("Colin has absorbed %d units of oxygen. \n", colin.oxygenAbsorbedForHuman);
    	printf("Matthew has absorbed %d units of oxygen. \n", matthew.oxygenAbsorbedForHuman);
    	printf("Michael has absorbed %d units of oxygen. \n", michael.oxygenAbsorbedForHuman);
    	
    }
    
    //keeps track of the carbon dioxide released by each human
    void carbonDioxideReleasedByHumans(long currentDay) {
    	dhaivat.carbonDioxideReleasedForHuman=currentDay*35.52*24;
    	colin.carbonDioxideReleasedForHuman=currentDay*35.52*24;
    	matthew.carbonDioxideReleasedForHuman=currentDay*35.52*24;
    	michael.carbonDioxideReleasedForHuman=currentDay*35.52*24;
    	mrscooper.carbonDioxideReleasedForHuman=currentDay*35.52*24;
    }
    
    //find out which humans are alive and which ones are dead. 
    int isHumanAlive(int oxygenAbsorbedForHuman) {
    	if(oxygenAbsorbedForHuman<2880) {
    		isAlive=0;
    		return isAlive;
    	}
    	else {
    		isAlive=1;
    		return isAlive;
    	}
    }
    
    
    void setAmountOfPlants(int amountOfPlants) {
    	//amountOfPlants = 2500+(100*counterForPlants);
    	
    	amountOfSoybeans=amountOfPlants*0.40;
    	amountOfPotatoes=amountOfPlants*0.15;
    	amountOfTomatoes=amountOfPlants*0.15;
    	amountOfStrawberries=amountOfPlants*0.15;
    	
    	
    }
    void isItHarvestTime (long currentDay) {
    	if(currentDay%60==0 && soybeans.isItAlive==1) {
    		caloriesForTheDay=caloriesForTheDay+200*amountOfSoybeans;
    	}
    	if(currentDay%45==0 && potatoes.isItAlive==1) {
    		caloriesForTheDay=caloriesForTheDay+30*amountOfSoybeans;
    	}
    	if(currentDay%1==0 && currentDay!=90  && strawberries.isItAlive==1) {
    		caloriesForTheDay=caloriesForTheDay+0.4*amountOfStrawBerries
    	}	
    	else {
    		if(currentDay==90) {
    			strawberries.isItAlive=0;
    		}
    		else {
    		
    		}
    	}
    }
    here's the header file:

    Code:
    struct Human{
        int oxygenAbsorbedForHuman;
        int carbonDioxideReleasedForHuman;
    	int isAlive;
    };
    struct Plant{
    	int foodDays;
    	int maxAge;
    	int caloriesPerDay;
    	int CO2UsedPerDay;
    	int isItAlive;
    };
    struct Plant soybeans;
    struct Plant potatoes;
    struct Plant tomatoes;
    struct Plant strawberries;
    
    //define globals
    int time=-1;
    
    //the amount of oxygen will be 20% of the spaceship size
    int spaceShipSize;
    int oxygenInSpaceShip;
    
    //define struct variables
    struct Human dhaivat;
    struct Human colin;
    struct Human matthew;
    struct Human michael;
    struct Human mrscooper;
    
    
    //number of plants
    int amountOfPlants; 
    int amountOfSoybeans;
    int amountOfPotatoes;
    int amountOfTomatoes;
    int amountOfStrawberries;
    int counterForPlants;
    int counterForPlants2;
    
    //prototype functions
    int timer(void);
    void oxygenAbsorbedByHumans (long currentDay);
    int isHumanAlive(struct Human someHuman);
    void carbonDioxideReleasedByHumans(long currentDay);
    void setAmountOfPlants(int amountOfPlants);
    void isItHarvestTime(long currentDay);
    But when I compile it, its giving me the following error:
    154: structure has no member named "isItAlive"
    157: (as above)
    160: (as above)

    165: (as above )
    Last edited by Poincare; 04-19-2009 at 02:37 PM. Reason: corrected errors

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    103
    anyone?

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Where is line 154? (highlight it) Does the structure it refer to has a member named "isItAlive"?

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    103
    thank you for replying cyberfish. But, I got the problem solved (i had used the wrong header file)

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Poincare View Post
    thank you for replying cyberfish. But, I got the problem solved (i had used the wrong header file)
    Hey -- I hope you can deduce why you solved this before anyone else even bothered to try (just in case you actually want some help with something in the future).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 10:56 AM
  2. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  3. Problems with functions in a class with pointer to self
    By BigDaddyDrew in forum C++ Programming
    Replies: 6
    Last Post: 02-03-2003, 11:24 PM
  4. problems passing parameters between functions
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-21-2001, 11:41 AM
  5. problems with functions
    By catalyst in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 06:55 AM