C编程[错误]预期声明或输入结束时的语句
问题描述:
我有以下代码。但是,当我尝试编译它时,我会在输入错误结束时得到预期的声明或声明。 你能告诉我为什么我得到这个错误?C编程[错误]预期声明或输入结束时的语句
/* File steelclass.c
This program reads a yield tensile strength from the file steelclassin.txt, calls the
function classfunc to determine the corresponding class and then the main program writes
the strength and corresponding class to a file steelclassout.txt. This continues until the
end of the input file is reached.
*/
#include<stdio.h.>
char classfunc(float s);
int main (void)
{
float str;
char cls;
FILE * fin = fopen("steelclassin.txt", "r");
FILE * fout = fopen("steelclassout.txt", "w");
fprintf(fout, "Tensile strength Class\n");
fprintf(fout, "------------------------\n");
while (fscanf(fin, "%f", &str)!= EOF){
cls = classfunc(str);
fprintf(fout, "%f %c\n", str, cls);
fclose(fout);
system("steelclassout.txt");
return 0;
}
char classfunc(float s)
{
char myClass;
if(s >= 1000.0){
myClass = 'A';
}else if(s >= 800.0){
myClass = 'B';
}else if (s >= 600.0){
myClass = 'C';
}else if (s >= 400.0){
myClass = 'D';
}else{
myClass = 'E';
}
return myClass;
}
答
/* Where is the closing } for this opening bracket?!?
|
V
*/
while (fscanf(fin, "%f", &str)!= EOF){
cls = classfunc(str);
fprintf(fout, "%f %c\n", str, cls);
如果这正是你试图编译代码,您必须在同时范围末尾缺少一个右括号。另外,你应该在使用之前检查'fin'和'fout'。请注意,如果'fscanf'无法读取浮点数,则返回0. –
使用代码格式化程序/自动注册器可以轻松避免这样的问题。如果你的编辑器/ IDE无法做到(即使有一些插件),请使用其他工具/编辑器来完成它,并考虑切换。 – hyde
'#include'看起来不正确。 –
wildplasser