简体   繁体   English

使用从文件读取的结构的函数没有输出吗?

[英]Functions using structure read from file return no output?

I am supposed to declare a structure that holds the name and grades of an unknown number of students. 我应该声明一个结构,其中包含数量未知的学生的姓名和成绩。 Here is how I did it: 这是我的做法:

struct student {
    char name[30];
    int grade1, grade2, grade3;
};

Then, I have a text file containing the information for each student on different lines. 然后,我有一个文本文件,其中包含不同行上每个学生的信息。 I read it this way. 我这样阅读。

student info[20];
int nr_students,i=0,k=0;
fstream inFile;
inFile.open("in.txt");
fstream outFile;
outFile.open("out.txt");

while(!EOF) {
    inFile>>info[i].name;
    inFile>>info[i].grade1;
    inFile>>info[i].grade2;
    inFile>>info[i].grade3;
    i++;

}
nr_students=i;
avg_promovability(outFile,info, nr_students);

I do not think this works properly, since the nr_students remains 0, apparently. 我认为这不能正常工作,因为nr_students显然保持为0。 Also, the first function: 另外,第一个功能:

void avg_promovability(fstream &outFile,student *info, int nr_students)
{
    int i;
    float sum=0,j=0,avg;
    for (i=0;i<nr_students;i++)
        if(info[i].grade1 >=5 && info[i].grade2>=5 && info[i].grade3>=5 ) {
            sum=sum + info[i].grade1 + info[i].grade2 + info[i].grade3;
            j++;
        }
    avg=sum/(3*j);
    outFile<<avg;

}

It's supposed to calculate the average grade of the students that have passed (that is, all their grades are >=5). 应该计算通过的学生的平均成绩(即,所有成绩均> = 5)。 It doesn't really do it, the out.txt file is always empty. 它并没有真正做到,out.txt文件始终为空。 Any suggestions as to what I'm doing wrong? 关于我在做什么错的任何建议吗?

That is not the way you check for the end of the file. 这不是检查文件末尾的方式。 Try 尝试

while (inFile>>info[i].name) {
     inFile>>info[i].grade1;
     inFile>>info[i].grade2;
     inFile>>info[i].grade3;
     i++;
}

The core problem of not reading the student info has been pointed out by the answer by @VaughnCato. @VaughnCato的答案指出了不阅读学生信息的核心问题。

Additionally... 另外...

Since you are using C++, I think you should use an std::vector<student> to hold the information of all students. 由于您使用的是C ++,我认为您应该使用std::vector<student>来保存所有学生的信息。 Then, you don't need to worry about counting the number of students and keeping track of them in nr_students . 然后,您不必担心要计算学生人数并在nr_students跟踪他们。

Then you can change the signature of avg_promovability from 然后,您可以更改avg_promovability的签名

void avg_promovability(fstream &outFile,student *info, int nr_students) 

to

void avg_promovability(fstream &outFile, std::vector<student> const& info) 

I would recommend overloading the extraction operator, >>, and assignment operator, =, to take in the name and three grades of the temporary student and assign it to the array. 我建议重载提取运算符>>和赋值运算符=,以输入临时学生的姓名和三年级并将其分配给数组。

Then.. 然后..

while(inFile >> student) {
   info[i] = student;
   i++;
}

This will ensure there are no errors that slip by. 这将确保不会遗漏任何错误。 This is probably better practice and will make debugging easier in case of seg faults. 这可能是更好的做法,如果出现段错误,将使调试更加容易。

erip erip

Try to replace this line of code 尝试替换此行代码

while(!EOF) {

with this 有了这个

while(infile!=EOF) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM