break语句终止循环
#include <stdio.h>#define sum 100000
int main()
{
float amount,aver,toal;
int i;
for(i = 1,total = 0;i<=1000;i++)
{
printf("please enter amount: ");
scanf("%f",&amount);
total = total +amount;
if(total>=sum) break;
}
aver = total/i;
printf("number = %d\naver = %d10.2f\n",i,aver);
return 0;
}
例题 照着书上打的,编译后出现问题。
--------------------Configuration: 061 - Win32 Debug--------------------
Compiling...
061.c
E:\c++\Microsoft Visual Studio\MyProjects\061\061.c(8) : error C2065: 'total' : undeclared identifier
E:\c++\Microsoft Visual Studio\MyProjects\061\061.c(12) : warning C4244: '=' : conversion from 'float ' to 'int ', possible loss of data
E:\c++\Microsoft Visual Studio\MyProjects\061\061.c(15) : warning C4244: '=' : conversion from 'int ' to 'float ', possible loss of data
执行 cl.exe 时出错.
061.obj - 1 error(s), 0 warning(s)
怎么解决 本帖最后由 baige 于 2020-8-25 23:35 编辑
#include <stdio.h>
#define sum 100000
int main()
{
float amount,aver,total;// total写错了
int i;
for(i = 1,total = 0;i<=1000;i++)
{
printf("please enter amount: ");
scanf("%f",&amount);
total = total +amount;
if(total>=sum) break;
}
aver = total/i;
printf("number = %d\naver = %10.2f\n",i,aver);// d去掉
return 0;
} 本帖最后由 sunrise085 于 2020-8-25 23:35 编辑
第六行定义的时候拼写错误
total写成了toal
页:
[1]