提问者:小点点

有人能指出我错在哪里吗?


我正试着用计算器计算我的平均成绩,但我似乎找不到我错在哪里。

#include <iostream>
#include <conio.h>
#include <cmath>

using namespace std;

int main()
{
  string subject[17] = {"Biology", "French"}; //"Sport", "Ed.Antreprenoriala", "Geografie", "Mate", "TIC", "Psihologie", "Romana", "Chimie", "Fizica", "Istorie", "Engleza", "Religie", "Info", "Desen", "Muzica"};
    int mark, numberOfMarks;
    float sumaNote = 0;
    double average, averageSum = 0, finalAverage;

    for(int i=0; i<2; i++){

        cout << subject[i] << ":" << "\n";
        cout << "How many marks?" << "\n";
        cin >> numberOfMarks;

        for(int j=0; j < numberOfMarks; j++){
            cin >> mark;
            sumaNote += mark;
        }
        average = sumaNote / numberOfMarks;

        averageSum += round(average);
    }

    cout << endl;

    finalAverage = averageSum/2;

    cout << finalAverage;

    getch();
    return 0;
    }

它应该是这样工作的:br>1)从字符串数组中取一个主语;2)询问我对这个主语有多少个标记;3)取标记的和的平均值;4)重复直到数组元素(主语)出来为止;5)取所有主语平均值的和,取平均值并输出答案;

***每个非自然平均值都需要向上或向下四舍五入(因此有round()函数);

***总共有17个被试,但我只使用了2个作为实验用途;

生物:多少分?2 8 10(它应该做(8+10)/2)法语:多少分?3 7 9 10(应为(7+9+10)/3

输出应为9+9/2=9

但我的代码没有这样做,也不知道为什么


共2个答案

匿名用户

您需要在循环中添加

匿名用户

关于编译:

    ;header不再使用,main函数末尾的getch()也不再使用。/li>
  1. 使用现代编译器,如CodeBlocks IDE,Repl Online Compiler,以避免使用过时的代码。/li>

程序:

  1. 添加了自动计算数组长度的变量。/li>
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    string subject[] = {"Biology", "French"}; //"Sport", "Ed.Antreprenoriala", "Geografie", "Mate", "TIC", "Psihologie", "Romana", "Chimie", "Fizica", "Istorie", "Engleza", "Religie", "Info", "Desen", "Muzica"};
    int mark, numberOfMarks;
    float sumaNote = 0;
    double average, averageSum = 0, finalAverage;

    // Find length of array 
    int len = sizeof(subject)/sizeof(subject[0]);

    // changed limit to len
    for(int i=0; i<len; i++) {

        cout << subject[i] << ":" << "\n";
        cout << "How many marks?" << "\n";
        cin >> numberOfMarks;

        for(int j=0; j < numberOfMarks; j++){
            cin >> mark;
            sumaNote += mark;
        }

        average = sumaNote / numberOfMarks;
        averageSum += round(average);

        sumaNote = 0; // important to reset sum
    }

    cout << endl;

    finalAverage = averageSum/len;

    cout << finalAverage;

    return 0;
}