473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

ostream and inserter problem

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4.  
  5. class Currency
  6. {
  7. public:
  8.     Currency(double v = 0.0)
  9.     {
  10.         unit = v;
  11.         cent = int((v - unit) * 100.0 + 0.5);
  12.     }
  13.     virtual void display(ostream& out) = 0;
  14. protected:
  15.     unsigned int unit;
  16.     unsigned int cent;
  17. };
  18.  
  19. class USDollar : public Currency
  20. {
  21. public:
  22.     USDollar(double v = 0.0) : Currency(v)
  23.     {}
  24.     virtual void display(ostream& out)
  25.     {
  26.         out << unit << ". " << setfill("0") << setw(2) << cent << setfill(" ") << " $";
  27.     }
  28. };
  29.  
  30. class DMark : public Currency
  31. {
  32. public:
  33.     DMark(double v = 0.0) : Currency(v)
  34.     {}
  35.     virtual void display(ostream& out)
  36.     {
  37.         out << unit << ". " << setfill("0") << setw(2) << cent << setfill(" ") << " DM";
  38.     }
  39. };
  40.  
  41. ostream& operator<<(ostream& o, Currency& c)
  42. {
  43.     c.display(o);
  44.     return o;
  45. }
  46.  
  47. void fn( Currency& c)
  48. {
  49.     cout << c << "\n";
  50. }
  51.  
  52. int main(int argcs, char* pArgs[])
  53. {
  54.     USDollar usd(1.50);
  55.     fn(usd);
  56.     DMark d(3.00);
  57.     fn(d);
  58.     return 0;
  59. }
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?
Aug 22 '07 #1
5 2944
RedSon
5,000 Expert 4TB
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.
Aug 22 '07 #2
RedSon
5,000 Expert 4TB
What errors are you getting. Because when I ran it in VS2005 there were about 60 syntax errors.
Aug 22 '07 #3
ilikepython
844 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. class USDollar : public Currency
  2. {
  3. public:
  4.     USDollar(double v = 0.0) : Currency(v)
  5.     {}
  6.     virtual void display(ostream& out)
  7.     {
  8.         out << unit << ". " << setfill("0") << setw(2) << cent << setfill(" ") << " $";
  9.     }
  10. };
  11.  
  12. class DMark : public Currency
  13. {
  14. public:
  15.     DMark(double v = 0.0) : Currency(v)
  16.     {}
  17.     virtual void display(ostream& out)
  18.     {
  19.         out << unit << ". " << setfill("0") << setw(2) << cent << setfill(" ") << " DM";
  20.     }
  21. };
  22.  
  23. ostream& operator<<(ostream& o, Currency& c)
  24. {
  25.     c.display(o);
  26.     return o;
  27. }
  28.  
  29. void fn( Currency& c)
  30. {
  31.     cout << c << "\n";
  32. }
  33.  
  34. int main(int argcs, char* pArgs[])
  35. {
  36.     USDollar usd(1.50);
  37.     fn(usd);
  38.     DMark d(3.00);
  39.     fn(d);
  40.     return 0;
  41. }
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.
Aug 22 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
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,display, etc, your amounts.

Start with a long int. Later is that's too small you can change the implementation to use a string.
Aug 22 '07 #5
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 ):
Expand|Select|Wrap|Line Numbers
  1. error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::_Fillobj<_Elem>' (or there is no acceptable conversion)
  2.  
NOT 60( probably Visual Studio is more restrictive)

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.
Aug 23 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: nimrod | last post by:
When running inserter and selector on the same connection (using binding expressions) on the second iteration the insertion/selection fails, receiving failure message: CT-Lib problem: sever=1...
0
by: Ryan M. Keith | last post by:
I am having a problem with the ostream operator in templated classes that I wrote (I'm using the Borland compiler), and I'm certain that the templates are the problem because when I remove the...
2
by: keit6736 | last post by:
Hi, I'm using the Borland compiler and I've created two templated classes in which I've overloaded the ostream << operator. However, when I try and use the operator on objects of either class I...
13
by: Peteroid | last post by:
These don't work (I'm using VS C++.NET 2005 Express with clr:/pure syntax): ostream& operator <<( ostream& output, String^ str ) { output << str ; //compile error return output ; } ...
5
by: truce | last post by:
I searched but couldn't find anything _quite_ like this. I have some code I inherited that needs some work. Basically I have a template that is instantiated in the c++ code and so only the...
5
by: Julian | last post by:
I have this piece of code that is working fine in VC6... but when i tried to compile the same code in VC++.NET, I am getting this error : error C2512: 'std::basic_ostream<_Elem,_Traits>' : no...
4
by: manontheedge | last post by:
i'm working on a C++ program that uses operator overloads (which i'm just learning to use), and i have the ostream working fine, but i'm thinking i'm using it wrong somehow because the ofstream...
1
by: shark | last post by:
Hi, all. This time I met a problem in "inserter" functional. My program is like the following: /////////////////////////////////////////////////////////////////////////// #include <iostream>...
6
by: syang8 | last post by:
Any one can specify the problem of the following code? The compiling error is on the friend function. If the base class is not inherited from ostream, or I just remove the friend function from the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.