运行此程序时的垃圾数据(学生记录程序)

时间:2017-01-11 18:31:48

标签: c++

首先,这是我自己的代码,而不是复制。

该程序应该从“grades.txt”文件中读取学生数据,该文件包含测验,决赛等标记并计算平均值,分配成绩,并写出已经通过但未通过的学生的姓名和成绩.txt“和”fail.txt“。 现在我面临的问题是,学生的总数是57.该计划确实适用于最多39名学生,但之后总是垃圾值:(

#include <iostream>
#include <string>
#include <cmath>
#include <fstream>
#include <cstdlib>


using namespace std;
int counter; //counts the number of students

struct STUDENT
{
    string firstname;
    string lastname;
    long idnumber;
    int quiz1;   //10%
    int quiz2;   //10%
    int mid1;   //15%
    int mid2;   //15%
    double homework;   //10%
    double Final;      //40%
    double average; //to be calculated
    char grade;  //to be calculated
};
void Stcounter()
{

    ifstream thing ("Grades.txt");
    string dummy;
    getline(thing,dummy);                    //for skipping the first line

    string line;
    int b=0;
    while(getline(thing, line))
        b++;
        counter=b;
        cout<<"Total number of students is  "<< counter<<endl;
}
void read( STUDENT record[57])
{
    float a;
        ifstream thing("Grades.txt");
string d;
getline(thing,d);

     for(int i=0;i<56;i++)
    {
      if( thing>> record[i].idnumber>>record[i].firstname>>record[i].lastname>>record[i].quiz1>>record[i].quiz2>>record[i].mid1>>record[i].mid2>>record[i].homework>>record[i].Final)

   a=(record[i].quiz1/10)+(record[i].quiz2/10)+((record[i].mid1*15)/100)+((record[i].mid2*15)/100)+(record[i].homework/10)+((record[i].Final*40)/100);
   record[i].average=a;
   if(a>=85)
   {
       record[i].grade='A';

   }
   else if(a>=70)
   {
       record[i].grade='B';

   }
   else if(a>=55)
   {
       record[i].grade='C';

   }
   else if(a>=40)
   {
       record[i].grade='D';

   }
   else
   {
       record[i].grade='F';

   }
    }

}
void  WriteToFile (STUDENT record[57])
{
    ofstream p, f;
    p.open("Pass.txt");
    f.open("Fail.txt");
    for (int i = 0; i <= 56; i++)
    {
        if (record[i].average >= 40)
        {
            p << record[i].idnumber << " " << record[i].firstname << " " << record[i].lastname << "  passed with grade " << record[i].grade <<endl;
        }
        else
            f << record[i].idnumber << " " << record[i].firstname << " " << record[i].lastname << "  has failed"<<endl;
    }
    p.close();
    f.close();
}
//This function counts how many students got a certain grade
void gradecount(STUDENT record[57])
{
    int c1=0,c2=0,c3=0,c4=0,c5=0;
    for(int i=0;i<57;i++)
    {
        if (record[i].grade=='A')
            c1++;
        else if(record[i].grade=='B')
            c2++;
        else if(record[i].grade=='C')
            c3++;
            else if(record[i].grade=='D')
            c4++;
            else
                c5++;

    }
    cout<<c1<<" students got A"<<endl;
    cout<<c2<<" students got B"<<endl;
    cout<<c3<<" students got C"<<endl;
    cout<<c4<<" students got D"<<endl;
    cout<<c5<<" students got F"<<endl;
}


int main()
{
    STUDENT record[57];
    read(record);
    WriteToFile (record);
    Stcounter();
    gradecount(record);
return 0;
}

1 个答案:

答案 0 :(得分:0)

如果你没有注意到你的if语句

float a;
if(beggar>>record[i].idnumber>>record[i].firstname>>record[i].lastname>>record[i].quiz1>>record[i].quiz2>>record[i].mid1>>record[i].mid2>>record[i].homework>>record[i].Final)
    a=(record[i].quiz1/10)+(record[i].quiz2/10)+((record[i].mid1*15)/100)+((record[i].mid2*15)/100)+(record[i].homework/10)+((record[i].Final*40)/100);
record[i].average=a;

它给你一个错误,你使用变量'a'而不进行初始化。因此,当您将a声明为float时,将其指定为0或其他