2 Replies - 1095 Views - Last Post: 09 March 2009 - 12:10 PM Rate Topic: -----

#1 Philipman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-March 09

Reading from a text file into an array of Classes

Posted 09 March 2009 - 10:11 AM

Hi there

I am about to rip my hair out with this. I am trying to read information from a text file into an array of classes. The text file contains the names of students followed by 4 of their marks, like this:
John Maloney
7 //This is a mark out of ten
8 // This as well
70 //The rest are percentages
62
Jacob Oram
6
7
67
89
etc.

This is the code I have so far:

#include <iostream>
#include <fstream>

using namespace std;

const float quizweight=0.25;
const float midtermweight=0.25;
const float finalweight=0.50;

class Student
{
public:
	Student();
	friend istream& operator >>(istream& ins,Student& student1);
	friend ostream& operator <<(ostream& outs,Student&student1);
	void set(string new_name,int new_quiz1,int new_quiz2,int new_midterm,
		 int new_final);
	float calculatescore();
	string getname();
	int getquiz1();
	int getquiz2();
	int getmidtermexam();
	int getfinalexam();
	
private:
	string name;
	int quiz1;
	int quiz2;
	int midtermexam;
	int finalexam;
};

Student::Student():name(""),quiz1(0),quiz2(0),midtermexam(0),finalexam(0)
{
//Intentionally blank
}

ostream& operator <<(ostream& outs,Student&student)
{

cout << "Student name:" << '\t' << student.name << endl;
cout << "Mark for quiz1 out of 10   :" << '\t' << student.quiz1 << endl;
cout << "Mark for quiz2 out of 10   :" << '\t' << student.quiz2 << endl;
cout << "Mark for miterm out of 100 :" << '\t' << student.midtermexam << endl;
cout << "Mark for final out of 100  :" << '\t' << student.finalexam << endl;
cout << "Average for the course	 :" << '\t' << student.calculatescore() << endl;

}

istream& operator >>(istream& ins,Student& student)
{

getline(ins,student.name);
ins >> student.quiz1;
ins >> student.quiz2;
ins >> student.midtermexam;
ins >> student.finalexam;

}

void Student::set(string new_name,int new_quiz1,int new_quiz2,int new_midterm,
		 int new_final)
{
name=new_name;
quiz1=new_quiz1;
quiz2=new_quiz2;
midtermexam=new_midterm;
finalexam=new_final;
}

float Student::calculatescore()
{
int quiztotal;
float average;

quiztotal=(quiz1+quiz2)*5; // normalizes quiz score into a percentage
average=(quiztotal*quizweight)+(midtermexam*midtermweight)
		+(finalexam*finalweight);
return average;
}

int Student::getquiz1()
{
return quiz1;
}

int Student::getquiz2()
{
return quiz2;
}

int Student::getmidtermexam()
{
return midtermexam;
}

int Student::getfinalexam()
{
return finalexam;
}


int main()
{

Student student[20];
ifstream input;
ofstream output;

input.open("Marksinput.txt");
output.open("Marksoutput.txt");


for (int k=0;k<20;k++)
{
input >> student[k];
}



for (int k=0;k<20;k++)
{
output << student[k];

}


input.close();
output.close();


return 0;
}





Now I'm pretty sure that the for loop I'm using won't do the trick. It reads the first of the records and the first record is also correctly displayed, but that's it. Only the first one works, all the other values are given as 0.

If anyone could point me in the right direction I'd mightily appreciate it.
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Reading from a text file into an array of Classes

#2 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Reading from a text file into an array of Classes

Posted 09 March 2009 - 10:34 AM

That is a weird problem. I've found that adding ins.ignore after reading the student information fixes the problem. I have no idea why though:

// ...
ins >> student.quiz1;
ins >> student.quiz2;
ins >> student.midtermexam;
ins >> student.finalexam;
ins.ignore();
.
Was This Post Helpful? 0
  • +
  • -

#3 Philipman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-March 09

Re: Reading from a text file into an array of Classes

Posted 09 March 2009 - 12:10 PM

View Postbsaunders, on 9 Mar, 2009 - 09:34 AM, said:

That is a weird problem. I've found that adding ins.ignore after reading the student information fixes the problem. I have no idea why though:

// ...
ins >> student.quiz1;
ins >> student.quiz2;
ins >> student.midtermexam;
ins >> student.finalexam;
ins.ignore();
.


Gahhhh! The joys of coding. Spent hours looking for the solution and it turns out to be one tiny little line.

Can't thank you enough though mate, I would never have thought of that. But you're right, it is a really weird problem. I scoured the internet for hours and checked and double-checked my C++ textbook(which is actually quite good usually), and nowhere did I find a mention of this kind of problem.

Does anyone know what the nature of this problem is? Would like to know the logic behind it.

Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1