如何解决我不能从Student.Txt将新数据插入学生列表 [英] How to solve I Cant Insert New Data To Student List From Student.Txt

查看:64
本文介绍了如何解决我不能从Student.Txt将新数据插入学生列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bool InsertResult(char *filename, List<student> *list)
{
	Student stu;
	Exam exam;

	bool read=true;
	char stuid[10];

	ifstream in; 
	in.open(filename);

	if(!in){
		cout << "The file cannot open." << endl;
		read = false; //set the read to false when the file not found
	}
	
	while(!in.eof())
	{	
		int check=1;
		in >> stuid;

		
		in >> exam.trimester;
		in >> exam.year;
		in >> exam.numOfSubjects;

		for(int i=0;i<exam.numofsubjects;i++)>
		{
			in>>exam.sub[i].subject_code;
			in>>exam.sub[i].subject_name;
			in>>exam.sub[i].credit_hours;
			in>>exam.sub[i].marks;
		}
		for(int i=1; i<=list->size(); i++)
		{
			list->get(i,stu);
			if(strcmp(stu.id,stuid) == 0)
			{	
				list->find2(stu)->item.exam->insert(1, exam);
				exam.calculateGPA();
				stu.calculateCurrentCGPA();
				cout << "\n\nname: " << stu.name << "    credit: " << stu.totalCreditsEarned << "\n";
				list->set(i,stu); 
				cout << stu;
				break;
			}
			//cout << stu;
		}
		cout << stu;
			
			} 

	in.close();
	cout << "\n\t Student results are successfully inserted.\n";
	return true;
}





//===========================Student.h=======================//
#ifndef Student_type
#define Student_type


#include	<cstring>
#include	"Exam.h"
#include    "Subject.h"
#include	"List.h"

//student info
class Student {		
public:

	char name[30];	
    char id[10];
	char course[3];
	char phone_no[10];
	double current_cgpa; 
	int currentCreditHours;  
	int totalCreditsEarned; 
	List<exam>	*exam;
	List<subject> *subject;

	Student();
	bool operator>= (Student);
	bool operator== (Student);
	bool calculateCurrentCGPA();
	bool calculateCurrentCreditHours();
	friend ostream& operator << (ostream&, Student);


};


Student::Student()
{
	strcpy(name,"");
	strcpy(course,"");
	strcpy(phone_no ,"");
	strcpy(id, "0");
	current_cgpa = 0.0;
	currentCreditHours = 0;
	totalCreditsEarned = 0;
	exam = new List<exam>;
	subject = new List<subject>;

}


bool Student::operator>=(Student p2)
{
	if (strcmp(name,p2.name) >=0 )
			return true;

	return false;
}

bool Student::operator==(Student p2)
{
	if (strcmp(name,p2.name) ==0 )
			return true;

	return false;
}

ostream &operator<< ( ostream &out, Student stu)
{
	
	out << "\nStudent Name                   :"<< stu.name;
	out << "\nStudent Course                 :"<< stu.course;
	out << "\nStudent Phone_No               :"<< stu.phone_no;
	out << "\nStudent Id                     :"<< stu.id;
	out << "\nStudent Current CGPA           :"<< stu.current_cgpa;
	out << "\nStudent Credit Hours           :"<< stu.currentCreditHours;
	out << "\nStudent To Total Credit Earned :"<< stu.totalCreditsEarned;

	out <<"\n";

	return out;
}

bool Student::calculateCurrentCGPA() 
{
	double temp = 0 ;
	int tempCredit = 0;
	Exam cur;

	for(int i=1;i<=(exam->size());i++)
	{
		exam->get(i,cur);
		cur.calculateGPA();

		for(int j=0;j<cur.numofsubjects;j++)>

		{
			temp += (cur.sub[j].getGradePoint() * cur.sub[j].credit_hours);
			tempCredit += cur.sub[j].credit_hours;
		}
	}
		totalCreditsEarned = tempCredit;
		current_cgpa = temp/tempCredit;	
	
	return true;
	

}
bool Student::calculateCurrentCreditHours() 
{
	Exam e;
	int temp_Credit =0;
	for(int j=1;j<=exam->size();j++)
	{	
		exam->get(j, e);
		temp_Credit = 0;
		for(int i=0; i<e.numofsubjects;>		{
			temp_Credit += e.sub[i].credit_hours;
		}
		}
	totalCreditsEarned = temp_Credit;

	return true;

}

#endif</subject></exam></subject></exam></cstring></student>

推荐答案

因为您每次都在重复使用相同的Student对象,而不是为每组新的学生详细信息创建一个新实例。

这意味着你添加第一个学生,然后为每个连续的学生覆盖他的详细信息,最后你会得到(比方说)你名单中最后一个学生的十个副本而不是十个不同的学生。
Because you are reusing the same Student object each time, instead of creating a new instance for each new set of student details.
This means that you add the first student, then overwrite his details for each successive student, and you end up with (say) ten copies of the last student in your list instead of ten different students.


这篇关于如何解决我不能从Student.Txt将新数据插入学生列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆