Thread: Initializing integers

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    4

    Initializing integers

    I am working on a homework assignment and have most of the program working, but when I try to compile it keeps telling me to initialize the coin variables in each class. However, they are supposed to be added then removed so I don't want to set them back to zero. Any help would be appreciated!

    Rewrite the Purse program given in Page 35 with functions to perform insert and remove operations. The function insert (int p, int n, int d, int q) will initialize pennies, nickels, dimes and quarters. The function dollars() will return the dollars. The function remove (int p, int n, int d, int q) will subtract pennies, nickels, dimes and quarters. The function display() returns a new String to print the content of the purse with remaining pennies, nickels, dimes and quarters.

    Code:
    usingnamespace std;
    
    
    int insert_money (int *p, int *n, int *d, int *q);
    int remove_money (int *p, int *n, int *d, int *q);
    int dollars();
    
    
    int main()
    {
        int pennies, nickels, dimes, quarters;
        int p, n, d, q;
       cout << "\nTime to insert some money";
       cout << "\nNumber of pennies to insert: ";
        cin >> p;
       cout << "\nNumber of nickels to insert: ";
        cin >> n;
       cout << "\nNumber of dimes to insert: ";
        cin >> n;
       cout << "\nNumber of quarters to insert: ";
        cin >> q;
    
        cout << quarters << "quarters " << dimes << "dimes" << nickels << "nickels" << pennies << "pennies" <<     insert_money(int *p, int *n, int *d, int *q;
    
       cout << "\nTime to insert some money";
       cout << "\nNumber of pennies to insert: ";
        cin >> p;
       cout << "\nNumber of nickels to insert: ";
        cin >> n;
       cout << "\nNumber of dimes to insert: ";
        cin >> n;
       cout << "\nNumber of quarters to insert: ";
        cin >> q;
    
        cout << quarters << "quarters " << dimes << "dimes" << nickels << "nickels" << pennies << "pennies" << insert_money(int *p, int *n, int *d, int *q;
                }
    
    
    
    
    int insert_money(int *p, int *n, int *d, int *q)
    {
        int pennies = 0, nickels = 0, dimes = 0, quarters = 0;
        pennies += *p;
        nickels += *n;
        dimes += *d;
        quarters += *q;
    }
    
    
    int remove_money(int *p, int *n, int *d, int *q)
    {
        int pennies, nickels, dimes, quarters;
        pennies -= *p;
        nickels -= *n;
        dimes -= *d;
        quarters -= *q;
    }
    
    
    int dollars(int pennies, int nickels, int dimes, int quarters, double total)
    {
        total = pennies + 5*nickels + 10*dimes + 25*quarters;
        total = (float)total/100.0;
    
        return total;
    }
    
    Last edited by Sambo2992; 09-26-2014 at 08:36 PM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    A number of things are wrong here.
    1) <#int *p#> and similar need to be replaced with actual C++ code.
    2) The way you have it, the functions don't share a wallet. If you want to share data, the easiest way is to pass that data arguments. Your local variables, even though they have the same name, don't actually refer to the same thing. In this case a better solution would be to use class for which insert and remove are members, but I realize you probably don't know how to do that yet.
    3) You don't show the existing purse program example, but it looks like you misunderstood what insert and remove are supposed to do. The intent seems to be that reading from cin and writing from cout would go in those functions.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Jul 2014
    Location
    Central Arizona
    Posts
    61
    A couple of hints. I don't see any #include <statement> at the top. You have a typo in the first line. You have a lot of typos using "" marks in the wrong place. Why didn't you use a newline for insert_money and more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-26-2012, 10:50 AM
  2. Initializing an arrays of integers
    By caduardo21 in forum C++ Programming
    Replies: 2
    Last Post: 03-09-2005, 12:43 PM
  3. initializing hex?
    By keithmolo in forum C++ Programming
    Replies: 4
    Last Post: 08-13-2003, 08:34 AM
  4. Initializing
    By Registered in forum C++ Programming
    Replies: 3
    Last Post: 07-19-2002, 01:57 PM
  5. initializing
    By srinurocks in forum C Programming
    Replies: 1
    Last Post: 06-18-2002, 06:11 AM