ostream and inserter problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ggloadi
    New Member
    • Aug 2007
    • 2

    ostream and inserter problem

    [CODE=cpp]#include<iostre am>
    #include<iomani p>
    using namespace std;

    class Currency
    {
    public:
    Currency(double v = 0.0)
    {
    unit = v;
    cent = int((v - unit) * 100.0 + 0.5);
    }
    virtual void display(ostream & out) = 0;
    protected:
    unsigned int unit;
    unsigned int cent;
    };

    class USDollar : public Currency
    {
    public:
    USDollar(double v = 0.0) : Currency(v)
    {}
    virtual void display(ostream & out)
    {
    out << unit << ". " << setfill("0") << setw(2) << cent << setfill(" ") << " $";
    }
    };

    class DMark : public Currency
    {
    public:
    DMark(double v = 0.0) : Currency(v)
    {}
    virtual void display(ostream & out)
    {
    out << unit << ". " << setfill("0") << setw(2) << cent << setfill(" ") << " DM";
    }
    };

    ostream& operator<<(ostr eam& o, Currency& c)
    {
    c.display(o);
    return o;
    }

    void fn( Currency& c)
    {
    cout << c << "\n";
    }

    int main(int argcs, char* pArgs[])
    {
    USDollar usd(1.50);
    fn(usd);
    DMark d(3.00);
    fn(d);
    return 0;
    }[/CODE]

    I honestyl don't know what's wrong with this code. I'm using Microsoft Visual C++ 2005 Express Edition. It simply doesn't compile. Any help?
    Last edited by RedSon; Aug 22 '07, 02:40 PM. Reason: CODE TAGS!!!!!
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    In the future please read the thread creation and reply guidelines located immediately to the right of the box in which you enter text. Failure to do so will increase your chances of getting banned from this site.

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      What errors are you getting. Because when I ran it in VS2005 there were about 60 syntax errors.

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by ggloadi
        [CODE=cpp]
        class USDollar : public Currency
        {
        public:
        USDollar(double v = 0.0) : Currency(v)
        {}
        virtual void display(ostream & out)
        {
        out << unit << ". " << setfill("0") << setw(2) << cent << setfill(" ") << " $";
        }
        };

        class DMark : public Currency
        {
        public:
        DMark(double v = 0.0) : Currency(v)
        {}
        virtual void display(ostream & out)
        {
        out << unit << ". " << setfill("0") << setw(2) << cent << setfill(" ") << " DM";
        }
        };

        ostream& operator<<(ostr eam& o, Currency& c)
        {
        c.display(o);
        return o;
        }

        void fn( Currency& c)
        {
        cout << c << "\n";
        }

        int main(int argcs, char* pArgs[])
        {
        USDollar usd(1.50);
        fn(usd);
        DMark d(3.00);
        fn(d);
        return 0;
        }[/CODE]

        I honestyl don't know what's wrong with this code. I'm using Microsoft Visual C++ 2005 Express Edition. It simply doesn't compile. Any help?
        I got it working by removing the setfill() calls in the display function statement.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Be aware that floating point should not be used for finance applications. The automatic rounding, which cannot be shut off, will cause books tro not balance.

          There are laws in Europe against using flosting point for fincance and I know of one company in the USA that was sued out of business by a large client.They used floating point.

          Programmers seem to think that's the thing to do because you have a decimal point. However, that's just a visual presentation and you can do the same thing with an integer.

          That is, 123 can display a $1.23 by using a display function.

          I recommend you create a class for your finance quantities and use member functions of that class to add, subtract,displa y, etc, your amounts.

          Start with a long int. Later is that's too small you can change the implementation to use a string.

          Comment

          • ggloadi
            New Member
            • Aug 2007
            • 2

            #6
            Originally posted by RedSon
            What errors are you getting. Because when I ran it in VS2005 there were about 60 syntax errors.
            I received 2 errors of this kind( + 1 warning ):
            Code:
            error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::_Fillobj<_Elem>' (or there is no acceptable conversion)
            NOT 60( probably Visual Studio is more restrictive)

            Originally posted by ilikepython
            I got it working by removing the setfill() calls in the display function statement.
            Your hint helped me much. I focused on setfill() and all I discovered was a subtle
            syntax error: I used setfill( char* ) - which is not defined, instead of setfill( char )

            All I did was to replace setfill( "0" ) with setfill( '0' )
            and setfill( " " ) with setfill( ' ' ) and it worked well

            PS: I like this forum very much. I never had such prompt replies.

            Comment

            Working...