C++循环变量按另一个变量递增
C++ for loop variable increment by another variable
我一直在研究一个简单的成绩计算器,我在"实验室"选项吐出一个糟糕的结果时遇到了麻烦,我正在寻找十进制百分比,但我不断得到一个非常非常大的指数数字。
我遇到问题的特定部分是calclabavg((函数 - 确切地说是for循环。
我不是在要求一个确切的解决方案,我只是想被指出正确的方向,以便我可以自己解决问题。
提前非常感谢:)
float calclabavg(){//funciton that calculates user lab averages
float x, vary, pointspos, sumpointspos, sumearned;
cout << "How many labs in labs?" << endl;
cin >> x;
float totalpoints = 0;
cout << "Do the points vary per lab? (Press 1 for yes, 0 for no)" << endl;
cin >> vary;
if (vary == 0){
cout << "How many points were possible on each lab?" << endl;
cin >> pointspos;
sumpointspos = x * pointspos;
}
for (int i = 0; i < x; i++){
float temp;
cout << "What was your score on lab " << i + 1 << endl;
cin >> temp;
sumearned += temp;
if (vary == 1){
cout << "How mant points possible on lab " << i + 1 << endl;
float pointsposvary;
cin >> pointsposvary;
sumpointspos += pointsposvary;
}
//pointspos = pointspos + temp;
}
return sumearned / sumpointspos;
}
好像你没有初始化sumearned
.calclabavg
开头的语句sumearned = 0.0;
至少会让你更进一步。顺便说一句:同时初始化其他变量是一种很好的做法,可以使代码更稳定。
相关文章:
- 如何从另一个文件继承私有成员变量和公共函数
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 一个变量的输入值也会保存到另一个变量中
- 从另一个 cpp 文件更改结构内、映射键内的变量
- 在另一个函数 (c++) 中调用变量
- 将双精度变量设置为另一个变量的值
- 为什么从另一个构造函数内部调用C++构造函数不修改类变量?
- 我有一个类,它创建了另一个类的实例.如何将变量通过第一个类传递到第二个类的实例化中?
- 为什么我可以使用 memcpy 将一个对象变量复制到另一个对象变量
- 为什么一个变量获得与另一个值相同的值
- 类中的一个变量显示,但另一个不显示
- c++问题:给一个变量赋值后,另一个变量发生了变化
- 在一个子程序中输入的变量用于C++中的另一个子程序
- (C++) 从另一个类访问变量时值无效
- 我如何将一个变量与另一个变量进行比较,例如我想如果(var1 > var2 x 1),然后执行此 c++
- 如何在一个函数中定义一个变量,并在另一个函数中访问和更改它?(C++)
- 如果一个变量在它之前释放了另一个(相同的数据类型)变量,如何将其分配给内存?
- 将变量从主窗口传递到另一个qt c++窗体
- memcpy() 在一个类中被调用以复制到另一个类变量中后会引发异常
- 将 int 转换为字符串,然后连接另一个变量以创建完整扩展名,然后将其转换为 const_char*