【问题标题】:Read in Student info from a .txt file and then caluclate the average and find the letter grade从 .txt 文件中读取学生信息,然后计算平均值并找到字母等级
【发布时间】:2019-05-02 19:56:22
【问题描述】:

我正在开发这个程序来读取学生的名字、姓氏和 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';
        }
    }

}

【问题讨论】:

  • 文件的格式是什么?
  • 请参阅:如何创建最小、完整和可验证的示例。 stackoverflow.com/help/mcve这不仅仅是在这里提问的要求,它也是学习调试自己的代码的重要部分
  • 发帖前请先上网搜索。 StackOverflow 上已经有大量的学生成绩作业。搜索“C++ 学生成绩作业”。否则,请使用调试器。
  • 我们可以获取输入文件吗?或者至少是前两个学生。

标签: c++ arrays function vector struct


【解决方案1】:

让我们看看你的getData() 函数。定义为:

void getData(Student &students)

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

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

哦哦!这声明了一个新的students,它隐藏了参数students。所以当你做students.name时,你说的是局部变量,而不是参数。

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 2014-02-08
      • 1970-01-01
      相关资源
      最近更新 更多