从文本文件读取到类中的对象向量 - 在另一个 txt 文件中编写
Reading from a text file into a vector of objects in a class- writing in another txt file
我已经更改了代码,现在程序可以正确读取学生形式的文件.txt但仍根据用户输入而不是学生进行计算.txt
#include #include #include #include #include #include #include #include
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);
}
错误的核心看起来就在这里:
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
无法验证代码的其余部分,因为似乎没有定义或分配(OP已经解决了这个问题) size
- Crom 只知道那些 for 循环是否覆盖了正确的地面 - 或者输入的有效性,因为没有提供输入文件。如果解析规则错误,OP 可能什么都不读取。调试器会比另一个堆栈溢出帖子更快地解决这个问题。
新东西。
我不明白为什么输入也没有解析。首先确保文件编码为 ASCII 文本。我不知道您在计算机上使用什么工具执行此操作。我使用十六进制编辑器,让您以字节值而不是字符查看文件,以查看字符是否与其预期的 ASCII 值不匹配。文本编辑器可能正在为您转换并隐藏差异。
例如,通常 UTF-8编码的文件看起来与 ASCII 文件完全相同,除了开头标记的二进制文件通知读取器程序它是 UTF-8 文件并且可能包含非 ASCII。
如果不是,那就是问题所在。将文件重新保存为 ASCII 文件。如果是 ASCII 文件,请尽量减少时间。编写一个小程序,除了打开文件外什么都不做,逐个令牌读取它,看看哪个令牌解析失败。像这样:
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;
}
}
一旦你知道文件在哪里失败,你就可以提出一个新问题了。这个有点太庞大了。
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- C++试图读取一个文件并输出到另一个文本文件
- 如何从另一个文件继承私有成员变量和公共函数
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- C++ 如何打开带有另一个目录的输入文件?
- 如何在 Gnuplot 中分别绘制 2 个文件数据?我有一个文件"sin.txt",另一个文件"cos.txt",我想将它们分别绘制在一个图表上
- 从另一个 cpp 文件更改结构内、映射键内的变量
- 程序在使用 system() 启动另一个可执行文件时停止
- 如何在安卓工作室中将c ++头文件从一个文件夹导入到另一个文件夹?
- 如何在另一个 CPP 文件中使用一个 cpp 文件中的函数?
- 使用 scope 运算符 (::) 引用另一个文件中的类
- JNI,使用两个 .so 文件时出错,其中一个文件需要另一个文件
- 如何将包含另一个对象向量的对象保存到文件中,并使用C++中的二进制文件从文件中读回?
- 两个线程一个使用流 Api,另一个线程创建文件失败并出现错误ERROR_SHARING_VIOLATION
- 为什么我不能从同一目录中的另一个文件中 #include 我的类?
- 使用命名空间在另一个".cpp"文件中定义
- 在 c++ 中打开文件、删除标点符号并追加到另一个文件中
- C++ 如何将两个 makefile 对象目标规则(位于另一个文件夹中)合并到一个目标/规则中?
- 是否可以将子进程的 stdout 重定向到父进程中的另一个文件?