calculator program

Discussion in 'Mac Programming' started by jamesapp, May 26, 2008.

  1. jamesapp macrumors 6502a

    Joined:
    Mar 7, 2008
    #1
    james-collinss-macbook-pro:chap2 jamescollins$ gcc atof.c calculator.c -o calculator.o
    atof.c: In function ‘atof’:
    atof.c:15: error: called object ‘val’ is not a function

    got the following error messages from terminal when i tried to compile a program from a book on c.

    i have a question on compiling what should i call a function which the book calls

    atof should i compile it as:

    Code:
     atof.c calculator.c -o calculator.o
    
    i also don't understand the error message:

    Code:
    atof.c:15: error: called object ‘val’ is not a function
    
    here is the function file which i called atof.c

    Code:
    #include <ctype.h>
    
    /* atof: convert string s to double used with calculator */
    double atof(char s[])
    {
      double val, power;
      int i, sign;
      
      for (i = 0; isspace(s[i]); i++) /* skip white space */
          ;
      sign = (s[i] == '-') ? -1 : 1;
      if (s[i] == '+' || s[i] == '-')
          i++;
      for (val = 0.0; isdigit(s[i]); i++)
          val = 10.0 * val (s[i] - '0');
      if (s[i] == '.')
          ++i;
      for (power = 1.0; isdigit(s[i]); i++) {
          val = 10.0 * val + (s[i] - '0');
          power *= 10.0;
      }
      return sign * val / power;
    }
    
    and here is the main program (should i call this calculator.c ?)

    Code:
    #include <stdio.h>
    
    #define MAXLINE 100
    
    /* rudimentary calculator using atof.c function */
    main()
    {
      double sum, atof(char []);
      char line[MAXLINE];
      int getline(char line[], int max);
      
      sum = 0;
      while (getline(line, MAXLINE) > 0)
          printf("\t%g\n", sum += atof(line));
      return 0;
    }
    
    any help would be appreciated.
     
  2. CaptainZap macrumors regular

    Joined:
    Jan 17, 2007
    #2
    Code:
    #include <ctype.h>
    
    /* atof: convert string s to double used with calculator */
    double atof(char s[])
    {
      double val, power;
      int i, sign;
      
      for (i = 0; isspace(s[i]); i++) /* skip white space */
          ;
      sign = (s[i] == '-') ? -1 : 1;
      if (s[i] == '+' || s[i] == '-')
          i++;
      for (val = 0.0; isdigit(s[i]); i++)
          val = 10.0 * [b]val (s[i] - '0')[/b];
      if (s[i] == '.')
          ++i;
      for (power = 1.0; isdigit(s[i]); i++) {
          val = 10.0 * val + (s[i] - '0');
          power *= 10.0;
      }
      return sign * val / power;
    }
    
    Look over that bolded part a little more.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    So CaptainZap already pointed you towards your first problem (Which, by the by, you should have been able to track pretty quickly, as the compiler told you what line the error was on), but there are a few other things going on.

    Compilation should go:
    gcc -c atof.c
    gcc atof.o -o calculator calculator.c

    It looks like you are using getline in your main function, which isn't C. I don't know if the book you are using includes a getline implementation, or if you're supposed to use the GNU extension, etc. but I wanted to let you know that this won't compile as is. fgets is an alternative that is standard, but won't read nulls, which probably is OK in this case.

    -Lee
     
  4. CaptainZap macrumors regular

    Joined:
    Jan 17, 2007
    #4
    The K&R C book teaches how to make a getline function and then uses it throughout the book. It isn't included in the source James posted but I'm assuming/hoping it is in there somewhere.

    And for compilation, I think he is putting a .o on the end to distinguish between the two files.
     
  5. jamesapp thread starter macrumors 6502a

    Joined:
    Mar 7, 2008
    #5
    i added the getline function. from reading the book the author didn't tell me to add it. i guess when i saw getline i should have known to add the function.
    i fixed the atof problem by adding a +
    Code:
    val = 10.0 * val + (s[i] - '0');
    
    the program compiled and ran successfully
    thanks.
     

Share This Page