converting problem
Jun 30, 2009 at 5:50pm UTC
ltdtombs isn't working.i take this error
"cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `char*' in assignment " i want to convert long double to char*
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
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <string>
#include <sstream>
using namespace std;
//////////////////////////////////
class bMoney
{
private :
long double money;
int n;
char * end;
int len;
char string[100];
char * karakter;
public :
bMoney(): money(0.0),n(0),len(0)
{ }
///////////////////////////
void getMoney()
{
cout << "Enter money: " ;
cin >> string;
}
//////////////////////////
void putMoney()
{cout <<"Your cash is:" << money << end;}
////////////////////////////////////////////////////////////
long double mstold(char para[])
{
int n=0;
char karakter[100];
char * end;
int len = strlen(string);
for (int i=0; i<len+1; i++)
{
if (string[i] >= '0' && string[i] <= '9' )
{
karakter[n] = string[i];
n++;
}
}
money = strtold(karakter,&end);
return money;
}
///////////////////////////////////////////////////////
void ldtombs()
{
std::ostringstream ss;
ss << money;
karakter = ss.str();
}
};
int main(int argc, char *argv[])
{
system("PAUSE" );
return EXIT_SUCCESS;
}
Jun 30, 2009 at 6:06pm UTC
karakter = strdup( ss.str().c_str() );
and then you have to free( karakter ) when you are done with the memory
Jun 30, 2009 at 8:06pm UTC
And why do you include
stdlib.h
and
cstdlib
in the same program? They're both identical, except that the first one is non-standard.
Jun 30, 2009 at 9:19pm UTC
thank your answers.@jsmith where does come c.str() ?.
@Qwertyman thank you. i choice stdlib.h because thats nice :)
Jun 30, 2009 at 9:42pm UTC
Jun 30, 2009 at 10:21pm UTC
cool thank you again
Jul 1, 2009 at 1:06am UTC
i choice stdlib.h because thats nice :)
???
Jul 1, 2009 at 6:39am UTC
stdlib.h more content than cstdlib
Jul 1, 2009 at 9:46am UTC
Last edited on Jul 2, 2009 at 5:03pm UTC
Jul 1, 2009 at 9:53am UTC
stdlib.h is the C header
cstdlib is the C++ header
If you are programming in C++ use cstdlib
Jul 2, 2009 at 9:00am UTC
ok.thank u
Topic archived. No new replies allowed.