C++ 从.txt文件中读取学生信息,然后计算平均值并找到字母等级

C++ 从.txt文件中读取学生信息,然后计算平均值并找到字母等级,c++,arrays,function,vector,struct,C++,Arrays,Function,Vector,Struct,我正在做这个项目,阅读学生的名字,姓氏和5个年级,并将学生信息放入一个结构中。从那里我尝试使用其他函数来查找平均等级、字母等级、最大等级和最小等级。我在阅读和存储学生信息,然后调用其他函数中的学生信息来计算平均值、字母分数等的正确方法上有问题。。。我的“displayAverages”函数没有列出任何名字,成绩是巨大的负数。如果你能帮我(在你看我的代码的头疼消失后)我会很感激 #include "pch.h" #include <iostream> #include <str

我正在做这个项目,阅读学生的名字,姓氏和5个年级,并将学生信息放入一个结构中。从那里我尝试使用其他函数来查找平均等级、字母等级、最大等级和最小等级。我在阅读和存储学生信息,然后调用其他函数中的学生信息来计算平均值、字母分数等的正确方法上有问题。。。我的“displayAverages”函数没有列出任何名字,成绩是巨大的负数。如果你能帮我(在你看我的代码的头疼消失后)我会很感激

#include "pch.h"
#include <iostream> 
#include <string> 
#include <fstream>
#include <iomanip>
#include <cmath> 
#include <vector>
#include <sstream>

using namespace std;

// Global variables
const int MAX_STUDENTS = 22;
const int MAX_GRADES = 5;
const string FILENAME = "NamesGrades.txt";

struct Student{
    string name;
    double grades[MAX_GRADES];
    double average;
    int max;
    int min;
}students[MAX_STUDENTS];
char getLetterGrade(double grade);

void getData(Student &students)
{
    ifstream fileIn;
    int numStudents = 0;

    fileIn.open(FILENAME.c_str());
    if (fileIn.fail())
    {
        cout << "Could not open file" << endl;
        system("PAUSE");
        exit(1);
    }

    while (fileIn) {
        for (int i = 0; i < MAX_STUDENTS; i++)
        {
            Student students;
            getline(fileIn, students.name);
            for (size_t i = 0; i < MAX_GRADES; i++)
            {
                fileIn >> students.grades[i];
            }
            return;
        }
    }

    fileIn.close();

    return;
}



void displayAverages(Student students) {
    double total;
    //double average;
    int maxLength = 50;

    cout << setprecision(1) << fixed << showpoint;

    // Providing a header
    cout << "\n\nGrade Averages\n";
    cout << setw(maxLength + 1) << left << "Name" << setw(4) << right <<

        "Average" << setw(6) << "Grade" << endl;

    for (int i = 0; i < 22; i++)
    {
        cout << setw(maxLength + 1) << left << students.name;
        total = 0;

        for (int j = 0; j < MAX_GRADES; j++)
        {
            total += students.grades[i];
        }

        students.average = (double)total / MAX_GRADES;

        cout << setw(7) << right << students.average << setw(6) <<

            getLetterGrade(students.average) << endl;
    }
}


char getLetterGrade(double grade) {
        {
        if (grade > 90) {
            return 'A';
        }
        else if (grade > 80) {
            return 'B';
        }
        else if (grade > 70) {
            return 'C';
        }
        else if (grade > 60) {
            return 'D';
        }
        else {
            return 'F';
        }
    }

}
#包括“pch.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//全局变量
const int MAX_学生=22;
const int MAX_GRADES=5;
常量字符串FILENAME=“NamesGrades.txt”;
结构学生{
字符串名;
双级[最高等级];
双倍平均;
int max;
int-min;
}学生[MAX_学生];
字符级(双级);
void getData(学生和学生)
{
ifstreamfilein;
int numStudents=0;
fileIn.open(FILENAME.c_str());
if(fileIn.fail())
{
学生成绩[i];
}
回来
}
}
fileIn.close();
回来
}
平均成绩(学生){
双倍总数;
//双倍平均;
int maxLength=50;

cout让我们看看您的
getData()
函数。它的定义如下:

void getData(Student &students)
由于返回类型为
void
,我猜您可能会传入一个
Student
,然后在函数中修改它。但是,您会:

Student students;
getline(fileIn, students.name);
噢!这声明了一个新的
students
,它对参数
students
进行阴影处理。因此,当您执行
students.name
时,您所指的是局部变量,而不是参数


杀死那个新的声明,事情应该像你期望的那样工作!

第一件事-你的代码应该有更好的结构

void getData(Student &students)
{
    ifstream fileIn;
    int numStudents = 0;// you are not using this variable

    fileIn.open(FILENAME.c_str());
    if (fileIn.fail())
    {
        cout << "Could not open file" << endl;
        system("PAUSE");
        exit(1);
    }
    while (fileIn) {
        for (int i = 0; i < MAX_STUDENTS; i++)//you should use auto here
        {
            Student students;//here you are making local object instead of changing the data off passed argument, you should get rid of this
            getline(fileIn, students.name);
            for (size_t i = 0; i < MAX_GRADES; i++)//you should use auto here
            {
                fileIn >> students.grades[i];
            }
            return;//you will return from you function after reading first student data so you should get rid of this
        }
    }
    fileIn.close();
    return;
}
void getData(学生和学生)
{
ifstreamfilein;
int numStudents=0;//您没有使用此变量
fileIn.open(FILENAME.c_str());
if(fileIn.fail())
{
学生成绩[i];
}
return;//在读取第一个学生的数据后,您将从函数返回,因此您应该去掉这个
}
}
fileIn.close();
回来
}
更改后:

void getData(Student &students) {
    ifstream fileIn;
    fileIn.open(FILENAME.c_str());
    if(fileIn.fail()) {
        cout << "Could not open file" << endl;
        system("PAUSE");
        exit(1);
    }
    while(fileIn) {
        for(auto i = 0; i < MAX_STUDENTS; i++) {
            getline(fileIn, students.name);
            for(auto i = 0; i < MAX_GRADES; i++)
                fileIn >> students.grades[i];
        }
    }
    fileIn.close();
}
void getData(学生和学生){
ifstreamfilein;
fileIn.open(FILENAME.c_str());
if(fileIn.fail()){
学生成绩[i];
}
}
fileIn.close();
}

文件的格式是什么?请参阅:如何创建最小、完整且可验证的示例。这不仅仅是在这里提问的要求,也是学习调试自己代码的一个有价值的部分。请在发布前搜索互联网。StackOverflow上已经有过多的学生成绩作业。搜索“C++学生成绩作业”。否则,使用调试器。我们可以获取输入文件。或者至少是前两个学生。