Thread: C assignment- ATM and reference parameters

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    5

    C assignment- ATM and reference parameters

    I'm back. I'd like to thank everyone for the help on my last program.

    So for this one, I need write a C program for an automatic teller machine that dispenses money. The user should enter the amount desired (a multiple of 10 dollars) and the machine dispenses this amount using the least number of bills. The bills dispensed are 50s, 20s, and 10s. Write a function that determines how many of each kind of bill to dispense.

    I've sent in like 5 programs to by teacher already, but he has rejected them because they don't use reference parameters.

    My friend send me a code he tried to work on, but it has some issues. Wont compile.

    I looked it over, but I can't figure out whats wrong with the damn thing. (Further proving that i suck at programming.)

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int getbills(int dollars, int* fifties, int* twenties, int* tens)
    
    {
    *fifties = dollars / 50;
    *twenties = (dollars %= 50) / 20;
    *tens = (dollars %= 20) / 10;
    
    return *fifties || *twenties || *tens;
    }
    
    void printBills(int dollars, int fifties, int twenties, int tens)
    {
    printf("\nDispensing %d dollars ...", dollars);
    if (fifties);
    printf("\n%d $50 bills", fifties);
    if (twenties);
    printf("\n%d $20 bills", twenties);
    if (tens);
    printf("\n%d $10 bills", tens);
    getch();
    }
    The Error messages I get are
    [Linker error] undefined reference to `WinMain@16'

    and

    ld returned 1 exit status

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    C doesn't have "reference parameters". The closes you get to references are pointers. Have you worked with pointers before?
    Code:
    #include<stdio.h>
    void changewhatispassed( int *x )
    {
        if( x )
            (*x)++;
    }
    
    int main( void )
    {
        int changeme = 5;
    
        changewhatispassed( &changeme );
        printf( "changeme is %d\n", changeme );
    
        return 0;
    }
    Pass the address of the variable to the function, and with the address, via pointer, you can change the value of the variable whose address you have.

    Your compiler error is actually because you don't have a main function though.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    5
    Ok. Before I keep on with this, I figure I should show the other program I had.

    This one works and I thought did what he wanted it to do, but I guess not.

    Code:
    #include "stdio.h"
    #include "conio.h"
    
    int value,rem,bill_50,bill_20,bill_10;
    void dispenser();
    
    int main(void)
    {
    printf("**Welcome**\n\n");
    dispenser();
    getch();
    }
    void dispenser()
    {
    int a,b;
    printf ("Enter the amount you wish to withdrawl\n");
    scanf("%d",&value);
    
      if(((value%10)==0) && (value>0))
    {
        //Calculates 50s                        
    	if(value>=50)
    	{
    	bill_50 = value/50;
    	value = value%50;
    	printf ("No of 50 Bills is %d \n",bill_50);
    	}
        //Calculates 20s
    	if((value>=20) && (value<50))
    	{
    	bill_20 = value/20;
    	value = value%20;
    	printf ("No of 20 Bills is %d \n",bill_20);
    	}
        //Calculates 10s
    	if(value==10)
    	{
    	bill_10 = 1;
    	value = value%10;
    	printf ("No of 10 Bills is %d \n",bill_10);
    	}
        printf("Press 1 for a new transaction and 0 to exit\n");
        scanf("%d",&a);
            
        if(a == 1)
          {
              dispenser();
          }
        if(a == 0)
         {
           printf("Thank you!!");
           exit(0);
         }
      
    
    }
         /*Safety for increments of 10*/
         else
         {
          printf ("Please Enter in Multiples 10 only, thank you \n\n");
          dispenser();
         }
    
    
    
    }
    I thought it was fine, but this is what he told me.

    "does not meet problem specification to compute number of bills in a function, returning all three to main as reference parameters"

    That's why I brought up the reference parameters. I thought the program was fine, but there must be something I'm missing that he wants.

    And yes, he is a crazy ridiculous bastard.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    He wants you to use pointers, and to pass the variables to the function instead of just using global variables. I've shown you how to pass a variable's address to a function, so you should be able to figure out how to pass 2 or 3 variables in the same manner.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    5
    Alright, thanks

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Quote Originally Posted by TigerTank77 View Post
    Ok. Before I keep on with this, I figure I should show the other program I had.

    Code:
    void dispenser(int *);
    int main(void)
    {
              int value;
             printf("**Welcome**\n\n");
            printf ("Enter the amount you wish to withdrawl\n");
            scanf("%d",&value);
            dispenser(&value);
            getch();
    }
    void dispenser(int *value)
    {
    int a,b;
    int rem,bill_50,bill_20,bill_10;
    
    while(value)
    {
        if(((value%10)==0) && (value>0))
        {
                    Denomination calculation logic
        } 
    }      
     
    
    }
    Probably this is what he meant.

Popular pages Recent additions subscribe to a feed