注册 登录
编程论坛 C++教室

[求助]C++程序不能运行?请指教

stephenliu 发布于 2007-08-10 22:06, 439 次点击

#include <iostream.h>
#include <iomanip.h>

int main ()
{
int total, gradeCounter, grade, max;
float average;
total = 0;
gradeCounter = 1;
/* cout << "Enter the max number of the students:";
cin >> max; */
while ( grade >=0 )
{
cout << gradeCounter <<" Enter grade: ";
cin >> grade;
total = total + grade;
gradeCounter ++;
}
if ( gradeCounter != 0 )
{
average = static_cast<float>(total) / gradeCounter;
cout << "The class average is: " << setprecision( 2 )
<< setiosflags ( ios::fixed | ios ::showpoint )
<< average << endl;
}
else cout << "No grades were entered" <<endl;
return 0;
}

运行是总是提示static_cast<float>错误,不知道怎么回事,请指教

2 回复
#2
福尔摩斯2007-08-10 22:11
static_cast<float> 是什么函数?

while ( grade >=0 )
{
cout << gradeCounter <<" Enter grade: ";
cin >> grade;
total = total + grade;
gradeCounter ++;
}
这个循环体有错误

grade没有初值,怎么能判断 grade >=0 呢?

而后你又 cin >> grade;

这不是很矛盾吗?
#3
terisevend2007-08-11 01:54
static_cast<float> 好像是类型强制转换...
对于LZ的代码,我看到的错误有:
(1).没有使用命名空间
(2).grade没有赋初值, 所以第一次判断grade >= 0 时, 是有可能无法进入while循环的...如果不想赋值,
建议使用do-while...

LZ你说, static_cast<float>(grade)这有错误, 那么LZ你可以换成float(grade)来调试试下...
1