0

以下のコードは、C でファイルを読み取ります。ファイル、平均スコア、最大スコア、および最大スコアを獲得したすべての学生の名前が表示されます。試験のスコア (小数点以下 1 桁までの 0-100 形式で、列のフィールド幅を使用) は配列に保存され、名前 (名前と姓は 15 文字に制限されています) は文字の 2 次元配列に保存されます。スコア配列と並列。私の問題は次のとおりです。

1)コードがファイルを正しく読み取り(印刷)しません(fscanfと配列に関連していると思います)。

2) 私の 2 つの関数は結果を出力しません。

どんな提案でも大歓迎です、ありがとう。

#include "tools.h"
#define MAX 30                  // Maximum number of students
int computeMax(double stSco[], int numSt);  // Gets the average and highest
                                            // score
void outputBest(double num[], char nameHg[][15], int hgPl, int totStu);

int main()
{
    double score[MAX];
    char name[MAX][15];
    char fileName[80];
    int k, count = 0, hgCt;
    stream dataFile;
    banner();
    printf("Type the name of file you want to read\n");
    scanf("%79[^/n]", fileName);
    dataFile = fopen(fileName, "r");
    if (dataFile == NULL)
    {
        fatal("Cannot open %s for input", fileName);
    }
    while (!feof(dataFile))
    {
        fscanf(dataFile, "(%lg,%s)", &score[k], &name[k]);
        printf("%6.1f %s\n", score[k], name[k]);
        count++;                // It counts how many students there are
    }
    hgCt = computeMax(score, count);    // Stores the value sent by the
                                        // function
    outputBest(score, name, hgCt, count);
    fclose(dataFile);
    bye();
    return 0;
}

int computeMax(double stSco[], int numSt)
{
    int k, maxScore = 0, sum = 0;
    double maximum = 0, average = 0;

    for (k = 0; k < numSt; k++)
    {
        sum += stSco[k];        // It sums all scores
        if (stSco[k] > maximum)
        {
            maximum = stSco[k];
            maxScore = k;       // Stores the index of the maximum score
        }
    }
    average = sum / numSt;
    printf("The average score is %d\n", average);
    printf("The maximum score is %d\n", maximum);
    return maxScore;
}

void outputBest(double num[], char nameHg[][15], int hgPl, int totStu)
{
    int k;
    for (k = 0; k < totStu; k++)
    {
        if (num[k] = hgPl)
        {                       // It finds who has the highest score
            printf("%s got the highest score\n", nameHg[k]);
        }
    }
}
4

1 に答える 1