Thread: Call by reference help!

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

    Call by reference help!

    Hi people I need a help on call by reference this is the program I wrote but the thing is it wont call the amount of kegs and diet coke at the end please help..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int answer,guests=0,kegs,dietCoke,pizzas;
    void invitedquests();
    void beverageorder(int);
    int foodorder(int);
    void allInfo(int,int,int,int);
    
    int main()
    {
        printf("Party Planing Assistant is pleased to be at your service!\n");
        
    
    
    if you try it you will see what happens you get;
    ??? guests
    0 kegs
    0 diet coke
    ?? pizzas
        do
        {
         printf("a. Enter number of invited quests.\n");
         printf("b. Determine beverage order.\n");
         printf("c. Determine food order.\n");
         printf("d. Display all information.\n");
         printf("e. Quit.\n");
         
         printf("Which do you want to choose?\n");
         scanf("%c",&answer);
         printf("\n\n");
         
       
         
         if(answer=='a')
         {
         invitedquests();
         }
         else if(answer=='b')
         {
         beverageorder(guests);
         }
         else if(answer=='c')
         {
         pizzas=foodorder(guests);
         }
         else if(answer=='d')
         {
         allInfo(guests,kegs,dietCoke,pizzas);
         }
         else if(answer=='e')
         {
         }
         else
         {
         printf("please select a valid choice\n");     
         }
         
         }while(answer!='e');
         
         
    
    system("pause");
    return 0;
    }
    
    void invitedquests()
    {
     int quests;
     
     printf("How many guests?\n");
     scanf("%d",&guests);
     
                  
    }
    
    void beverageorder(int guests)
    {
     int beerDrinker,kegs,dietCokeDrinkers,dietCoke;
     
     if(guests==0)
     {
       printf("You must enter choice a before the beverage order can be computed.\n");                
       system("pause");
       return 0;
     }
     
     else
     {
     beerDrinker=guests / 2;
     
                        if(beerDrinker %20 != 0)
                        {              
                        kegs=beerDrinker / 20 + 1;
                        }
                        else
                        {
                        kegs=beerDrinker / 20;
                        }
     
     dietCokeDrinkers=guests / 2;
     dietCoke=dietCokeDrinkers / 6;                  
     
     }    
    
    printf("%d\n",kegs);
    printf("%d\n\n\n",dietCoke);
    
    }
    
    int foodorder(int guests)
    {
         
         
         if(guests==0)
         {
         printf("You must enter choice a before the food order can be computed.\n");                
         system("pause");
         return 0;
         }
     
         else
         {
             if(guests %4 != 0)
             {              
             pizzas=guests / 4 + 1;
             }
             else
             {
             pizzas=guests / 4;
             }
         }
    return pizzas;
    }
    
    void allInfo(int guests,int kegs,int dietCoke,int pizzas)
    {
         printf("%d People\n%d Kegs of Beer\n%d Cases of Diet Coke\n%d Pizzas\n",guests,kegs,dietCoke,pizzas);
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Since this is C, not C++, there is no such thing as "pass by reference".

    You are probably thinking of using pointers, which would do something like this:
    Code:
    void func(int *x)
    {
       *x = 100;
    }
    
    int main()
    {
       int a = 0;
       func(&a);
       printf("a = %d\n", a); // All being well, this will output "a = 100"
       return 0;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. call by reference
    By Jackie in forum C Programming
    Replies: 3
    Last Post: 10-29-2001, 06:46 AM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM