String to Long Double and vice versa

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<iostream>
using namespace std;
#include<cstdlib>
#include <string>
#include<cstring>
#include "convert.h"



class bMoney
{
private:
long double money;
public:
bMoney();
bMoney(char s[]);
void operator + (bMoney);
void putmoney();
};

//----------------

bMoney::bMoney(): money(0)
{}
//-----------------------------
bMoney::bMoney(char temp[])
{

     money = strtold(temp,'\0');// here is something wrong
    cout <<"BadConversion:" <<money<<endl;
 }
 
 //-----------
     
     void bMoney::operator + (bMoney s)
     {money+=s.money;
     s.money = money;}
     
////-----
     
     void bMoney::putmoney()
     {
  
       string temp= stringify(money);   // this is ok 


     
     cout <<"money Ldouble:"<<money<<endl;
     cout <<"temp string;" <<temp;
     
     cout <<"\n";
     }
     
     //-------------------------------
     
     int main()
     {
         bMoney obj1="35862.1";
         bMoney obj2="2521521";
         
         obj1.putmoney();
         obj1+obj2;
         
         obj2.putmoney();
         obj1.putmoney();
         
         cin.get();cin.get();
         return 0; }
     
     



//and the Header file convert which works OK!

 #include <iostream>
 #include <sstream>
 #include <string>
 #include <stdexcept>
 
 class BadConversion : public std::runtime_error {
 public:
   BadConversion(std::string const& s)
     : std::runtime_error(s)
     { }
 };
 
 inline std::string stringify(long double x)
 {
   std::ostringstream o;
   if (!(o << x))
     throw BadConversion("stringify(double)");
   return o.str();
 } 


The strtold function doesn't convert the string to long double properly, and i couldn't find why even after hours of searching the web.What is the matter with it:D?
Last edited on
Next time please use code tags. There is a button when you are posting, under "Format:". Paste your code, highlight all the code, and click the <> button. It makes it much easier to read.

Thanks.


Anyways, onto your code...

Hmm, well I took a look at it.... That's a good one. I can't figure it out... Make sure the parameters are correct for strtold(). It's not a standard function, so I have no idea...
Last edited on
The output looks ok, which are the same as the initial values in main(). What output were u expecting?
I get only 6 digits any value larger than 6 digits wull get some other signs and end with 006
Still works with
358623.128965
and
252152145123234
. Set the precision higher and display in scientific notation to see the other digits.
hmm ty, so it should work! something i love to hear, can you tell me what changes do i have to do to set the precision higher and display scientific notation. I know to have red about it once but can't remember meanwhile I try to google it.
http://www.cplusplus.com/reference/iostream/manipulators/

You could try setting the precision first without the scientific notation.
so Ty for the link. It corrected the money output but what with the string TEMP , is there anyway to output it same way as Money cuz it is still 2.80882e+007 instead of 28088187.11 ,
but if it's more complicated. I can let it that way as it gives the result i need.

--
nah that was it I learned some stuff with that code and it was enough thanks 'bobito' for your help.
I noticed that you have 20 post surely they are 20 answers, same amount to my questions :D
Last edited on
how about dropping stringify and sending money directly to cout with the manipulators fixed and setprecision?
Topic archived. No new replies allowed.