Results 1 to 5 of 5

Thread: Pass by reference

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    6
    Thanks
    0
    Thanked
    0 times in 0 posts

    Pass by reference

    Hello all,

    yet again im trying to work out some c++, but this time I am doing pass by referencing. I am trying to allow each of my functions to pass down the edited reqchange as they each make modifications.

    The program compies, and the 50c function works, and so does the 20c function, but unfortunately the 10c function reverts back to the value stored by the 50c function and thenuses that instead of the edited 20c function. Hence this also will not pass onto the 5c function.

    If any1 could point me in the right direction or have any weblinks on pass by referencing that htey think could help would be great.

    Code:
    /*****************************************************************
     Author: Michael R
     Date: 8th April 2005
     Purpose: Change in Coins 
    *****************************************************************/
    
    #include <stdio.h>
    
    void userinput (int &reqchange)
    {
    
        printf("Welcome to the change calculator Program\n");
        printf("Enter how much change is needed to be distributed to the client...");
        scanf("%d", &reqchange);
        printf("\n");             
        return;
    }
    
    void fiftycents (int &reqchange)
    {
    
        if (reqchange >= 50)
        {
        reqchange = (reqchange - 50);
        printf("\n");
        printf("Required number of 50cent pieces = 1");
        }
        
          else
          {
              printf("\n");
              printf("Required number of 50cent pieces = 0");
          }        
    return;    
    }
    
    void twentycents (int &reqchange, int twenty_cents, int reqtwenty_cents)  
    {
            
        if (reqchange >= 20)    
        {
        reqtwenty_cents = reqchange;
        reqchange = ((reqtwenty_cents % 20) * 20);
        twenty_cents = (reqtwenty_cents / 20); 
        
        printf("\n");
        printf("Required number of 20cent pieces = %d", twenty_cents);
        }
         else
            {
              printf("\n");
              printf("Required number of 20cent pieces = 0");
            } 
               
    return;
    }
    
    void tencents (int &reqchange, int ten_cents, int reqten_cents)
    
    {
            
        if (reqchange >= 10)    
        {
        reqten_cents = reqchange;
        reqchange = ((reqten_cents % 10) * 10);
        ten_cents = (reqten_cents / 10); 
        printf("\n");
        printf("Required number of 10cent pieces = %d", ten_cents);
        }
         else
            {
              printf("\n");
              printf("Required number of 10cent pieces = 0");
            } 
               
    return;
    }
    
    void  fivecents (int &reqchange)
    
    {
          
       if (reqchange == 5)    
       {
        printf("\n");
        printf("Required number of  5cent pieces = 1 "); 
       }
       
          else
          {
          printf("\n"); 
          printf("Required number of  5cent pieces = 0");
          }
    return;
    }
    int main ()
    {
      int reqchange, twenty_cents, reqtwenty_cents, ten_cents, reqten_cents;
    
     userinput(reqchange);
     fiftycents(reqchange);
     twentycents(reqchange, twenty_cents, reqtwenty_cents);
     tencents(reqchange, ten_cents, reqten_cents);
     fivecents(reqchange);
    
    return(0);
    }
    Anyway any help/ reference/direction steering would be great.

    Cheers,

    Spitfire

  2. #2
    Flower Child stytagm's Avatar
    Join Date
    Aug 2004
    Location
    London
    Posts
    754
    Thanks
    47
    Thanked
    23 times in 18 posts
    Hi There,

    I'm not 100% sober and my C++ is rusty at best, But I've a hunch the problem might actually be with your 20c function. For starters your logic seems odd to me, why not use a loop a bit like:
    Code:
    while (reqchange >= 20)
    {
       TwentyCents++; // tally up the twenty cent coins by one.
       reqchange = reqchange - 20; // Knock 20 cents of the total to compensate
    }
    Then you would only need to use one variable, which would be less confusing. I think I'd also be carefull that when I declared my variables I initialised them to zero. ie:
    Code:
    int TwentyCents = 0;
    Why are all the following variables declared in main, and passed in when they are only used in the functions? Declare them in the functions.
    Code:
    twenty_cents, reqtwenty_cents, ten_cents, reqten_cents;
    Also, is it safe to assume in your 5c function that you'll only get 5 or 0 cents? What about 3cents, or 7? If you use logic like the while loop above it's a bit more robust.

    I Can't actually work out if there's anything wrong with your calling by reference, it looks fine to me. I would say however, that you don't need a return statement when you're not actually returning anything, you can just leave it out I think.

    So to summarise, I think your pass by reference is fine, but that the logic of your 20c function, while getting the right number of 20c coins, probably buggers up the remainder of reqchange. Try something like the following:
    Code:
    void twentycents (int &reqchange)
    {
       int twentyCentCoins = 0;
       while (reqchange >= 20)
       {
          reqchange = reqchange - 20;
          twentyCentCoins++;
       }
       printf("Required number of 20cent pieces = %d", twentyCentCoins);
    }
    Do something similar to this for the 10 and 5 cent functions as well. If you need the twentyCentCoins value later on in the main function, then declare it in main, and pass it in by value just like reqchange. If the variable's only needed in the function, declare it inside and leave it alone.

    Hope that helps and sorry for rambling. G'night.
    They told me I was gullible ... and I believed them.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    6
    Thanks
    0
    Thanked
    0 times in 0 posts
    yeah im trying to learn pass by referencing, im oging on a paragraph in our lecture notes at uni, tutorials arent 2 helpfull with this. I gave the addition a go, it was my first plan but didn't work to well. i will try and modify that.

    cheers for the input, its greately appreciated, hehe time to spend the rest of the arvo working on it

    -- Using the logic that 5c cents is the lowest coin denomiation in cash transactions

    --Think i will have to try and use my calculating functino because by using these ones heere, i dont know how to work the functions properly. the joys of uni explaining very little
    Last edited by spitty!; 10-04-2005 at 10:30 AM.

  4. #4
    Theoretical Element Spud1's Avatar
    Join Date
    Jul 2003
    Location
    North West
    Posts
    7,508
    Thanks
    336
    Thanked
    320 times in 255 posts
    • Spud1's system
      • Motherboard:
      • Gigabyte Aorus Master
      • CPU:
      • 9900k
      • Memory:
      • 16GB GSkill Trident Z
      • Storage:
      • Lots.
      • Graphics card(s):
      • RTX3090
      • PSU:
      • 750w
      • Case:
      • BeQuiet Dark Base Pro rev.2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Asus PG35VQ
      • Internet:
      • 910/100mb Fibre
    ummm that isnt C++ code, thats C code...

    Like stytagm says you could just declare your variables locally in this case, but if you did want to pass them through, pointers are the way to go - that would solve the problem.

    or actaully make that into C++ code by converting it to a class, making those variables private to that class and adding those methods to it

  5. #5
    Flower Child stytagm's Avatar
    Join Date
    Aug 2004
    Location
    London
    Posts
    754
    Thanks
    47
    Thanked
    23 times in 18 posts
    Quote Originally Posted by Spud1
    ummm that isnt C++ code, thats C code...
    I did notice that, I've got to confess I'd have been happier with those nasty "cout << string" statements (I've never properly used printf, isn't that what the cavemen used to write on the walls?).
    Code:
    cout << "Required number of 20cent pieces = " << twentyCentCoins << endl;
    Much better

    PS sorry for bumping this fairly quiet thread. I'm stuck at work and need to occupy my mind somehow.
    They told me I was gullible ... and I believed them.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 31-03-2005, 11:23 AM
  2. Crucial Ballistix - Quick and Dirty Test
    By M@tt in forum PC Hardware and Components
    Replies: 6
    Last Post: 20-07-2004, 04:57 PM
  3. Pass recovery...
    By retroborg in forum Software
    Replies: 3
    Last Post: 20-07-2004, 10:56 AM
  4. if i knew ( actually good pass on poem )
    By Wiffle_BMXer in forum General Discussion
    Replies: 8
    Last Post: 15-11-2003, 12:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •