473,406 Members | 2,763 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.

Help urgent

13
i have a currency class which is as follows
Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. #include <sstream>
  3. #include <cmath>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. #include "Tokeniser.h"
  9. #include "Utilities.h"
  10. #include "Currency.h"
  11.  
  12. Currency::Currency():_dollars(0),_cents(0){}
  13.  
  14. Currency::Currency(const Currency& curr)
  15. {    
  16.     _dollars = curr._dollars;
  17.     _cents = curr._cents;
  18. }
  19.  
  20. Currency::Currency(int dollars, int cents):_dollars(dollars),_cents(cents){}
  21.  
  22. Currency::Currency(float curr)
  23. {
  24.     _dollars = (int)curr;  
  25.     _cents = (int)((curr - _dollars) * 100);     
  26. }
  27.  
  28. Currency::Currency(const string& curr)
  29. {
  30.      double f = atof(curr.c_str());
  31.     _dollars = (int)f;
  32.     _cents = (int)((f - _dollars) * 100);
  33. }
  34.  
  35. int Currency::getDollars() const
  36. {    
  37.     return _dollars;        
  38. }
  39.  
  40. int Currency::getCents() const
  41. {    
  42.     return _cents;        
  43. }
  44.  
  45. void Currency::setDollars(int dollars)
  46. {    
  47.     this->_dollars = dollars;        
  48. }
  49.  
  50. void Currency::setCents(int cents)
  51. {    
  52.     this->_cents = cents;        
  53. }
  54.  
  55. float Currency::toFloat() const
  56. {
  57.  
  58.  
  59.     return 0;
  60. }
  61.  
  62. string Currency::toString() const
  63. {
  64.     Currency curr;
  65.  
  66.     string returning_string;
  67.     string assignStringDollars, assignStringCents ;
  68.     ostringstream dollarStream, centStream;
  69.  
  70.     dollarStream << curr.getDollars();
  71.     centStream << curr.getCents();
  72.  
  73.     assignStringDollars = dollarStream.str();
  74.     assignStringCents = centStream.str();
  75.  
  76.     returning_string = assignStringDollars + "." + assignStringCents;
  77.  
  78.     return returning_string;    
  79.  
  80.  
  81. }
  82.  
  83. void Currency::fromFloat(float curr)
  84. {
  85.  
  86.  
  87.  
  88. }
  89.  
  90. int Currency::fromString(const string& curr)
  91. {
  92.     Tokeniser tok(curr, ".");
  93.     char *garbage;
  94.  
  95.     _dollars = strtol(tok.nextToken().c_str(), &garbage, 10);
  96.     if (garbage != NULL && strlen(garbage) > 0)
  97.     {
  98.         return -1;
  99.     }
  100.  
  101.     _cents = strtol(tok.nextToken().c_str(), &garbage, 10);
  102.     if (garbage != NULL && strlen(garbage) > 0)
  103.     {
  104.         return -1;
  105.     }
  106.  
  107.     return 0;
  108. }
  109.  
  110. Currency& Currency::operator =(const Currency& obj)
  111. {
  112.     if (*this != obj)
  113.     {
  114.         _dollars = obj.getDollars();
  115.         _cents = obj.getCents();
  116.     }
  117.  
  118.     return *this;
  119. }
  120.  
  121. bool Currency::operator <(const Currency& obj) const
  122. {
  123.     if (_dollars < obj.getDollars())
  124.     {
  125.         return true;
  126.     }
  127.     if (_dollars > obj.getDollars())
  128.     {
  129.         return false;
  130.     }
  131.     if (_cents < obj.getCents())
  132.     {
  133.         return true;
  134.     }
  135.  
  136.     return false;
  137. }
  138.  
  139. bool Currency::operator ==(const Currency& obj) const
  140. {
  141.     if (_dollars == obj.getDollars() && _cents == obj.getCents())
  142.     {
  143.         return true;
  144.     }
  145.  
  146.     return false;
  147. }
  148.  
  149. bool Currency::operator !=(const Currency& obj) const
  150. {
  151.     return !(*this == obj);
  152. }
  153.  
  154. ostream& operator <<(ostream& out, const Currency& obj)
  155. {    
  156.     out << obj.getDollars() << "." << obj.getCents();
  157.     return out;        
  158. }
How do i do the tofloat and from float ?? please

• Function: toFloat
This function converts the internal currency representation to a floating point number.
• Function: fromFloat
This function converts a floating point number to the internal representation of the currency class.
Nov 27 '06 #1
8 1515
sicarie
4,677 Expert Mod 4TB
Can you tell me, in a little more detail, what toFloat and fromFloat are supposed to do? (ie. what variables do they use, what do those variables represent, and what are you going to return?)
Nov 27 '06 #2
CJK
13
Expand|Select|Wrap|Line Numbers
  1. /**
  2.      * Returns a float representation of the current instance
  3.      *
  4.      * @returns float representation
  5.      */
  6.     float toFloat(void) const;
  7.  
  8. /**
  9.      * Converts a float object to a currency object
  10.      *
  11.      * @param curr Float object to convert
  12.      */
  13.     void fromFloat(float curr);
  14.  
Nov 27 '06 #3
sicarie
4,677 Expert Mod 4TB
I can see the code, but can you describe that in a few sentences?

(This will create your algorithm/pseudocode and give a better conceptualization of what is going on in those functions)
Nov 27 '06 #4
CJK
13
See the thing is that is how i was given it and so am so lost its not funny
Nov 27 '06 #5
sicarie
4,677 Expert Mod 4TB
See the thing is that is how i was given it and so am so lost its not funny
Right, and that's why you need to understand what they are doing - this is a simple coding job, but the conceptualization of it is a little more difficult.

So with the variables in the class, you have _dollars and _cents, right? toFloat takes nothing, but returns a float as you can tell from the prototype

float toFloat(void)

The first part is the return type, and the part in the parenthesis is what you are passing to it.

So toFloat is going to use the values it has (_dollars and _cents) to create a floating point number of the whole, and return that. Just in a simple sentence or two, can you tell me how it would start doing that?
Nov 27 '06 #6
CJK
13
umm maybe converting firstly the two ints to floats?
Nov 27 '06 #7
sicarie
4,677 Expert Mod 4TB
umm maybe converting firstly the two ints to floats?
Yeah. And then add them together. Pretty easy, though you might have to divide _cents by 100 (make sure it's a float first!). Try working on that, post again if you get caught up somewhere.
Nov 27 '06 #8
CJK
13
Thank you for the help its 3am and im out of it a bit
Nov 27 '06 #9

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

Similar topics

28
by: Tamir Khason | last post by:
Follwing the struct: public struct TpSomeMsgRep { public uint SomeId;
8
by: Tim::.. | last post by:
Can someone please tell me why I keep getting the following error for some of my web application users but not others??? Even though the application runs from a central webserver??? Thanks for...
7
by: zeyais | last post by:
Here is my HTML: <style> ..leftcolumn{float:left;width:300px;border: 1px solid #ccc} ..rtcolumn{float:left;width:600px;border: 1px solid #ccc} </style> <body> <div class="leftcolumn"...
17
by: Saps | last post by:
Hi all. Can anyone help me here. I have loads of .sql files and i need a way to call these from my asp page so the user can run them from the browser. Meaning i have a page with a list of all...
3
by: N. Spiker | last post by:
I am attempting to receive a single TCP packet with some text ending with carriage return and line feed characters. When the text is send and the packet has the urgent flag set, the text read from...
7
by: Rainer Queck | last post by:
Hello NG, I am currently confronted with the task to equip a vs2005 project with context sensitive online help (F1). Reading through the MSDN-Library I found out about the "HelpProvider" class,...
9
needhelp123
by: needhelp123 | last post by:
Can any one send me a quick sort simple logic pogram... its very urgent urgent
6
by: jenipriya | last post by:
Hi all... its very urgent.. please........i m a beginner in oracle.... Anyone please help me wit dese codes i hv tried... and correct the errors... The table structures i hav Employee (EmpID,...
1
by: psantosh12 | last post by:
Hello Frnds Please need help to resolve error.......... it is very very urgent........ The error is Runtime Error Description: An application error occurred on the server. The current custom...
2
by: =?Utf-8?B?U2NvdHRSYWREZXY=?= | last post by:
I'm creating a doc project for my c# program. I've done this before but this time sonething is wrong. I build my doc project and is succeeds but when I open the help file, there is no documentation...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.