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

时间:2018-11-30 16:21:37

标签: c++ arrays function vector struct

我正在研究此程序,以读取学生的名字,姓氏和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';
        }
    }

}

2 个答案:

答案 0 :(得分:-1)

让我们看一下您的getData()函数。定义为:

void getData(Student &students)

由于返回类型为void,因此我猜测您可能会传入Student,然后在函数中对其进行修改。但是,您可以这样做:

Student students;
getline(fileIn, students.name);

哦,哦!这将声明一个新的students,该阴影将覆盖参数students。因此,当您进行students.name时,就是在谈论局部变量,而不是参数。

杀死新的声明和事物应该像您期望的那样工作!

答案 1 :(得分:-1)

第一件事-您的代码应具有更好的结构!

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(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();
}