C++ 如何使用EOF循环对文件中的每个学生进行迭代

C++ 如何使用EOF循环对文件中的每个学生进行迭代,c++,C++,如何设置循环以读取文件数据,直到达到eof 基本上,我有一个包含学生数据的数据文件,每个学生都有测验成绩、考试成绩、实验室成绩等 我有一个函数,它以字母等级的形式给出值 我的问题是设置循环,为每个学生计算每个函数4次。到目前为止,我的循环代码使用了一个计数器循环,但我应该将其转换为一个eof循环 我下面的代码在没有更多数据可读取时结束 int main() { int counters = 1; char lab; char assgniment_score; c

如何设置循环以读取文件数据,直到达到eof

基本上,我有一个包含学生数据的数据文件,每个学生都有测验成绩、考试成绩、实验室成绩等

我有一个函数,它以字母等级的形式给出值

我的问题是设置循环,为每个学生计算每个函数4次。到目前为止,我的循环代码使用了一个计数器循环,但我应该将其转换为一个eof循环

我下面的代码在没有更多数据可读取时结束

int main()
{
    int counters = 1;
    char lab;
    char assgniment_score;
    char quiz_score;
    char test_score;
    float final_total_average;
    myfile.open("infile.txt");
    while (counters <= 20) {
        lab = Compute_lab_grade();
        assgniment_score = Compute_Assignment_grade();
        quiz_score = Compute_Quiz_grade();
        test_score = Compute_test_grade();
        final_total_average = (10 * (total(quiz_score))
            + (25 * (total(assgniment_score)))
            + (30 * (total(lab))) + (35 * total(test_score))) / 100;
        cout << "Student " << counters << endl;
        cout << "Student Lab grade is : " << lab << endl;
        cout << "Student Assignment grade is : " << assgniment_score << endl;
        cout << "Student Quiz grade is : " << quiz_score << endl;
        cout << "Student Exam grade is : " << test_score << endl;
        cout << "Student Final grade is : " << final_average_score(final_total_average) << endl << endl;
        counters++;
    }
}
intmain()
{
int计数器=1;
煤焦实验室;
字符识别分数;
char测验分数;
char测验评分;
浮动最终总平均值;
myfile.open(“infle.txt”);

而(countersRational)的答案是:OP一下子朝着许多错误的方向前进,我认为其中一些可以用一个好的例子来阻止

问题1:EOF循环是一个神话。请阅读以下内容:

问题2:使用
char
表示数字。从技术上讲,
char
是一种整数类型,但它通常与单个字符关联,其输入/输出例程会相应重载。例如,如果给定输入“1234”
std::cin>>a_char;
将导致
char a_char
包含字符“1”,但是
std::cin>>a_int;
将导致
int an_int
包含数字1234。此外,使用字符表示数字通常会导致读者混淆,他们可能会误解您的代码是什么你可以这样做,有时这是一个合适的选择,但这次没有多大意义

问题3杂乱无章且未检查的IO。通常最好读入您想要的所有内容,然后,如果输入良好,立即对所有内容做出决定。如果学生的记录没有27个整数,则读取所有27个整数,并检查每个整数是否正确读取。如果您无法全部读取,则这是一个错误。如果e不是整数,这是一个错误。如果你有错误,学生不是有效的输入,应该被丢弃或更仔细地调查

问题4:不使用数据结构来包含相关数据。学生由一堆信息表示。使用可访问和操作信息的方法打包这些信息很方便。阅读封装

小唠叨:

现在我们开始:

#include <iostream>
#include <fstream>
#include <sstream>

// using namespace std; avoid using this. Can be very dangerous to the unwary.

// container class to aggregate student stats and provide manipulators to read
// and display a student
class Student
{
    static int count; // only the students use the counter. No need for it to be global.
    int student_number;
    // int telegraphs your intent much better than char
    // These variables are numbers, not characters and should be treated like them
    int lab;
    int assgniment_score;
    int quiz_score;
    int test_score;
    float final_total_average;

public:
    // simple input function. This is a bit too simple for OP, but should be
    // enough for OP to get the idea.
    friend std::istream & operator>>(std::istream & in, Student & student)
    {
        std::string line;
        student.student_number = count ++;
        if (std::getline(in, line))
        {
            std::stringstream stream(line);
            if (stream >> student.lab
                       >> student.assgniment_score
                       >> student.quiz_score
                       >> student.test_score)
            { // if we read all the variables we needed
                // divided by 4.0 because division of int by int will
                // give an int, not a float.
                student.final_total_average = (student.lab +
                        student.assgniment_score +
                        student.quiz_score +
                        student.test_score) / 4.0;
            }
            else
            { // failed to read. Mark stream bad.
                in.setstate(std::istream::failbit);
            }
        }

        return in;
    }

    // simple output function OP shouldn't have to change much here.
    friend std::ostream & operator<<(std::ostream & out, Student & student)
    {
        //endl is line feed and IO flush very expensive, so  use only
        // when you absolutely need to flush the stream. This turns out to be
        // almost enver.
        out << "Student " << student.student_number << '\n'
            << "Student Lab grade is : " << student.lab <<  '\n'
            << "Student Assignment grade is : " << student.assgniment_score <<  '\n'
            << "Student Quiz grade is : " << student.quiz_score <<  '\n'
            << "Student Exam grade is : " << student.test_score <<  '\n'
            << "Student Final grade is : " << student.final_total_average << '\n';
        return out;
    }
};

// allocate storage for student counter
int Student::count = 1;

int main()
{
    // create and open file. As with the counter, no need for it to be global 
    // because it can easily be passed by reference into functions that need it.
    std::ifstream myfile("infile.txt"); 
    Student student; // allocate a dummy student to read into and print

    // because we have smart rules for reading in a student, this is easy.
    while (myfile >> student) // read students until error or end of file
    {
        std::cout << student; // write the current student
    }
}
#包括
#包括
#包括
//使用名称空间std;避免使用此名称空间。对于粗心的人来说可能非常危险。
//容器类来聚合学生统计信息,并提供要读取的操纵器
//展示一个学生
班级学生
{
static int count;//只有学生使用计数器。不需要全局计数器。
国际学生联合会编号;
//int电报你的意图比char好得多
//这些变量是数字,而不是字符,应该像它们一样对待
国际实验室;
国际认可分数;
智力测验成绩;
智力测验成绩;
浮动最终总平均值;
公众:
//简单的输入函数。这对于OP来说有点太简单了,但应该是
//足以让OP得到这个想法。
friend std::istream&operator>>(std::istream&in,学生和学生)
{
std::字符串行;
student.student_number=count++;
if(std::getline(in,line))
{
std::stringstream(行);
如果(流>>学生实验室
>>学生协会分数
>>学生测验分数
>>学生。考试(分数)
{//如果我们读取所有需要的变量
//除以4.0,因为int除以int将
//给出一个int,而不是float。
student.final\u total\u average=(student.lab+
学生协会分数+
学生测验分数+
学生。考试分数)/4.0;
}
其他的
{//读取失败。请将流标记为错误。
in.setstate(std::istream::failbit);
}
}
返回;
}
//简单的输出函数OP在这里不需要做太多更改。

friend std::ostream&operator我们需要查看文本输入文件的结构。您可能需要重复调用fgets()和sscanf(),每行将有一名学生。标点符号和正确的语法极大地提高了文本的可读性。试试看!我没有看到任何代码从您的文件中读取。它在测验成绩函数中执行。他们都从文件中读取,因为我将文件设置为全局变量我的问题如何将其设置为fil的结尾通过调用函数打印学生成绩数据来循环读取: