我收到错误:
将无效操作数键入二进制和& (有' int *'和' int')
这是我的计划。问题发生在第34行或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;
}
答案 0 :(得分:0)
您没有在fscanf()
语句中用逗号分隔参数。他们应该是:
fscanf (infile, " %i %i %i %f %s", &nums, &num1, &num2, &fcost, name);
和
fscanf (infile, " %d %d %d %f %s", &nums, &num1, &num2, &fcost, name);
请注意,name
是一个数组,当您将其传递给fscanf()
时会转换为指针。因此,需要删除&
运算符。如评论中所述,%c
的格式应为name
。
我还建议对main()
函数使用标准定义。 main() {..}
已过时,应予以避免。
相反,您可以将其写为int main(int)
。
答案 1 :(得分:0)
我看到一些问题。首先,没有逗号分隔scanf
的参数。
接下来,fscanf
的最后一个参数应为%s
格式,并且name
不会&
传递。
接下来,feof
不是控制循环的方法,你应该检查来自fscanf
的返回值。
您还在%c
中area
使用了错误的格式说明符printf
,%d
最后请谨慎使用%d
或%i
格式说明符。除非您希望用户输入除小数之外的其他数字,否则请坚持%d
。
所以我建议循环应该是
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);
}