hi,
how to convert the DOUBLE date into a string without using some certain function of the library?
I need your code(name.c)
thx in advance.
happy new year!
hi,
how to convert the DOUBLE date into a string without using some certain function of the library?
I need your code(name.c)
thx in advance.
happy new year!
Post a bit of your code that you've tried so far.
Also, you might want to read this:
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
but "sprintf""snprintf""fprintf"are all functions of the library.
So you want to do this yourself? Do it on paper first, then translate that to psuedo code, then real code. Have fun
Just in case you don't know, this is an educational forum, where we help others learn. It's not a place to get free coding done. Make some effort, post the code you've tried and someone will help you.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
I am confesed by my own code:
When I entered the double date:4545.6767.the output is 4545.676699999999999.Why?what should I do ?
Code:#include <stdio.h> #include <string.h> #define Array_MaxValue 17 char *MY_DoubleToChar(double db,char *db_str) { int DecP=1,loop; double _db; int db_arr[Array_MaxValue]; if(db>=0.0) { while(db>=10.0) { DecP++; db/=10; } for(loop=0;loop<17;loop++) { if(loop==DecP) { db_arr[loop]=-2; continue; } db_arr[loop]=(int)db; db-=db_arr[loop]; db*=10.0; } for(loop=0;loop<17;loop++) db_str[loop]=db_arr[loop]+48; return(db_str); } else { _db=-db; while(_db>=10.0) { DecP++; _db/=10; } for(loop=0;loop<17;loop++) { if(loop==DecP) { db_arr[loop]=-2; continue; } db_arr[loop]=(int)(_db); _db-=db_arr[loop]; _db*=10.0; } for(loop=0;loop<17;loop++) db_str[loop]=db_arr[loop]+48; return(db_str); } } int main() { const double i; char *str; char *sign; str=(char *)malloc(sizeof(char)*Array_MaxValue); sign=(char *)malloc(sizeof(char)); printf("enter the DOUBLE date:\n"); scanf("%lf",&i); if(i<0.0) { *sign='-'; printf("%c",*sign); printf("%s",MY_DoubleToChar(i,str)); } else printf("%s",MY_DoubleToChar(i,str)); free(str); getch(); return 0; }
>sign=(char *)malloc(sizeof(char));
Why are you using malloc for a single char?
>const double i;
You don't treat this as const, so don't declare it as const.
>str=(char *)malloc(sizeof(char)*Array_MaxValue);
You forgot to include stdlib.h and hid that error by casting the result of malloc. See the FAQ concerning this issue and you'll learn how to call malloc properly.
>if(db>=0.0)
Beware floating-point comparisons, they can be tricky.
>the output is 4545.676699999999999
Try walking through your code. Does each instruction do as you expect? If not, try and figure out why it does what it does.
My best code is written with the delete key.
> the output is 4545.676699999999999.Why?
Perhaps you should also be reading the background material I posted in this thread
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
> the output is 4545.676699999999999.Why?
Perhaps you should also be reading the background material I posted in this thread
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.