Thread: problem with atof

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    184

    problem with atof

    hello guys,
    i was with one of the problem where i was forced to use atof function. the problem here is, it returns me 0.0 always which mean there in an error while converting it. can any on tell me at what instance will the atof generator error. here is the peice of code where i used the atof function

    Code:
     for(ctr=1;ctr<=argc;ctr++)
       {
        converted[ctr-1]=atof(*argv[ctr]);
       }
    any examples of atof error will be appriciated

    s.s.harish

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >for(ctr=1;ctr<=argc;ctr++)
    This is suspicious.

    >converted[ctr-1]=atof(*argv[ctr]);
    And this is broken. argv[argc] is a null pointer, and because your loop continues through ctr having a value of argc, you're dereferencing a null pointer. Dereferencing argv[ctr] is also incorrect as it's already a pointer to char and that's what atof expects. Perhaps something more like this is what you want:
    Code:
    for ( i = 1; i < argc; i++ ) /* Skip program name */
      converted[i - 1] = atof ( argv[i] );
    >any examples of atof error will be appriciated
    If the string doesn't represent a valid floating point value?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    no prelude it dosn't work for me still, look at this code
    Code:
    int main(int argc, char *argv[])
     {
      float converted[argc];
      float result;
      .
      .
      .
      for ( i = 1; i < argc; i++ ) /* Skip program name */
      converted[i - 1] = atof ( argv[i] );
      .
      .// and here some mathematical function which could perform basic operation will be called
      .//by sending the converted[], &result
      .
      .
      printf("%f",result);
    and output will look like this
    Code:
    [d4088019@outcast ~/other]$ gcc ex.c
    [d4088019@outcast ~/other]$ a.out 1.25 + 3.25
    The result is:0.000000
    or when i use atoi rather than atof i will work fine by returning th proper interger value
    but when i use the atof fucntion i returns me the 0.0 which is effectivly an error. and couldn't able to find the why it is an error

    any help will be appriciated

    s.s.harish

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by ssharish
    Code:
    
      float converted[argc];
    and output will look like this
    Code:
    [d4088019@outcast ~/other]$ gcc ex.c
    [d4088019@outcast ~/other]$ a.out 1.25 + 3.25
    The result is:0.000000
    This line is definitely an error and I'm surprised that you didn't get warning or error while compiling
    Anyway you should use

    Code:
    float *converted = malloc(argc*sizeof(float));
      float result;
      int i;
      for ( i = 1; i < argc; i++ ) /* Skip program name */
      converted[i - 1] = atof ( argv[i] );
    Note that atof returns double so expect to get a compiler warning about loosing precision.

    And one thing more:
    PRELUDE IS ALWAYS RIGHT, AND HER EXAMPLES ARE ALWAYS CORRECT.
    Last edited by Micko; 03-22-2005 at 09:04 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Can't say I see your problem, ssharish.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    	double *converted = malloc(argc*sizeof(double));
    	int i;
    
    	for (i = 1; i < argc; i++ ) /* Skip program name */
    	{
    		converted[i - 1] = atof ( argv[i] );
    		printf("%f ",converted[i-1]);
    	}
    
    	//and here some mathematical function which could perform basic operation will be called
    	//by sending the converted[], &result
    	free(converted);
    	return 0;
    }
    With input:
    Code:
    1.25 + 3.25
    Output:
    Code:
    1.250000 0.000000 3.250000
    And one thing more:
    PRELUDE IS ALWAYS RIGHT, AND HER EXAMPLES ARE ALWAYS CORRECT.
    Quoted for emphasis.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    thax guys i works works for me now. probabaly i might gone wrong.

    [code]

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    Code:
    AND THAX PRELUDE FOR LETTING ME KNOW THE BASIC PROBLEM
    s.s.harish

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    guys i have a problem with atof still, can any one tell me what is wrong with this main prog. the atof return me 0.0 allways
    Code:
    int main(int argc, char *argv[])
     {
      double *converted=(double *)malloc(argc*sizeof(double));
      float result=0.0;
      char ch;
      int ctr,i=0;
     
       for ( i = 1; i < argc; i++ ) /* Skip program name */
       {
      converted[i - 1] = atof ( "1.25 ");
      printf("%d",converted[i-1]);
    }  
      ch=*argv[2];
      calcs(ch,converted,&result);
      print_result(result);
      free(converted);
      getchar();
      return 0;
     }
    thax very much

    s.s.harish

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >printf("%d",converted[i-1]);
    %d is for signed integers, you want %f to print a double.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    Code:
    >printf("%d",converted[i-1]);
    %d is for signed integers, you want %f to print a double.
    still it gives me an error Prelude

    can u please help me with this

    s.s.harish

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Come on now. You've posted here enough to know better.
    Code:
    int main(int argc, char *argv[])
     {
      double *converted=(double *)malloc(argc*sizeof(double));
    1) Stop casting malloc.

    Quote Originally Posted by ssharish
    still it gives me an error Prelude
    2) Go read the forum announcements again, because you've obviously skipped the part about posting the error you're getting.

    3) Your indentation is absolutely horrible.

    Code:
      calcs(ch,converted,&result);
    4) calcs? What the hell is calcs? You want us to find the error in a function you don't include? We're psychics now?


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    the actual problem is not with the calcs it is with the atof function llok at this
    Code:
    int main(int argc, char *argv[])
     {
      double *converted=(double *)malloc(argc*sizeof(double));
      float result=0.0;
      char ch;
      int ctr,i=0;
     
       for ( i = 1; i < argc; i++ ) /* Skip program name */
       {
      converted[i - 1] = atof ( argv[i]);
      printf("%lf",converted[i-1]);
    }  
      ch=*argv[2];
      calcs(ch,converted,&result);
      print_result(result);
      free(converted);
      getchar();
      return 0;
     }
    float calcs(char ch, double *argv,float *ptr)
     {
      switch(ch)
        {
         case'+': add(argv,&ptr);
                  break;
         case'-': sub(argv,&ptr);
                  break;
         case'*': mul(argv,&ptr);
                  break;
         case'/': div(argv,&ptr);
                  break;
         default: printf("Invalid Operation\n");
                  break;
        }
      }
    and in tha mian atof will always return me the 0.0 rather than giving me the converted value of the string.

    i can this code will let u know what is the problem is. and the rest of the part is working fine
    thank u very much

    s.s.harish

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's funny, it works for me:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main( int argc, char *argv[] )
    {
        if( argc > 1 )
        {
            int i;
            for( i = 1; i < argc; i++ )
                printf("%s is %f\n", argv[ i ], atof( argv[ i ] ) );
        }
    
        return 0;
    }
    
    
    /*
    $ atofproggy 10.2 11.3 14.4
    10.2 is 10.200000
    11.3 is 11.300000
    14.4 is 14.400000
    */
    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    i found the problem guys, the probleam was i havn't included the header file stdlib.h

    but atoi works withoout including the appropriate hearder(sdlib.h) but why not atof

    can any one tell me why it is??
    i am very curise about that

    thank very much

    s.s.harish

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Because your compiler feels like it. Or doesn't, as the case may be. There is nothing in regards to C which says how your compiler has to work as far as being nice enough to correct you when you do something silly like leave out headers.

    What it should do is give you error messages saying it can't find the function you're trying to use. If it does work, well, you're lucky. If not, you end up with something like this. I suspect you were getting some warnings that you were ignoring.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM