Thread: C void functions-- help please?

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    C void functions-- help please?

    hey everyone im looking for some help i just started a programing unit and im not looking for poeple to do my work for me but i am looking for help... the program im writing randomly creates fish and adds them to an array but its ment to do this in a void function and i just dont understand that so if anyone could give some advice with maby some example code that i could understand you would help me out so much...ive tryed lookin through tutroials but im still comming up blank

    here is my code ...its rather messy and for that im sorry...but its my first time programing

    Code:
     
    
    #define PERCHMAX  10000
    //Defines Minimum Weight For ANy fish
    #define MIN 500
    //Declares Max Weight of basket
    #define MAXWBASKET  15000
    //Max Number Fish To Pick From
    #define AviliableFish  4
    
    struct fish{
    char fishName[30];
    char latinName[40];
    int weight;
    };
    
    struct fishing{
           
           int numberFish;
           int totalWeight;
           struct fish Basket[50];
           };
           
           
    typedef struct fish Fish;
    typedef struct  fishing Fishing;
    
    #include "Fish.h"
    
    
    
    
    //This Function is Designed to Imitate The Fishing Process
    
    //This Function Prints The Results That The porgram Returns
    
    
    
    int main(void){
        //Prints Introduction then waits till user hits key to continue 
        printf("Welcome To The Fishing Program \n");
        printf("This Program was Created By Shane Innes (2700508)\n");
        Fishing *Basket;
        void fishing(Fish *weight ,Fishing *Basket;);
        init_array(s_array, n);
        print_array(s_array,n);
        //Pauses System and waits for a reply from user
        system("PAUSE");
        return 0;
        
    
    
     //   strcpy(a.fishName, "Shane Innes");
    
        
    }
    
    void fishing(Fishing *Basket, numberFishCaught)
    {
         //Pauses Program And waits for a keystroke
        int totalweight ;
        int numberFishCaught = 0 ;
        
        Fish a,b,c,d;
        Fishing Fishing;
        //Creates a fishcheck
        //x becomes total weight
        int x ;
        //Creates A random number Genrater based of the time
        srand(time(0));
        //Creates a while loop, While fish weight is less than max basket weight  keep going
        while (  x < MAXWBASKET){
        int fishType;
        fishType = ((rand()&#37;AviliableFish)+ 1  ); //creates a random number between 1 and 4
        switch (fishType)		//Switch Statment Checks Values Agnsit Fishtype vairble
        {
        	/* If expression resolves to 1, jump here */
            case 1:
            printf("case1");
            a.fishName, "Australian Bass";
            a.latinName, "Macquaria Novemaculeata";
            int FishWeight;
            FishWeight = ((rand()%AUSBASMAX)+ 1  );
            a.weight = FishWeight;
            if (FishWeight > 500 && FishWeight < AUSBASREC)
            {
                           numberFishCaught = numberFishCaught + 1;
                           Fishing.numberFish = numberFishCaught;
                           Fishing.Basket[numberFishCaught]= a;
                           x = x + FishWeight;
                           printf("Caught : %s\n",a.fishName);
                           printf("Weight : %d\n",a.weight);
                           printf("Status : Was Put In Basket");
            }
            else if (FishWeight < 500 || FishWeight > AUSBASREC)
            {
                           printf("Caught : %s\n",a.fishName);
                           printf("Weight : %d\n",a.weight);
                           printf("Status : Was release Back Into Stream");
            }
                           
                           
                           
            break; /* break - cases the execution to jump  out of the 'switch' block.             */
       
    	    /* If expression resolves to 2, jump here */
            case 2:
            printf("case2");
            
            b.fishName, "Fish 2";
            b.latinName, "Fish 2";
            FishWeight = ((rand()%SHORTFINEELMAX)+ 1  );
            a.weight = FishWeight;
            if (FishWeight > 500 && FishWeight < SHORTFINEELREC)
            {
                           numberFishCaught = numberFishCaught + 1;
                           Fishing.numberFish = numberFishCaught;
                           Fishing.Basket[numberFishCaught]= b;
                           x = x + FishWeight;
                           printf("Caught : %s\n",b.fishName);
                           printf("Weight : %d\n",b.weight);
                           printf("Status : Was Put In Basket");
            }
            else if (FishWeight < 500 || FishWeight > SHORTFINEELREC)
            {
                           printf("Caught : %s\n",b.fishName);
                           printf("Weight : %d\n",b.weight);
                           printf("Status : Was release Back Into Stream");
            }
            break;
       
       
       
       
       
       
       
    	    /* If expression resolves to 3, jump here */
            case 3:
            printf("case3");
            c.fishName, "Fish 3";
            c.latinName, "Fish 3";
            FishWeight = ((rand()%EELCATFISHMAX)+ 1  );
            a.weight = FishWeight;
            if (FishWeight > 500 && FishWeight < EELCATFISHREC)
            {
                           numberFishCaught = numberFishCaught + 1;
                           Fishing.numberFish = numberFishCaught;
                           Fishing.Basket[numberFishCaught]= c;
                           x = x + FishWeight;
                           printf("Caught : %s\n",c.fishName);
                           printf("Weight : %d\n",c.weight);
                           printf("Status : Was Put In Basket");
            }
            else if (FishWeight < 500 || FishWeight > EELCATFISHREC)
            {
                           printf("Caught : %s\n",c.fishName);
                           printf("Weight : %d\n",c.weight);
                           printf("Status : Was release Back Into Stream");
            }
            break;
        
            case 4:
            printf("case4");
            d.fishName, "Fish 4";
            d.latinName, "Fish 4";
            FishWeight = ((rand()%PERCHMAX)+ 1  );
            a.weight = FishWeight;
            if (FishWeight > 500 && FishWeight < PERCHREC)
            {
                           numberFishCaught = numberFishCaught + 1;
                           Fishing.numberFish = numberFishCaught;
                           Fishing.Basket[numberFishCaught]= d;
                           x = x + FishWeight;
                           printf("Caught : %s\n",d.fishName);
                           printf("Weight : %d\n",d.weight);
                           printf("Status : Was Put In Basket");
            }
            else if (FishWeight < 500 || FishWeight > PERCHREC)
            {
                           printf("Caught : %s\n",d.fishName);
                           printf("Weight : %d\n",d.weight);
                           printf("Status : Was release Back Into Stream");
            }
            
            break;
    
    
    	    /* Any other value jumps here.            */
            default:
            printf("A Error Occured This dose not effect results please ignore\n");
            break;
                  }
              }
         }
         
    void print_basket(Fishing *Basket, numberFishCaught){
     int i;
         for(i = 1; i < n; i++){
               Basket[i]
    Thanks agian if anyone could help out
    in case i wasnt clear i wasnt sure how i should be declaring my void function as well as calling it in my main code
    Last edited by shane981; 03-28-2008 at 05:31 AM. Reason: clarity

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    /* declaration */
    void add(int a, int b, int* res);
    
    /* calling */
    int main(void)
    {
       int a = 5;
       int res;
       add(a, 6, &res);
       return 0;
    }
    
    /* definition */
    void add(int a, int b, int *res)
    {
       if(res)
          * res = a+b;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    in the requirements the example if of the void functin is
    void fishing( struct fish*, struct fishing*)
    and i dont understand this....its ok if no one can help ill just wait till i can get in touch wif my lecturer..just trying 2 figure it out and get a good start on this

  4. #4
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Quote Originally Posted by shane981 View Post
    in the requirements the example if of the void functin is
    void fishing( struct fish*, struct fishing*)
    and i dont understand this....its ok if no one can help ill just wait till i can get in touch wif my lecturer..just trying 2 figure it out and get a good start on this
    You would call it like this:

    Code:
    struct fish *fish_ptr;
    struct fishing *fishing_ptr;
    
    fishing(fish_ptr, fishing_ptr);
    You are passing the function two pointers, which means you're passing those parameters by reference. This means that if you alter the values of the pointer or the struct it points to inside the fishing() function, they'll be altered in the main() function too. Therefore, you don't have to return anything from the fishing() function. Passing by reference accomplishes the same thing, in effect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. saying hello and 1st question
    By darksys in forum C Programming
    Replies: 12
    Last Post: 10-31-2008, 02:58 PM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM