Thread: atof() acting weird?

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    10

    atof() acting weird?

    This is how the program is setup.

    The atof() function isn't working the way it should, and is producing bogus numbers. Any idea's?

    Normally I'd just start with a float, but this is for a bigger program. I just made a little script that re-produces this problem for me. Any idea's?


    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char test[100];
        double num = 0;
    
    
        printf("Testing number: ");
        gets(test);
        
        num = atof(test);
        printf("num = %.2f\n", num);
    
        return 0;
    }
    Results:

    Testing number: 45
    num = 2293232.00

    Thanks,
    Mellowz
    Last edited by Mellowz; 07-07-2006 at 07:08 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Perhaps you should include stdlib.h.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    Noob question...What atof() function was the program using since the header wasn't included, or why didn't the compiler complain?


    And gets() ftw
    Last edited by Link_26; 07-07-2006 at 07:38 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What atof() function was the program using since the header wasn't included
    Undefined behavior.

    >or why didn't the compiler complain?
    It probably didn't have warnings set high enough.

    >And gets() ftw
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Don't use gets. It's too safe. Use fgets. It's more dangerous. Why
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Floating Values acting weird.
    By torqu3e in forum C Programming
    Replies: 9
    Last Post: 12-19-2006, 11:12 AM
  2. DirectX 9 materials acting weird
    By Rune Hunter in forum Game Programming
    Replies: 7
    Last Post: 12-27-2005, 12:07 AM
  3. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 04:53 PM
  4. char[] acting weird
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2003, 06:45 PM
  5. n00b needs help with api graphics acting very weird
    By CheeseWeaver in forum Windows Programming
    Replies: 2
    Last Post: 03-18-2003, 03:15 PM