Currency Conversion using OO

Discussion in 'C++' started by Willing 2 Learn, Oct 12, 2005.

  1. I'm still having trouble getting my program to do arithmetic in
    cents(keeping all #'s) then convert the answer in a format of dollars &
    cents. The main program should add, subtract, scalar multiply(by int)&
    show, have a constructor w/ & w/out arguments. Header file should have
    private data & all 6 functions from above.Class definition file should
    implement my ADT class.
    What I have so far:
    Main program
    #include "jahcurrency.h"
    #include <iostream.h>
    void main()
    {
    Currency a(4,3);
    Currency b(2,1);
    Currency total;

    total=a.add(b);
    total.show();
    total=a.sub(b);
    total.show();
    total=a.multiply(5);
    total.show();
    }
    Header file
    class Currency
    {
    private:
    double cents;
    public:
    Currency();
    Currency(int d,int c);
    Currency add(Currency b);
    Currency sub(Currency b);
    Currency multiply(double d);
    double calc(Currency s);
    double calc2(Currency f);
    void show(void);
    };
    Class definition
    #include "jahcurrency.h"
    #include <iomanip.h>
    #include <math.h>
    #include <iostream.h>

    char sign='$';


    Currency::Currency()
    {
    x=0;

    }
    Currency::calc2(Currency f)
    {

    f=(x*100)-(x%100);

    return f;
    }
    Currency::calc(Currency s)
    {

    s=(x%100);

    return s;

    }
    Currency::Currency(int d, int c)
    {

    d=(x*100)-(x%100);
    c=(x%100);

    return d;

    }


    Currency::add(Currency b)
    {
    Currency x;
    x.cents= x+ b.x;

    return x;
    }
    Currency::sub(Currency b)
    {
    Currency x;
    x.cents= x- b.x;

    return x;
    }
    Currency::multiply(Currency d)
    {
    Currency x;
    x.cents= x*d.x;

    return x;
    }


    Currency::show()
    {
    cout<<sign<<d<<". "<<s<<endl;
    }
    How different would the program be if cents is in long then convert
    ans. for each opeartion to dollars & cents ?
    Your help would be appreciated in how to fix this problem.
     
    Willing 2 Learn, Oct 12, 2005
    #1
    1. Advertising

  2. Willing 2 Learn

    osmium Guest

    "Willing 2 Learn" writes:

    > I'm still having trouble getting my program to do arithmetic in
    > cents(keeping all #'s) then convert the answer in a format of dollars &
    > cents. The main program should add, subtract, scalar multiply(by int)&
    > show, have a constructor w/ & w/out arguments. Header file should have
    > private data & all 6 functions from above.Class definition file should
    > implement my ADT class.
    > What I have so far:
    > Main program
    > #include "jahcurrency.h"
    > #include <iostream.h>
    > void main()
    > {
    > Currency a(4,3);
    > Currency b(2,1);
    > Currency total;
    >
    > total=a.add(b);
    > total.show();
    > total=a.sub(b);
    > total.show();
    > total=a.multiply(5);
    > total.show();
    > }
    > Header file
    > class Currency
    > {
    > private:
    > double cents;


    Fatal flaw. Even if you get it to work it will not be usable. People are
    very fussy about money and they want totals to add up to 100.0%. A double
    is only an *approximation* of a real number.

    Also, Currency is a bad name for the class. You have gone to great pains to
    handle coins. In common usage currency is paper money. How about "Money"
    as a name?

    <anip>
     
    osmium, Oct 12, 2005
    #2
    1. Advertising

  3. On 12 Oct 2005 08:01:29 -0700, "Willing 2 Learn"
    <> wrote:

    >I'm still having trouble getting my program to do arithmetic in
    >cents(keeping all #'s) then convert the answer in a format of dollars &
    >cents. The main program should add, subtract, scalar multiply(by int)&
    >show, have a constructor w/ & w/out arguments. Header file should have
    >private data & all 6 functions from above.Class definition file should
    >implement my ADT class.
    > What I have so far:
    >Main program
    >#include "jahcurrency.h"
    >#include <iostream.h>


    #include <iostream> /* WITHOUT the H */

    Including STL headers with .h ending is deprecated and can lead to
    many problems.

    >void main()


    int main()

    The C++ standard requires that main() return int.

    >{
    > Currency a(4,3);
    > Currency b(2,1);
    > Currency total;
    >
    > total=a.add(b);
    > total.show();
    > total=a.sub(b);
    > total.show();
    > total=a.multiply(5);
    > total.show();
    >}
    >Header file
    >class Currency
    >{
    > private:
    > double cents;
    > public:
    > Currency();
    > Currency(int d,int c);
    > Currency add(Currency b);

    Should be:
    Currency add(Currency b) const;
    Do you know why? You need to know...

    > Currency sub(Currency b);
    > Currency multiply(double d);
    > double calc(Currency s);
    > double calc2(Currency f);
    > void show(void);


    void show() const;

    >};
    >Class definition
    >#include "jahcurrency.h"
    >#include <iomanip.h>
    >#include <math.h>
    >#include <iostream.h>


    >#include <iomanip>
    >#include <iostream>


    You could include <cmath> but then the names would have to be in
    namespace std.

    >
    >char sign='$';
    >
    >
    >Currency::Currency()
    >{
    > x=0;


    Where is x declared? What about your member variable cents??

    >
    >}
    >Currency::calc2(Currency f)
    >{
    >
    > f=(x*100)-(x%100);
    >
    >return f;
    >}
    >Currency::calc(Currency s)
    >{
    >
    > s=(x%100);
    >
    >return s;
    >
    >}
    >Currency::Currency(int d, int c)
    >{
    >
    > d=(x*100)-(x%100);
    > c=(x%100);
    >
    >return d;


    ???
    You can't return anything from a constructor.
    And you throw away the second calculation, so what good is it?

    >}
    >
    >
    >Currency::add(Currency b)


    Should be:
    Currency Currency::add(Currency b) const
    >{
    > Currency x;
    > x.cents= x+ b.x;
    >
    > return x;
    >}
    >Currency::sub(Currency b)


    Same as above...

    >{
    > Currency x;
    > x.cents= x- b.x;
    >
    > return x;
    >}
    >Currency::multiply(Currency d)
    >{
    > Currency x;
    > x.cents= x*d.x;
    >
    > return x;
    >}
    >
    >
    >Currency::show()
    >{
    > cout<<sign<<d<<". "<<s<<endl;
    >}
    >How different would the program be if cents is in long then convert
    >ans. for each opeartion to dollars & cents ?
    >Your help would be appreciated in how to fix this problem.


    Did you try to compile this before posting?
    I didn't think so...

    --
    Bob Hairgrove
     
    Bob Hairgrove, Oct 12, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martyn Fewtrell

    String (Currency) to Decimal Conversion

    Martyn Fewtrell, Jun 21, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    4,185
    Martyn Fewtrell
    Jun 21, 2005
  2. Just starting out

    Currency conversion program

    Just starting out, Sep 5, 2005, in forum: C Programming
    Replies:
    14
    Views:
    820
    Gordon Burditt
    Sep 12, 2005
  3. Willing 2 Learn

    Currency Conversion using OO

    Willing 2 Learn, Oct 11, 2005, in forum: C++
    Replies:
    3
    Views:
    473
    Greg Comeau
    Oct 11, 2005
  4. Willing 2 Learn

    Currency Conversion using OO

    Willing 2 Learn, Oct 12, 2005, in forum: C++
    Replies:
    0
    Views:
    286
    Willing 2 Learn
    Oct 12, 2005
  5. powerranger

    currency conversion webservice

    powerranger, Jan 25, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    117
    Jan Tielens
    Jan 26, 2004
Loading...

Share This Page