Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
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.
 

CaptainZap

macrumors regular
Jan 17, 2007
170
0
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

CaptainZap

macrumors regular
Jan 17, 2007
170
0
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.
 

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.