-2

I'm getting an error:

Type invalid operands to binary & (have 'int *' and 'int')

here is my program. The problem occurs at line 34 or the fscanf num1

    #include <stdio.h>
    #include <stdlib.h>

    FILE *infile;
       FILE *prnt;
      main()
     {
int num1, num2, nums;
char complex;
float fcost;
char name [11];
infile = fopen ("F:/DATA.txt",  "r");
prnt   = fopen ("F:/income.txt",    "w");
if (infile == 0)
{
    printf ("FILE NOT ON DISK\n");
    system("pause");
    return 0;
}
fprintf (prnt, "%-15s %-23s %6s\n\n", "ABAHLMAN", "Program 1", "PAGE 1");
fprintf (prnt, "\n");

fscanf (infile, " %i %i %i %f %c", &nums &num1 &num2 &fcost &name);

while (!feof (infile))
{
    int area = (nums * 200) + (num1 * 300) + (num2 * 450);
    float cost = fcost + (area * 75.00);
    double income = 12 * ((nums *450) + (num1 * 550) + (num2 *700));
    float payback = cost/ income;

    fprintf (prnt, "%-10s %5f %7c %9.2f\n", name, payback, area, cost);
    fscanf (infile, " %d %d %d %f %c", &nums &num1 &num2 &fcost &name);
}

fclose (infile);
fclose (prnt);
return 0;
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. – too honest for this site Sep 04 '16 at 19:02

2 Answers2

0

You are not separating the arguments with comma in fscanf() statements. They should be:

fscanf (infile, " %i %i %i %f %s", &nums, &num1, &num2, &fcost, name);

and

fscanf (infile, " %d %d %d %f %s", &nums, &num1, &num2, &fcost, name);

Note that name is an array and it gets converted into a pointer when you pass it to fscanf(). So, the & operator needs to be dropped. As noted in the comments, the format should be %c for name.

Also, see: What is array decaying?

I'd also suggest to use a standard definition for main() function. main() {..} is outdated and should be avoided. Instead, you can write it as int main(int).

Community
  • 1
  • 1
P.P
  • 117,907
  • 20
  • 175
  • 238
  • Thank you its these small mistakes that get me. So declaring main int is a new thing? My professor is very old and he has us using watcom, but i switched to eclipse any thanks for your help. – Anthony Bahlman Sep 04 '16 at 19:30
  • @AnthonyBahlman It's not really a new thing. It's been there at least since 1989 (1st C standard)! – P.P Sep 04 '16 at 19:31
0

I see a few problems. First, no commas to separate the arguments for scanf.

Next, the last argument for fscanf should have %s format and be passed name without &.

Next, feof is not the way to control a loop, you should check the return value from fscanf anyway.

You also use the wrong format specifier %c for area in printf which should be %d

Finally be consisent with the use of %d or %i format specifier. Unless you want the user to input in other number bases than decimal, stick to %d.

So I suggest the loop should be

while (fscanf (infile, " %d %d %d %f %s", &nums, &num1, &num2, &fcost, name) == 5)
{
    int area = nums * 200 + num1 * 300 + num2 * 450;
    float cost = fcost + area * 75.00;
    double income = 12 * (nums * 450 + num1 * 550 + num2 * 700);
    float payback = cost / income;
    fprintf (prnt, "%-10s %5f %7d %9.2f\n", name, payback, area, cost);
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56