adding a function to a program

This is a discussion on adding a function to a program within the C++ Programming forums, part of the General Programming Boards category; I have this program that takes an amount of change and then tells how many of each type of coin ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    14

    adding a function to a program

    I have this program that takes an amount of change and then tells how many of each type of coin you will receive back. I want to change this program so that a function will handle all of the calculations. What I want to know is if I'm even on the right track as far as doing this? I don't want to continue changing things if I'm doing it wrong...



    Code:
    // *********************************************************** 
    // Program Make Change: Given any amount of change expressed 
    // in cents, this program computes the number of half-dollars, 
    // quarters, dimes, nickels, and pennies to be returned, 
    // returning as many half-dollars as possible, then quarters, 
    // dimes, nickels, and pennies in that order. 
    //************************************************************ 
           //header file 
    #include <iostream> 
    using namespace std; 
           //named constants 
    
    void convertChange(int tempchange, int& nQuarter, int& nDime, int& nNickel, int& nonedollar, 
    int& nfivedollar, int& ntendollar, int& ntwentydollar); 
    
    const int Halfdollar = 50; 
    const int Quarter = 25; 
    const int Dime = 10; 
    const int Nickel = 5; 
    const int onedollar = 100; 
    const int fivedollar = 500; 
    const int tendollar = 1000; 
    const int twentydollar = 2000; 
    int main() 
    { 
           //declare variable 
       int change; 
       double changeone; 
           //Statements: Step 1 - Step 12 
       cout << "Enter change in decimal value: ";                //Ask user for change in decimal value 
       cin >> changeone;                                    //Input change 
       cout << endl; 
       cout << "The change you entered is " << changeone 
            << endl;                                     //Output entered change 
    change = changeone * 100; 
       cout << "The number of 20 dollars to be returned is "; 
          //<< change / twentydollar << endl; 
       //change = change % twentydollar; 
    
       cout << "The number of 10 dollars to be returned is "; 
         //<< change / tendollar << endl; 
      //change = change % tendollar; 
       cout << "The number of 5 dollars to be returned is "; 
         //<< change / fivedollar << endl; 
       //change = change % fivedollar; 
       cout << "The number of 1 dollars to be returned is "; 
         //<< change / onedollar << endl; 
       //change = change % onedollar; 
       //cout << "The number of half-dollars to be returned " 
            //<< "is " << change / Halfdollar 
            //<< endl;                                     //Perform integer division to find number of half-dollars to be returned. 
       //change = change % Halfdollar;                     //Remove change from larger value. 
       cout << "The number of quarters to be returned is "; 
            //<< change / Quarter << endl;                 //Perform integer division to find number of quarters to be returned. 
       //change = change % Quarter;                        //Remove change from larger value. 
       cout << "The number of dimes to be returned is "; 
            //<< change / Dime << endl;                    //Perform integer division to find number of dimes to be returned. 
       //change = change % Dime;                           //Remove change from larger value. 
       cout << "The number of nickels to be returned is "; 
            //<< change / Nickel << endl;                  //Perform integer division to find number of nickels to be returned. 
       //change = change % Nickel;                         //Remove change from larger value. 
       cout << "The number of pennies to be returned is "; 
            //<< change << endl;                           //Use remaining change to find number of pennies. 
       return 0; 
    } 
      
    void convertchange(int change, int nQuarter, int nDime, int nNickel, int nonedollar, 
    int nfivedollar, int ntendollar, int ntwentydollar) 
    { 
    const int Quarter = 25; 
    const int Dime = 10; 
    const int Nickel = 5; 
    const int onedollar = 100; 
    const int fivedollar = 500; 
    const int tendollar = 1000; 
    const int twentydollar = 2000; 
    
    nDime = change / Dime; 
    change change % Dime; 
    
    nQuarter = change / Quarter; 
    change = change % Quarter; 
    
    nNickel = change / Nickel; 
    change = change % Nickel; 
    
    }

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Yes, you are on the right track. You made the function arguments references in your prototype of convertChange at the top of the file. Don't forget to do the same at the bottom.

    One important thing you must do is call the function. Inside main, add some variables that are local to main to hold the results. Then, call the function and pass those variables as parameters, and finally output the values in those variables.

    Just keep going, you'll get it.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    just about got it. The return type and function name must be the same in the definition as in the declaration. The parameter list must have the same type in the same order in both the declaration and the definition. The declaration has a semicolon after the parameter list, whereas the definition has a function body after the parameter. The definition must have the parameter names in addition to the type, whereas the declaration only needs the type, and may or may not include the parameter names.


    In your example, you have a number of references in the parameter list, but in the definition you have none. Therefore the parameter types aren't the same in the declaration and the definition. You have to decide where you are doing the output. If you are doing it in the called function, I'd just pass the amount of change as a single parameter. If you are doing it in main I'd declare variables in main() to send to the called function and output the values in main() after the function call.
    Code:
    Version 1
    void convertChange(int change)
    {
       //put your code from main in here
    }
     
    Version 2
    int main()
    {
      int ndime = 0;
      int nnickels = 0;
      int nquarters = 0;
      //etc.
      
      //get user input
     
      //function call
      convertChange(change, ndime, nnickels, nquarters);
      
      //output here
      cout << ndime << endl;
      cout << nnickels << endl;
      cout << nquarters << endl;
     }
     
    void convertChange(int change, int & ndime, int & nnickels, int & nquarters)
    {
       //do calculations here
    }

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    14
    thanks for the help!

    now i don't have any errors after building and compiling, however once i execute the program it won't run properly. any ideas why?

    here's my updated code:
    Code:
     
    
    // *********************************************************** 
    // Program Make Change: Given any amount of change expressed 
    // in cents, this program computes the number of half-dollars, 
    // quarters, dimes, nickels, and pennies to be returned, 
    // returning as many half-dollars as possible, then quarters, 
    // dimes, nickels, and pennies in that order. 
    //************************************************************ 
           //header file 
    #include <iostream> 
    using namespace std; 
           //named constants 
    
    void convertChange(int change, int& nquarter, int& ndime, int& nnickel, int& nonedollar, 
    int& nfivedollar, int& ntendollar, int& ntwentydollar); 
    
    const int Halfdollar = 50; 
    const int Quarter = 25; 
    const int Dime = 10; 
    const int Nickel = 5; 
    const int onedollar = 100; 
    const int fivedollar = 500; 
    const int tendollar = 1000; 
    const int twentydollar = 2000; 
    int main() 
    { 
    
    int ndime = 0;
    int nnickel = 0;
    int nquarter = 0;
    int nonedollar = 0;
    int nfivedollar = 0;
    int ntendollar = 0;
    int ntwentydollar = 0;
    
    
           //declare variable 
       int change; 
       double changeone; 
           //Statements: Step 1 - Step 12 
       cout << "Enter change in decimal value: ";                //Ask user for change in decimal value 
       cin >> changeone;                                    //Input change 
       cout << endl; 
       cout << "The change you entered is " << changeone 
            << endl;                                     //Output entered change 
    change = changeone * 100; 
    
    convertChange(change, ndime, nnickel, nquarter, nonedollar, nfivedollar, ntendollar, ntwentydollar);
       //cout << "The number of 20 dollars to be returned is "; 
          //<< change / twentydollar << endl; 
       //change = change % twentydollar; 
    
       //cout << "The number of 10 dollars to be returned is "; 
         //<< change / tendollar << endl; 
      //change = change % tendollar; 
       //cout << "The number of 5 dollars to be returned is "; 
         //<< change / fivedollar << endl; 
       //change = change % fivedollar; 
       //cout << "The number of 1 dollars to be returned is "; 
         //<< change / onedollar << endl; 
       //change = change % onedollar; 
       //cout << "The number of half-dollars to be returned " 
            //<< "is " << change / Halfdollar 
            //<< endl;                                     //Perform integer division to find number of half-dollars to be returned. 
       //change = change % Halfdollar;                     //Remove change from larger value. 
       //cout << "The number of quarters to be returned is "; 
            //<< change / Quarter << endl;                 //Perform integer division to find number of quarters to be returned. 
       //change = change % Quarter;                        //Remove change from larger value. 
       //cout << "The number of dimes to be returned is "; 
            //<< change / Dime << endl;                    //Perform integer division to find number of dimes to be returned. 
       //change = change % Dime;                           //Remove change from larger value. 
       //cout << "The number of nickels to be returned is "; 
            //<< change / Nickel << endl;                  //Perform integer division to find number of nickels to be returned. 
       //change = change % Nickel;                         //Remove change from larger value. 
       //cout << "The number of pennies to be returned is "; 
            //<< change << endl;                           //Use remaining change to find number of pennies. 
       
      //output statements
    
     cout << "The number of nickels to be returned is " << nnickel << endl;
     cout << "The number of dimes to be returned is " << ndime << endl;
     cout << "The number of quarters to be returned is " << nquarter << endl;
     cout << "The number of 1 dollars to be returned is " << nonedollar << endl;
     cout << "The number of 5 dollars to be returned is " << nfivedollar << endl;
     cout << "The number of 10 dollars to be returned is " << ntendollar << endl;
     cout << "The number of 20 dollars to be returned is " << ntwentydollar << endl;
    
    
    
    return 0; 
    } 
      
    void convertChange(int change, int& nquarter, int& ndime, int& nnickel, int& nonedollar, 
    int& nfivedollar, int& ntendollar, int& ntwentydollar) 
    { 
    const int Quarter = 25; 
    const int Dime = 10; 
    const int Nickel = 5; 
    const int onedollar = 100; 
    const int fivedollar = 500; 
    const int tendollar = 1000; 
    const int twentydollar = 2000; 
    
    ndime = change / Dime; 
    change = change % Dime; 
    
    nquarter = change / Quarter; 
    change = change % Quarter; 
    
    nnickel = change / Nickel; 
    change = change % Nickel; 
    
    nonedollar = change / onedollar;
    change = change % onedollar;
    
    nfivedollar = change % nfivedollar;
    change = change % nfivedollar;
    
    ntendollar = change % ntendollar;
    change = change % ntendollar;
    
    ntwentydollar = change % ntwentydollar;
    change = change % ntwentydollar;
    
    
    
    }

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>however once i execute the program it won't run properly. any ideas why?

    Yes You have to find change starting from the largest currency to the smallest. Also, the fivedollar/tendollar/twentydollar should be the same as the others, except reorganized.

    Code:
    ntwentydollar = change / twentydollar;
    change = change % twentydollar;
     
    ntendollar = change / tendollar;
    change = change % tendollar;
     
    nfivedollar = change / fivedollar;
    change = change % fivedollar;
     
    nonedollar = change / onedollar;
    change = change % onedollar;
     
    nquarter = change / Quarter; 
    change = change % Quarter; 
     
    ndime = change / Dime; 
    change = change % Dime; 
     
    nnickel = change / Nickel; 
    change = change % Nickel;
    If there's other errors, you need to tell us what the problems are... we can't magically figure out what's appearing on your screen that's wrong

    **EDIT**
    Also, I don't know why you made the change-finding different for the dollar bills. It should be the same as for the coins...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    14
    cool, the program works now!

    BUT.... it's giving me incorrect answers.... for example, if i input .05 cents it tells me i get 1 quarter back when i should get 1 nickel back. that means i've got a logic error somewhere in my calculations right?

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>that means i've got a logic error somewhere in my calculations right?
    That would be a good guess

    Have you carefully compared your code with the changes I made? If you already fixed that, try copying and pasting (but make a backup first) from the code I posted, just in case you missed something small. Otherwise, let us know and I'll take a look again.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    14
    still not working... whenever i enter .05, .10, or .25 cents it doesn't work. it will work for pennies and also all the dollars.

    Code:
    // *********************************************************** 
    // Program Make Change: Given any amount of change expressed 
    // in cents, this program computes the number of half-dollars, 
    // quarters, dimes, nickels, and pennies to be returned, 
    // returning as many half-dollars as possible, then quarters, 
    // dimes, nickels, and pennies in that order. 
    //************************************************************ 
           //header file 
    #include <iostream> 
    using namespace std; 
           //named constants 
    
    void convertChange(int change, int& nquarter, int& ndime, int& nnickel, int& nonedollar, 
    int& nfivedollar, int& ntendollar, int& ntwentydollar, int& npennies); 
    
    
    int main() 
    { 
    
    int nnickel = 0;
    int ndime = 0;
    int nquarter = 0;
    int nonedollar = 0;
    int nfivedollar = 0;
    int ntendollar = 0;
    int ntwentydollar = 0;
    int npennies = 0;
    
           //declare variable 
       int change; 
       double changeone; 
           //Statements: Step 1 - Step 12 
       cout << "Enter change in decimal value: ";                //Ask user for change in decimal value 
       cin >> changeone;                                    //Input change 
       cout << endl; 
       cout << "The change you entered is " << changeone 
            << endl;                                     //Output entered change 
    change = changeone * 100; 
    
    convertChange(change, nnickel, ndime, nquarter, nonedollar, nfivedollar, ntendollar, ntwentydollar, npennies);
       //cout << "The number of 20 dollars to be returned is "; 
          //<< change / twentydollar << endl; 
       //change = change % twentydollar; 
    
       //cout << "The number of 10 dollars to be returned is "; 
         //<< change / tendollar << endl; 
      //change = change % tendollar; 
       //cout << "The number of 5 dollars to be returned is "; 
         //<< change / fivedollar << endl; 
       //change = change % fivedollar; 
       //cout << "The number of 1 dollars to be returned is "; 
         //<< change / onedollar << endl; 
       //change = change % onedollar; 
       //cout << "The number of half-dollars to be returned " 
            //<< "is " << change / Halfdollar 
            //<< endl;                                     //Perform integer division to find number of half-dollars to be returned. 
       //change = change % Halfdollar;                     //Remove change from larger value. 
       //cout << "The number of quarters to be returned is "; 
            //<< change / Quarter << endl;                 //Perform integer division to find number of quarters to be returned. 
       //change = change % Quarter;                        //Remove change from larger value. 
       //cout << "The number of dimes to be returned is "; 
            //<< change / Dime << endl;                    //Perform integer division to find number of dimes to be returned. 
       //change = change % Dime;                           //Remove change from larger value. 
       //cout << "The number of nickels to be returned is "; 
            //<< change / Nickel << endl;                  //Perform integer division to find number of nickels to be returned. 
       //change = change % Nickel;                         //Remove change from larger value. 
       //cout << "The number of pennies to be returned is "; 
            //<< change << endl;                           //Use remaining change to find number of pennies. 
       
      //output statements
    
    
    cout << "The number of pennies to be returned is " << npennies << endl;
    cout << "The number of nickels to be returned is " << nnickel << endl;
    cout << "The number of dimes to be returned is " << ndime << endl;
    cout << "The number of quarters to be returned is " << nquarter << endl;
    cout << "The number of 1 dollars to be returned is " << nonedollar << endl;
    cout << "The number of 5 dollars to be returned is " << nfivedollar << endl;
    cout << "The number of 10 dollars to be returned is " << ntendollar << endl;
    cout << "The number of 20 dollars to be returned is " << ntwentydollar << endl;
    
    
    
    return 0; 
    } 
      
    void convertChange(int change, int& nquarter, int& ndime, int& nnickel, int& nonedollar, 
    int& nfivedollar, int& ntendollar, int& ntwentydollar, int& npennies) 
    { 
     
    const int Nickel = 5; 
    const int Dime = 10; 
    const int Quarter = 25;
    const int onedollar = 100; 
    const int fivedollar = 500; 
    const int tendollar = 1000; 
    const int twentydollar = 2000; 
    
    ntwentydollar = change / twentydollar;
    change = change % twentydollar;
     
    ntendollar = change / tendollar;
    change = change % tendollar;
     
    nfivedollar = change / fivedollar;
    change = change % fivedollar;
     
    nonedollar = change / onedollar;
    change = change % onedollar;
     
    nquarter = change / Quarter; 
    change = change % Quarter; 
     
    ndime = change / Dime; 
    change = change % Dime; 
     
    nnickel = change / Nickel; 
    change = change % Nickel; 
    
    
    npennies = change;
    
    
    }

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    The way function parameters are passed is based on the order, not the names. So when you called the function in main you did this:
    Code:
    convertChange(change, nnickel, ndime, nquarter, nonedollar, nfivedollar, ntendollar, ntwentydollar, npennies);
    but the function itself was expecting this:
    Code:
    convertChange(int change, int& nquarter, int& ndime, int& nnickel, int& nonedollar, int& nfivedollar, int& ntendollar, int& ntwentydollar, int& npennies)
    Notice how the function expects the quarter variable first, but you pass it the nickel variables. Make sure the variables match up and are in the correct order.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    14
    problem solved. thanks everyone for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  3. Replies: 2
    Last Post: 05-10-2002, 05:16 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21