C++循环变量按另一个变量递增

C++ for loop variable increment by another variable

本文关键字:变量 另一个 循环 C++      更新时间:2023-10-16

我一直在研究一个简单的成绩计算器,我在"实验室"选项吐出一个糟糕的结果时遇到了麻烦,我正在寻找十进制百分比,但我不断得到一个非常非常大的指数数字。

我遇到问题的特定部分是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;至少会让你更进一步。顺便说一句:同时初始化其他变量是一种很好的做法,可以使代码更稳定。

相关文章: