GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-May-2004, 17:40
kilgortrout kilgortrout is offline
New Member
 
Join Date: May 2004
Posts: 12
kilgortrout is on a distinguished road

convert double to char


hello,

I need to convert a double (20 with 4 digits after decimal) to char [20].

can any oen help.

thank you
kilgor
  #2  
Old 11-May-2004, 18:04
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
this has been discussed in a thread recently. click below:
previous thread
Last edited by JdS : 13-May-2004 at 11:30. Reason: used bbcode for [THREAD] link
  #3  
Old 12-May-2004, 08:11
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by machinated
this has been discussed in a thread recently. click below:
previous thread

And once again, I think that sprintf is the easiest way to do this. I wrote custom functions to do this when I found out that itoa nad ftoa are not standard functions and the *suck* compared to sprintf (my opinion).

CPP / C++ / C Code:
char my_string[22];   //To hold . and null
sprintf(string,"%20.4f",my_double);

Cheers,
d
  #4  
Old 12-May-2004, 17:07
kilgortrout kilgortrout is offline
New Member
 
Join Date: May 2004
Posts: 12
kilgortrout is on a distinguished road
Quote:
Originally Posted by dsmith
And once again, I think that sprintf is the easiest way to do this. I wrote custom functions to do this when I found out that itoa nad ftoa are not standard functions and the *suck* compared to sprintf (my opinion).

CPP / C++ / C Code:
char my_string[22];   //To hold . and null
sprintf(string,"%20.4f",my_double);

Cheers,
d
i just tried it but got this error
double errcnt = 0;
char num_errcnt[13];
int fillstruct();

int main()
{
fillstruct();

return(0);
}

int fillstruct()
{
int x;
strcpy(num_errcnt, "000006789012");
errcnt = atof(num_errcnt);
cout << "numerr " << num_errcnt[11] << endl;
cout << "prefix " << errcnt << endl;
strcpy(num_errcnt, "000000000000");
cout << "before " << num_errcnt << endl;
sprintf(num_errcnt, "%20.4f", errcnt);
cout << "after " << num_errcnt << endl;
cin >> x;
return(0);
}
error: implicit declaration of function 'int sprintf(...)
  #5  
Old 12-May-2004, 17:11
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
you probably dint include the proper header file for sprintf. which is it???? ask dsmith!
  #6  
Old 12-May-2004, 17:15
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by machinated
you probably dint include the proper header file for sprintf. which is it???? ask dsmith!

I agree mach. (But I usually agree with you anyway )

try:
CPP / C++ / C Code:
#include <stdio.h>
  #7  
Old 12-May-2004, 17:21
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
hm.. thats weird. i just tried it without including stdio.h and it worked. probably because i used namespace std. by the way, why are you CINing x but not doing anything with it? did you mean to return x?

also whats weird is that your compiler assumes that sprintf is of type int!
  #8  
Old 12-May-2004, 17:28
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by machinated
also whats weird is that your compiler assumes that sprintf is of type int!

This is a typical error when you don't have a function definition because C/C++ assumes that it returns an int.
  #9  
Old 12-May-2004, 18:36
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,281
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by dsmith
This is a typical error when you don't have a function definition because C/C++ assumes that it returns an int.
Not only that but sprintf() does return an int... ;-)
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #10  
Old 12-May-2004, 21:21
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
what do you know! c++ "assumed" something correctly!
__________________
spasms!!!
 
 

Recent GIDBlogI?m Home by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
using vector or array oshiotse C++ Forum 4 16-Apr-2004 11:59
some I/O problems...again cameron C++ Forum 3 03-Mar-2004 22:39
(read/write file) newbie need help plz momotx C Programming Language 6 28-Jan-2004 14:40
convert long to pointer to char realpopeye C++ Forum 2 26-Sep-2003 11:22
char to operator calculus87 C++ Forum 3 04-Sep-2003 11:05

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:07.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.