在正确循环代码和平均循环(主要停留在这个)C++ 的同时中断 do

Breaking do while looping code properly and averaging the loops (majorly stuck on this) C++

我已经放弃了这个作业一次,但我保存了旧代码,所以如果有什么可以挽救的,我会洗耳恭听。

我现在有一个 do while 循环,它会根据用户输入重复两个问题,而且效果很好。但我不知道如何将最终数字相加,更不知道如何正确计算它们的平均值,以便将它们转换为百分比。

#include<iostream>
#include<iomanip>

using namespace std;

int main() 
{
    //declare variables
    int n = 0;
    int counter = 0;
    double score = 0.0;
    double total = 0.0;
    double avg = 0.0;
    double scoreTotal = 0.0;
    double totalTotal = 0.0;


    //Prompt user for number of assignments
    cout << "How many assignments are there? ";
    cin >> n;

    counter = 0;
    do
    { 
        counter++;

        //prompt user for score    */count up for each query*/
        cout << "What is the score for assignment number " << counter << "? ";
        cin >> score;

        //prompt user for totals         */count up for each total*/
        cout << "What is the total score available for assignment number " << counter << "? ";
        cin >> total;

    }
    while(counter < n);

    //calculate averages

    scoreTotal += score;
    totalTotal += total;

    avg = ((scoreTotal / totalTotal) * 100) / n; 

    //output how much it was out of and percent

    cout << "Your total is " << scoreTotal << " out of " << totalTotal << ", or " <<  avg << "%" << endl;

    return 0;
}

我只是在 avg 部分进行猜测,结果显示不正确,所以我知道那是错误的。

我的旧代码让它正确地结束了循环周期,但它确实对新代码造成了很大的打击,例如将计数器更改为仅偶数,并且它输出的提示比用户要求的高。

#include<iostream>

using namespace std;

int main() 
{

    float s;  // assignment score
    float t;  // total points worth
    float p;  // percentage     
    int n;

    //input the number of assignments
    cout << "How many assignments are there? ";
    cin >> n;

        for (int i=1; i <=n; i++)
        {
            //input the total points earned for assignment
            cout << "What is the score earned for this assignment? ";
            cin >> s;

            //input the number of points assignment is worth
            cout << "How many points was the assignment worth? ";
            cin >> t;

            //calculate percentage
            p = ((s / t) * 100) ;
            p += i;
        }


    //output score
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout << "Total score: " << p << "%"<< endl;

    return 0;
}

我的教科书也没有帮助。它从解释 Do 和 Do While 循环跳到展示如何使用 For 循环,但没有解释它们的兼容性。

任何帮助!谢谢!

你的代码有点乱,但我有点明白你作业的要点了。

根据您的标题,您似乎在寻找用户输入的 X 作业总数。现在您只跟踪用户输入的最后分数和总分。

例如。 分数 = 5,总计 = 10 - 下一次迭代 分数 = 7,总分 = 20 - 5 和 10 被淘汰。

您需要做的是使用数组(这似乎超出了您的范围)来跟踪所有这些,因此我建议您将它们全部加起来并跟踪累积分数和总分。为此,您只需添加

scoreTotal += score;
totalTotal += total;  // Terrible name sorry

+= 的意思是 scoreTotal = score + scoreTotal ,它会记录累计总和。最后,您所要做的就是 scoreTotal/totalTotal 找到平均值。

最终代码如下:

#include<iostream>
#include<iomanip>

using namespace std;

int main() 
{
    //declare variables
    int n, counter;
    double score, total, scoreSum = 0, totalSum = 0, answer;


    //Prompt user for number of assignments
    cout << "How many exercises to input? ";
    cin >> n;

    counter = 0;
    do
        { 
        counter++;

        //prompt user for score    */count up for each query*/

        cout << "What is the score for exercise " << counter << "? ";
        cin >> score;

        //prompt user for totals         */count up for each total*/

        cout << "What is the total points possible for exercise " << counter << "? ";
        cin >> total;


        //calculate averages

        scoreSum += score;
        totalSum += total;

        answer = (scoreSum / totalSum) * 100; 

            }
        while ( counter < n);


    //output how much it was out of and percent

    cout << "Your total is " << scoreSum << " out of " << totalSum << ", or " << fixed << setprecision (2) << answer << "%" << endl;

    return 0;
}