如何在input_data函数中更正我的嵌套for循环?

时间:2017-04-27 22:36:16

标签: c++ file

我的问题是在input_data()函数中接受两个参数,infile和statistics_array。此函数读取(循环)五组学期期末考试数据,并填充一部分statistics_array。其中包括年份,成绩数,并将成绩数输入到考试类型结构I中的成绩数组中觉得循环是错误的,因为最后当我输出所有信息是错误的我的代码和文件内容是如何解决这个问题?

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
struct ExamType {
    string semester_name;
    int year;
    int num_grades;
    float average_grade;
    int grades_above_average;
    int grades_below_average;
    int grades_array[35];
};
void input_data(ExamType[], ifstream&);
void calculate_statistics(ExamType[]);
void print_results(ExamType[]);
void calculate_average(ExamType[], int);
void calculate_above_average(ExamType[], int);
void calculate_below_average(ExamType[], int);
int main()
{
    char make_type;
    ifstream myfile;
    ExamType statistics_array[5];
    myfile.open("infile.txt");
    input_data(statistics_array, myfile);
    calculate_statistics(statistics_array);
    print_results(statistics_array);
}

void input_data(ExamType statistics_array[], ifstream& myfile)
{
    int num_students;
    for (int i = 0; i < 5; i++) {
        myfile >> statistics_array[i].semester_name;
        myfile >> statistics_array[i].year;
        myfile >> statistics_array[i].num_grades;
        for (int j = 0; j < statistics_array[i].num_grades; j++) {
            statistics_array[j].grades_array[i];
        }
    }
}

void calculate_statistics(ExamType statistics_array[])
{
    for (int index = 0; index < 5; index++)
    {
        calculate_average(statistics_array, index);
        calculate_above_average(statistics_array, index);
        calculate_below_average(statistics_array, index);
    }

}
void calculate_average(ExamType statistics_array[], int index)
{

    float sum=0;
    float total_average;
    for (int i = 0; i < statistics_array[index].num_grades; i++)
    {

         sum=sum+statistics_array[index].grades_array[i];
    }

    total_average = sum / statistics_array[index].num_grades;
    statistics_array[index].average_grade = total_average;
}

void calculate_above_average(ExamType statistics_array[], int index)
{
    int count = 0;
    for (int i = 0; i < statistics_array[index].num_grades; i++)
    {

        if (statistics_array[index].grades_array[index] > statistics_array[index].average_grade) 
        {

            count++;
        }
        statistics_array[index].grades_above_average = count;
    }
}
void calculate_below_average(ExamType statistics_array[], int index)
{
    int count = 0;
    for (int i = 0; i < statistics_array[index].num_grades; i++)
    {

        if (statistics_array[index].grades_array[index] < statistics_array[index].average_grade)
        {

            count++;
        }
        statistics_array[index].grades_below_average = count;
    }

}
void print_results(ExamType statistics_array[])
{
    for (int i = 0; i < 5; i++)
    {
        cout << "Analysis of exams in fall" << " " << statistics_array[i].semester_name << statistics_array[i].year<<endl;
        cout << endl;
        cout << "The number of grades above average are" << " " << statistics_array[i].grades_above_average<<endl;
        cout << "The number of grades above average are" << " " << statistics_array[i].grades_below_average<<endl;


    }
}

输出:

Fall 2013 17
54
96
68
99
35
81
93
32
12
23
45
55
66
77
88
99
100
Spring 2014 10
99
48
22
41
66
80
91
55
83
92
Fall 2014 12
32
12
23
45
51
62
97
38
59
80
91
98
Spring 2015 20
93
41
60
80
90
55
88
99
32
12
23
45
55
66
77
88
99
100
99
98
Fall 2015 15
92
45
61
86
90
55
82
92
32
12
23
45
53
61
73

1 个答案:

答案 0 :(得分:1)

int j = 0; j < statistics_array[i].num_grades; j++) {
        myfile >> statistics_array[i].grades_array[j];
    }

input_data函数的嵌套循环代码应根据上述进行修改。