Thread: atof

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    7

    atof

    I have a multi-file program(im making a rudimentary excel application based in terminal), so I wont include all of the code.


    My problem involves stdlib.h.

    When I put stdlib.h in the header file, and use atof, the number is seemingly random.

    Ex: Put in 4.3, "78643.0000" is returned.


    However, when I put stdlib.h in the functions.c file, it works fine (put in 4.3, 4.3000 returned).


    Any clue what the issue might be?


    This is the header file code
    Code:
    //file name lab7.h
    #include<stdio.h>
    #include<strings.h>
    #include<stdlib.h>
    
    
    struct column{
         int type;
         char value[25];
         };
    typedef struct {
         struct column col[9];
         } matrix;
    
    
    void matrixinitialize(matrix * usermatrix);
    void dispsheet(matrix * usermatrix);
    int  prompt(int * rowcol, matrix * usermatrix);
    This is the top and atof of the functions.c file
    Code:
    #include "lab7.h"
    .
    .
    .
    float cell;
    cell = (float)atof(usermatrix[b].col[a].value);

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need to cast atof. If you do, it's because you forgot to include the appropriate header. Make a small program that illustrates your problem, not just random bits of code.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main( void )
    {
        char buf[ BUFSIZ ] = { "4.567" };
        float f = 0.0;
    
        f = atof( buf );
        printf( "f is %f\n", f );
    
        return 0;
    }

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

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    OK
    Assume: #include <stdlib.h> is in a header file header.h

    Code:
    #include "header.h"
    int main(){
      char number[] = "4.3"
      float numberf = 0;
    
      numberf = atof(number);
      printf("%f",numberf);
    }

    The output if I don't include stdlib.h(just in header file) in main is:78643.000
    If I do include stdlib.h in main is: 4.30000

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's lucky it even compiles if you don't use the header. To successfully use a function you need to know about the function. If you don't know about it, the compiler will assume everything for arguments and return values are integers.


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

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    I do use the header. My assignment requires me to use all #include statements in the header file, and just one #include statement in the main code, which is a reference to my header.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So why are you complaining about what happens when you don't use the header, if you are using the header? Who cares what happens if you don't use it?


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

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    No I'm saying that when I use the header to say #include <stdlib.h>, it doesn't work. I have to put #include<stdlib.h> in my functions.c program if I want it to work.


    Code:
    //file header.h
    #include <stdlib.h>
    Code:
    #include "header.h"
    int main()
    doesn't work


    Code:
    #include <stdlib.h>
    int main()
    does work.


    I need the other way to work, and was wondering if anyone had any ideas why it might not be working.
    Last edited by djessemoody; 10-27-2011 at 08:01 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by djessemoody View Post
    No I'm saying that when I use the header to say #include <stdlib.h>, it doesn't work. I have to put #include<stdlib.h> in my functions.c program if I want it to work.
    I don't believe you. Save as incredulous.h:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    Save as incredulous.c:
    Code:
    #include"incredulous.h"
    int main( void )
    {
        char right[ BUFSIZ ] = { "4.3" };
        float sure;
        
        printf( "right = %s\n", right );
        sure = atof( right );
        printf( "sure = %f\n", sure );
        
        return 0;
    }
    Compile and run.


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

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    That version works for me, however this is my output for the file im working on.
    Code:
    $ ./a.out
            |   A   |   B   |   C   |   D   |   E   |   F   |   G   |   H   |   I   |
    ---------------------------------------------------------------------------------
       1    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       2    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       3    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       4    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       5    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       6    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       7    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       8    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       9    |       |       |       |       |       |       |       |       |       |
    Enter the row and column (example: E9)
     Q0-Q9 to quit
    input: A3
    
    
     Old input: 
    Please Enter New Input: 4.3
            |   A   |   B   |   C   |   D   |   E   |   F   |   G   |   H   |   I   |
    ---------------------------------------------------------------------------------
       1    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       2    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       3    |78643.000|       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       4    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       5    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       6    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       7    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       8    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       9    |       |       |       |       |       |       |       |       |       |
    Enter the row and column (example: E9)
     Q0-Q9 to quit
    input:

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    This is if I just put stdlib.h in my functions.c file
    Code:
    $ ./a.out
            |   A   |   B   |   C   |   D   |   E   |   F   |   G   |   H   |   I   |
    ---------------------------------------------------------------------------------
       1    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       2    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       3    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       4    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       5    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       6    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       7    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       8    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       9    |       |       |       |       |       |       |       |       |       |
    Enter the row and column (example: E9)
     Q0-Q9 to quit
    input: A3
    
    
     Old input: 
    Please Enter New Input: 4.3
            |   A   |   B   |   C   |   D   |   E   |   F   |   G   |   H   |   I   |
    ---------------------------------------------------------------------------------
       1    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       2    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       3    |  4.300|       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       4    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       5    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       6    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       7    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       8    |       |       |       |       |       |       |       |       |       |
    ---------------------------------------------------------------------------------
       9    |       |       |       |       |       |       |       |       |       |
    Enter the row and column (example: E9)
     Q0-Q9 to quit

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's a reason I told you to try a small example. Did my example work for you? (Yes.) Then the problem isn't with your header, assuming your header just has that line in it. It's with something else. So how about actually posting your code instead of random snipped bits?


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

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    Code:
    void dispsheet(matrix * usermatrix) {
        int a = 0;
        int b = 0;
        float cell=0.0;
    
        printf("        |   A   |   B   |   C   |   D   |   E   |   F   |   G   |   H   |   I   |\n");
        while (a < 9){
            printf("---------------------------------------------------------------------------------\n");
            b= 0;
            printf("   %d    |",a+1);
            while(b<9){
                if(usermatrix[b].col[a].type == 0)
                    printf("%7s|",usermatrix[b].col[a].value);
    // ignore         if(usermatrix[b].col[a].type == 1)
    // ignore            avg(b,a,usermatrix)
                else if(usermatrix[b].col[a].type == 4){
                    cell = atof(usermatrix[b].col[a].value);
                    printf("%7.3f|",cell);
                }
                else printf("         |");
                b++;
            }
            printf("\n");
            a++;
        }
    }
    Code:
    
    
    
    # include <stdio.h>
    # include <stdlib.h>
    # include <strings.h>
    
    
    struct column{
         int type;
         char value[25];
         };
    typedef struct {
         struct column col[9];
         } matrix;
    
    
    void matrixinitialize(matrix * usermatrix);
    void dispsheet(matrix * usermatrix);
    int  prompt(int * rowcol, matrix * usermatrix);
    Last edited by djessemoody; 10-27-2011 at 08:27 PM.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by quzah View Post
    So how about actually posting your code instead of random snipped bits?

    Quzah.
    I second this suggestion.

    I vote for improper allocation of the usermatrix array.

    Tim S.
    Last edited by stahta01; 10-27-2011 at 08:35 PM.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Besides, the proper way to use includes is to put as few of them in the header files as possible, whilst at the same making sure that every header file has the includes it needs to compile if it were to be the first file included by a source file. Header include guards should also be used.
    If you're being taught otherwise then you're being taught wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Are there spaces between # and include in the real file, or is that error introduced by posting it here?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. atof is not exact
    By wakeup in forum C Programming
    Replies: 3
    Last Post: 04-02-2008, 09:46 AM
  2. atof() question
    By salvadoravi in forum C Programming
    Replies: 7
    Last Post: 01-10-2008, 02:37 PM
  3. Need some help with atof()
    By ICool in forum C Programming
    Replies: 13
    Last Post: 09-23-2007, 05:57 AM
  4. atof()
    By Luigi in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2003, 10:08 AM
  5. atof
    By larry in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2001, 09:56 AM