[英]the float variable doesn't store a floating value
struct score
{
int math;
int science;
int english;
int history;
int mapeh;
int tle;
int filipino;
};
struct info
{
string firstName;
string lastName;
score grades;
float average;
int studentnum;
int rank;
bool remark;
};
here is my code这是我的代码
the value stored in the subjects is:存储在主题中的值是:
85 87 89 90 94 92 95
respectively,分别,
vector<info> stud;
for( int i=0; i<stud.size(); i++)
{
stud[i].average= (stud[i].grades.math
+stud[i].grades.science
+stud[i].grades.english
+stud[i].grades.history
+stud[i].grades.mapeh
+stud[i].grades.tle
+stud[i].grades.filipino)/7;
}
the value stored in stud[i].average should be 90.28 but instead it is only 90.存储在stud[i].average 中的值应该是90.28,但它只是90。
what have i missed?我错过了什么? i have tried using the debugger and it shows me 90 (no decimals)
我试过使用调试器,它显示我 90(没有小数)
using setprecision it will only show me 90.00 not 90.28使用 setprecision 它只会显示我 90.00 而不是 90.28
i have also tried changing the data type for the subjects to float, and it didnt work.我还尝试将主题的数据类型更改为浮动,但没有奏效。
thank you.谢谢你。
Try changing to this:尝试更改为:
stud[i].average= (stud[i].grades.math
+stud[i].grades.science
+stud[i].grades.english
+stud[i].grades.history
+stud[i].grades.mapeh
+stud[i].grades.tle
+stud[i].grades.filipino)/7.0;
(int)/(float)
will give you a (float)
result. (int)/(float)
会给你一个(float)
结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.