编程和为类编写C代码的新手。 我的程序假设从.txt文件中获取数据,计算,然后将结果打印到.txt文件中。
我得到:
双精度*和双精度到二进制运算符的无效操作数
在第 fscanf(therm, "%lf %lf",&temp &con);
行(底部附近的for
环(
/* Preprocessor directives */
#include <stdio.h>
#include <math.h>
#define inputfile "c:\engr 200\thermal.txt"
#define outputfile "c:\engr 200\results.txt"
/*Main function*/
int main (void)
{
/*Declare and intialize variables*/
double temp, sumtemp = 0.0, con, sumcon = 0.0, tem_con, sumtem_con =
0.0, tsq, sumtsq = 0.0;
int i, ndata;
FILE *therm, *res;
/*Open files*/
therm = fopen (inputfile, "r");
res = fopen (outputfile, "w");
/*Print output headings*/
printf ("******************************************");
printf ("nTEMPERATURE vs THERMAL CONDUCTIVITY");
printf ("nby ");
printf ("nnTemp Conduct Temp Sqrd Temp*Conduct");
/* Verify input file and read control number */
// NOTE: unmatched 'else' followed, presuming a
// test for 'therm'
if (!therm)
{
printf ("nnnn ERROR OPENING INPUT FILE.");
printf ("nn PROGRAM TERMINATED.nnn");
return 0;
}
else
{
/* Read control number */
fscanf (therm, "%i", &ndata);
/* Compute temp and conductivity data and print results */
for (i = 1; i <= ndata; i++) {
fscanf (therm, "%lf %lf", &temp & con);
sumtemp = sumtemp + temp;
sumcon = sumcon + con;
sumtsq = sumtsq + pow (temp, 2);
sumtem_con = sumtem_con + temp * con;
printf ("n%3.0f %5.1f %6.1f %6.1f", sumtemp, sumcon, sumtsq,
sumtem_con);
fprintf (res, "%3.0f %5.1f %6.1f %6.1f", sumtemp, sumcon,
sumtsq, sumtem_con);
}
}
/*Close the input file*/
fclose (therm);
fclose (res);
/*Exit the program*/
return 0;
}
你能帮我诊断这个错误吗?
您错过了两个变量之间的逗号,并且 if 语句也丢失了。
fscanf(therm, "%lf %lf", &temp &con);
这应该是
fscanf(therm, "%lf %lf", &temp, &con);
应该有一个 if 语句。
/* Verify input file and read control number */