Storing Data in a Structure

This is a discussion on Storing Data in a Structure within the C++ Programming forums, part of the General Programming Boards category; Please take a look at my code. I've done "phase 1" - now I'm on "phase 2" I'm stuck on ...

  1. #1
    Unregistered
    Guest

    Storing Data in a Structure

    Please take a look at my code. I've done "phase 1" - now I'm on "phase 2"

    I'm stuck on the last two parts

    Here are the instructions

    Phase Two::
    Enlarge the program to have three MONEY variables. Prompt the user for the various change for two of the MONEY variables; then add the member data items of the two MONEY objects together, using the dot operator to access the member variables and storing the result in the third variable and all three Money variables should be displayed in a dollars and cents format $ddd.cc and display a count of the number of each type of coin now stored in the third money variable.

    Here is what I have for the code. Also If you have any other suggestion -->I'd like to have feedback.


    #include <stdio.h>
    #include <iostream.h>

    struct money
    {
    int Quarters;
    int Dimes;
    int Nickles;
    int Penny;
    };

    /***********************************Prototype Declarations************************************** **********/
    int caclulate(money getsQuartersDimes, money getsNicklesPennies, int total, money storeFinal);
    /************************************************** ************************************************** *****/

    int main ()
    {

    int total=0;
    money getsQuarterDimes;
    money getsNicklesPennies;
    money storeFinal;


    cout << "Enter the number of Quarters ";
    cin >> getsQuarterDimes.Quarters;
    cout << "Enter the number of Dimes ";
    cin >> getsQuarterDimes.Dimes;
    cout << "Enter the number of Nickles ";
    cin >> getsNicklesPennies.Nickles;
    cout << "Enter the number of Pennies ";
    cin >> getsNicklesPennies.Penny;

    total = caclulate(getsQuarterDimes, getsNicklesPennies, total, storeFinal);

    cout << "You have " << total << " cents" << endl;

    return 0;
    }

    int caclulate(money getsQuartersDimes, money getsNicklesPennies, int total, money storeFinal)
    {
    storeFinal.Quarters = getsQuartersDimes.Quarters* 25 ;
    storeFinal.Dimes = getsQuartersDimes.Dimes * 10;
    storeFinal.Nickles = getsNicklesPennies.Nickles * 5;
    storeFinal.Penny = getsNicklesPennies.Penny * 1;


    total = storeFinal.Quarters + storeFinal.Dimes + storeFinal.Nickles + storeFinal.Penny ;


    return total;
    }

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    After you've obtained input for two money variables pass them into a function like -

    money add(const money& one, const money& two)
    {
    money three;
    three.Quarters = one.Quarters+two.Quarters;
    three.Dimes=one.Dimes+two.Dimes;
    //etc

    return three;
    }

    In your main function you'd call it like

    money result = add(first,second);

    where first and second are your money variables that have been inputted.

    You can modify your calculate function (or add a similar display function) to create the value in ddd.cc and calculating the amount of coins.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data structure for french-english dictionary
    By officedog in forum C Programming
    Replies: 9
    Last Post: 03-20-2009, 02:43 AM
  2. Storing in structure
    By bennyboy in forum C Programming
    Replies: 1
    Last Post: 02-26-2005, 06:14 AM
  3. Data structure implementation
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 07-31-2003, 08:44 AM
  4. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM
  5. Dynamic Data Structure -- Which one is better?
    By Yin in forum C++ Programming
    Replies: 0
    Last Post: 04-11-2002, 12:38 AM

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