C++ 从文本文件读入类中的对象向量-写入另一个txt文件

C++ 从文本文件读入类中的对象向量-写入另一个txt文件,c++,C++,我已经更改了代码,现在程序正确地读取了students.txt文件,但仍然是根据用户输入而不是students.txt计算的 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 using namespace std; //Declaring class Student class Student { private: string newName; int newScore1; int newScore2; int newScore3; float

我已经更改了代码,现在程序正确地读取了students.txt文件,但仍然是根据用户输入而不是students.txt计算的
#包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括

using namespace std;

//Declaring class Student   

class Student
{
private:

  string newName;
  int newScore1;
  int newScore2;
  int newScore3;
  float newFinal;

public:
  // No argument constructors
  Student ()

 {
  newName     = " ";
  newScore1   = 0;
  newScore2   = 0;
  newScore3   = 0;
  newFinal    = 0.00;

 }
 //all arguements constructors
 Student (string name, int score1, int score2, int score3, float final)

 {
 newName     = name;
 newScore1   = score1;
 newScore2   = score2;
 newScore3   = score3;
 newFinal      = final;
 } 
 //getters
 string getName() const
 {
  return newName;
 }
 int getScore1() const
 {
 return newScore1;
 }
 int getScore2()const
 {
 return newScore2;
 }
 int getScore3()const
 {
 return newScore3;
 }

 float getFinal () const
 {
  return newFinal;
 }

 //Setters
 void setName(string name)
 {
  newName = name;
 }
 void setScore1(int score1)
 {
 newScore1=score1;
 }
 void setScore2(int score2)
 {
  newScore2=score2;
 }
 void setScore3 (int score3)
 {
 newScore3 = score3;
 }

 void setFinal (float final)
 {
    newFinal = final;
 }


};
//asks for number of students, 
// function asks for input to fill in vector
//sorts the inputs to get max 2 scores out of 3 
//puts the data in a vector of class Student
//Sends data to a text file students.txt
 void fillvector(vector <Student>& newMyClass)

{
 string name;
 float score1;
 float score2;
 float score3;
 float final;
 float tmp;
 cout << "Please enter the number of Students: " << endl;
 int classSize;
 cin >> classSize;
 for (int i = 0; i < classSize; i ++)
   {
   cout << "Enter Student's name" << endl;
   cin >> name;
   cout << "Enter Student's Score 1" << endl;
   cin >> score1;
   cout << "Enter Student's Score 2" << endl;
   cin >> score2;
   cout << "Enter Student's Score 3" << endl;
   cin >> score3;
   if(score1>score2)
     {
      float tmp = score1;
      score1 = score2;
      score2 = tmp;
     }
   if(score1>score3)
     {
     float tmp = score1;
     score1=score3;
     score3 = tmp;
     }
   if(score2>score3)
    {
     float tmp = score2;
     score2=score3;
     score3=tmp;
    }

  final = (score2+score3)/2;

  Student newStudent (name, score1, score2, score3, final);
  newMyClass.push_back(newStudent);
  cout << endl;

  ofstream myfile;
  myfile.open ("students.txt", std::ofstream::out |std::ofstream::app );
  myfile << name<< setw(5)<< score1<< setw(5)<<score2<<setw(5)  <<score3<<setw(5)<<final<<setw(5)<<endl;
  myfile.close();
  cout << "Copied to students.txt" << endl;


   }
    cout << endl;
}
//reads data from textfile students.txt
//calculated teh minimum scores and maximum scores
//sends the minimum and maximum scores to text file Results.txt
void readToVector(vector <Student>& newMyClass)

{
    string name;
float score1;
float score2;
float score3;
float finalScore;
Student newStudent (name, score1, score2, score3, finalScore);
unsigned int size = newMyClass.size();
ifstream fin("students.txt");
if (fin.is_open())
  {cout << "File open" << endl;
    while(fin >> name >> score1 >> score2 >> score3 >> finalScore)
      {
    newStudent.setName(name);
    newStudent.setScore1(score1);
    newStudent.setScore2(score2);
    newStudent.setScore3(score3);
    newStudent.setFinal(finalScore);
    newMyClass.push_back(newStudent);
    //cout << newStudent.getName() << newStudent.getFinal() << endl;

      }
     fin.close();
    cout << endl;
    Student studWMaxScore  = newMyClass[0];
    float maxscore = studWMaxScore.getFinal();

for (unsigned int i =0; i < size; i++) 
{
    if (maxscore < newMyClass[i].getFinal())
  { 
   maxscore = newMyClass[i].getFinal();
   studWMaxScore = newMyClass[i];

  }

}
cout << "Maximum Score is " << maxscore << endl;
  ofstream myfile;
   myfile.open ("Result.txt", std::ofstream::out  );
   myfile << "Maximum Score" << endl;
   myfile << maxscore << endl;
   myfile << "Name of the student with maximum score is " <<  endl;
   myfile << studWMaxScore.getName() << endl <<  endl; 
   // myfile.close();
   cout << "Copied to Results.txt" << endl; 

Student stuWMinScore = newMyClass[0];
float minscore = stuWMinScore.getFinal();
for (unsigned int i =0; i < size; i++)
{
  if (minscore > newMyClass[i].getFinal())
    {
      minscore = newMyClass[i].getFinal();
      stuWMinScore = newMyClass[i];

    }   
}
cout << "Minimum Score is " << minscore << endl; 
// ofstream myfile;
//  myfile.open ("Result.txt", std::ofstream::out );
 myfile << "Mimimum Score" << endl;
myfile << minscore << endl;
myfile << "Name of the student with minimum score is " <<  endl;
myfile << stuWMinScore.getName() << endl <<  endl;
//  myfile.close();
cout << "Copied to Results.txt" << endl;
  }
else
  cout << "file in not open" << '\n';

}


//prints out the name and scores of each student
void printVector (const vector<Student>& newMyClass)

{
unsigned int size = newMyClass.size();
for (unsigned int i =0; i < size; i++)

  {
cout << "Student name is: "<< newMyClass[i].getName() << endl;
cout << "Student Score 1 is "<< newMyClass[i].getScore1()<< endl;
cout << "Student Score 2 is "<< newMyClass[i].getScore2()<< endl;
cout << "Student Score 3 is "<< newMyClass[i].getScore3()<< endl;
cout << "Student Final Score is " << newMyClass[i].getFinal() << endl;
cout << endl; 
  }

}




int main ()

{
vector <Student> myClass;
fillvector (myClass);
readToVector(myClass);
    printVector(myClass); 
}
使用名称空间std;
//申报班级学生
班级学生
{
私人:
字符串newName;
国际新闻中心1;
国际新闻中心2;
int newScore3;
新决赛;
公众:
//无参数构造函数
学生()
{
newName=“”;
newScore1=0;
newScore2=0;
newScore3=0;
newFinal=0.00;
}
//所有参数构造函数
学生(字符串名称、整数分数1、整数分数2、整数分数3、浮点最终)
{
newName=名称;
newScore1=score1;
newScore2=score2;
newScore3=score3;
newFinal=final;
} 
//吸气剂
字符串getName()常量
{
返回newName;
}
int getScore1()常量
{
返回新闻核心1;
}
int getScore2()常量
{
返回新闻核心2;
}
int getScore3()常量
{
返回新闻核心3;
}
float getFinal()常量
{
返回newFinal;
}
//二传手
void setName(字符串名称)
{
newName=名称;
}
无效设置分数1(整数分数1)
{
newScore1=score1;
}
无效设置分数2(整数分数2)
{
newScore2=score2;
}
无效设置分数3(整数分数3)
{
newScore3=score3;
}
无效设置最终版(浮动最终版)
{
newFinal=final;
}
};
//询问学生人数,
//函数要求输入以填充向量
//对输入进行排序,以获得3分中最多2分
//将数据放在班级学生的向量中
//将数据发送到文本文件students.txt
void fillvector(vector和newMyClass)
{
字符串名;
浮动计分1分;
浮动计分2分;
浮动计分3分;
浮动决赛;
浮动tmp;
cout类大小;
for(int i=0;i分数2)
{
浮动tmp=1分;
分数1=分数2;
得分2=tmp;
}
如果(分数1>分数3)
{
浮动tmp=1分;
分数1=分数3;
得分3=tmp;
}
如果(分数2>分数3)
{
浮动tmp=2分;
得分2=得分3;
得分3=tmp;
}
最终得分=(得分2+得分3)/2;
学生新闻学生(姓名、分数1、分数2、分数3、期末);
newMyClass.push_back(newStudent);

coutbug的核心看起来就在这里:

while(fin >> name >> score1 >> score2 >> score3 >> finalScore)
{
    newStudent.setName(name);
    newStudent.setScore1(score1);
    newStudent.setScore2(score2);
    newStudent.setScore3(score3);
    newStudent.setFinal(finalScore);
}
fin.close();
newMyClass.push_back(newStudent); 
一旦缩进被修复,很容易看出,
newStudent
仅在文件被完全读取后才被推入向量中。OP应该将最后一个学生放入向量中,但其他学生都没有。因为OP可能希望文件中的所有学生都进入向量中

while(fin >> name >> score1 >> score2 >> score3 >> finalScore)
{
    newStudent.setName(name);
    newStudent.setScore1(score1);
    newStudent.setScore2(score2);
    newStudent.setScore3(score3);
    newStudent.setFinal(finalScore);
    newMyClass.push_back(newStudent); <- moved push_back to here
}
fin.close(); <- probably not necessary

一旦你知道文件失败的地方,你就可以提出一个新问题。这个问题有点太多了。

你切断了函数的一部分。最小值和最大值计算在哪里?students.txt是否与程序在同一目录中?请提供更完整的代码(包括学生类)还有一个students.txt文件的示例。您好,感谢您的回复,我已经添加了类定义和最小-最大计算。请阅读并相应地编辑您的问题。您提供的代码仍然不完整,远未编译。此外,您没有提供student.txt文件。哦,请修复缩进。当您这不起作用。你应该发布它,以便更好地练习。谢谢,我尝试将push_移回while循环,但不影响结果。它仍然只读取用户对结果的输入。txt由于大小由用户输入的学生数量决定,因此我尝试使用newMyClass.size();获取大小。当您使用开发环境的调试器运行代码时,您是否曾输入
while
循环?或者,去老派学校。在while循环中放置一个cout以打印学生的名字。没有名字,没有学生,您的解析规则对您的输入文件是错误的。这似乎是问题所在,但我无法找到一个正确的语法来解析它。向一位专家展示了代码,他告诉我只解决这个问题,但在尝试了许多方法后,我无法得到正确的代码。按照steiner的要求,在包含学生列表的文件中添加几行。很可能其中一个输入标记没有解析为浮点数。例如,“约翰·史密斯3.14 99.6 82.0 666.13”将把约翰读成名字,然后把史密斯读成分数1。显然,这不会奏效。
int main()
{
    ifstream fin("students.txt");
    if (fin.is_open())
    {
        int line = 0;

        while(true) // loop forever
        {
            line++;
            if (!fin >> name)
            {
                std::cout << "Could not read name on line " << line << std::endl;
                break; // exit loop
            }
            if (!fin >> score1)
            {
                std::cout << "Could not read score1 on line " << line << std::endl;
                break;
            }
            if (!fin >> score2)
            {
                std::cout << "Could not read score2 on line " << line << std::endl;
                break;
            }
            if (!fin >> score3)
            {
                std::cout << "Could not read score3 on line " << line << std::endl;
                break;
            }
            if (!fin >> finalScore)
            {
                std::cout << "Could not read finalScore on line " << line << std::endl;
                break;
            }
        }
    }
    else
    {
       std::cout << "Could not open file" << std::endl;
    }
}