#include "STDAFX.H" 
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;

class Subject 
{ 
private: 
  string code;   
  int  cu; 
  char pf; 
public: 
  Subject(void); 
  void setCode(string); 
  void setCU(int); 
  void setPF(char);   
}; 

class Student 
{ 
private: 
  string name; 
  int count;
  int totalCU; 
  Subject subjects[4];   
public: 
  Student(void); 
  void setName(string); 

  void setCount(int);  
  
  void setTotalCU(int); 

  void setSCode(string,int); 

  void setSCU(int,int);  
      
  void setSpf(char,int);   
 
  string getName(void);  
  int getSCount(void); 
  int getTCU(void); 
};



// implementation section

// set the total count for passed subjects  
void Student::setCount(int cPass)
{
	count = cPass;
}
// set the total credit units of passed subjects      
void Student::setTotalCU(int PassTotalCU)
{
	totalCU = PassTotalCU;
}
// set the subject code for the ith subject   
void Student::setSCode(string subject,int i)
{

} 
// set the  subject CU for the ith subject  
void Student::setSCU(int,int)
{
}  
// set the subject grade (pass or fail)         
void Student::setSpf(char,int)
{

}

 string Student::getName(void)
{
}  
  int Student::getSCount(void)
{
} 
  int Student::getTCU(void)
{
} 
Subject::Subject(void) 
{ 
} 
void Subject::setCode(string c) 
{ 
  code = c; 
} 
void Subject::setCU(int u) 
{ 
  cu = u; 
} 
void Subject::setPF(char p) 
{ 
  pf = p; 
} 

// end implementation section

int main() 
{ 
  Student students[4]; 
   
  int count, cu, totalCU; 
  string name, code; 
  char pf; 
  ifstream inFile; 
  // declare other relevant variables here 
 
       
  
  inFile.open("C:\\student.dat");
    if (!inFile.good())
    {
        cout << "File not found" << endl;
		return 1;
    }

	while(!inFile.eof())
	{
        // use getline to read entire line  
        getline(inFile, s);
        cout << s << endl;
    }
    
 
// Part (ii): code goes here 

}

The question:
Each student takes up to 4 subjects. The first field in each record is a
name, followed by each subject’s code, number of credit units and
grade of the subject. The grade for each subject is either a P or F; P
means pass while F means fail.

Student.dat
Peter Subject1 4 F Subject2 3 P Subject3 5 P Subject4 5 P
Ali Subject1 4 P Subject2 3 P Subject3 5 P Subject4 5 P
Janet Subject1 4 F Subject2 3 F Subject3 5 P Subject4 5 P
Koh Subject1 4 P Subject2 3 P Subject3 5 P Subject4 5 P


I'm suppose to get the values from student.dat and pass them in to an array, but i cant seem to think of a way to implement the student class. can someone help me out?

You'll need to write the set and get functions for Student and Subject, as well as possibly adding some.

Do you have a Subject object in main? I think you'll need one.

Start reading in from the input file into variables in main , then use your set functions in the Student class to fill in the info for student[i] (where i ranges from 0 to 3). I would use the >> operator instead of getline in main . Why parse the line if you don't have to?

I don't see any variable named s declared before the red lines below:

while(!inFile.eof())
	{
        // use getline to read entire line  
        getline(inFile, s);
        cout << s << endl;
    }

i added the missing code in.

int main() 
{ 
  Student students[4]; 
   
  int count, cu, totalCU; 
  string name, code; 
  char pf; 
 ifstream fin;
    string filename;
    string s;
  // declare other relevant variables here 
 
       
  
  inFile.open("C:\\student.dat");
    if (!inFile.good())
    {
        cout << "File not found" << endl;
		return 1;
    }

	while(!inFile.eof())
	{
        // use getline to read entire line  
        getline(inFile, s);
        cout << s << endl;
    }

Start reading in from the input file into variables in main , then use your set functions in the Student class to fill in the info for student[i] (where i ranges from 0 to 3). I would use the >> operator instead of getline in main .

can you kindly give me an example for the set or get functions?

or do i do this for the get functions of the student class?

strcpy(name,n);

while(!inFile.eof())
	{
        // use getline to read entire line  
        getline(inFile, s);
        cout << s << endl;
    }

That's wrong code. eof gets set when it has read eof, so and in that case getline() won't do a thing, so you'll output the last output twice (possibly).

Correct code:

while(getline(inFile, s)         // use getline to read entire line  
	{
        cout << s << endl;
    }

From there, again, use the set functions. You do know what your set functions do right? :D Some of the classes functions are empty though, either get rid of them or make them useful.

An example

Student someStudent;
someStudent.setName("Anne");
cout << someStudent.getName() << endl;

i'm limited to the functions given in the qns. kinda like having mental block when i try to think of how to code for the set functions =(

Here's an example of set and get functions for the count data member in class Student (note that count is an int. Note that the parameter passed to the setcount function is an int and that the getcount function returns an int. If count was a string, the get function would return a string and the set function would take a string as a parameter).

void Student::setcount (int cnt)
{
     count = cnt;
}

int Student::getcount ()
{
     return count;
}

Every private data member usually has a set function and a get function. set functions are usually void functions and take a single parameter that is the same as the type of the data member they are setting (int in this case since count is of type int). get functions have a return type that is the same as the type of the data member (since count is type int, getcount returns an int) and take no parameters).

set and get functions should be public. The data members themselves that they correspond to are normally private. You already have a few in your code. Run through all of your data members and create a set and get function for them. The get functions should return something and take no parameters. The set functions won't return anything and will take a parameter.

A small addition:
Sometimes set functions do return something, and most of the time that's whether they succeeded or not.

Anyway, seeing the format, it's like this:

<name>, list(<subject>, <number>, <character>)

Make a function that separates those variables, so you can output them individually. If you can do that, then you can set them individually with those set functions handed to you.

i got around to coding abit more, but i'm wondering how to set the grade to P or F for the setSpf function. my code looks like this now. any ideas?

class Student 
{ 
private: 
  string name; 
  int count;
  int totalCU; 
  Subject subjects[4];   
public: 
Student(void); 
  void setName(string); 
  void setCount(int);  
  void setTotalCU(int); 
  void setSCode(string,int); 
  void setSCU(int,int);  
  void setSpf(char,int);   
  string getName(void);  
  int getSCount(void); 
  int getTCU(void); 
};

void Student::setName(string Name)
{
	name = Name;
}

// set the total count for passed subjects  
void Student::setCount(int cPass)
{
	count = cPass;
}
// set the total credit units of passed subjects      
void Student::setTotalCU(int PassTotalCU)
{
	totalCU = PassTotalCU;
}
// set the subject code for the ith subject   
void Student::setSCode(string subject,int i)
{
		i = 0;
		for (i = 0; i < 3; i++)
		{
			subject = "subject" +i;	
		}
	
} 
// set the  subject CU for the ith subject  
void Student::setSCU(int Q,int CreU)
{
	CreU = 0;
	while( CreU < 3)
		{
			switch (CreU)
   {
	   case '1' : Q = 4;
		break;

		case '2' : Q = 3;
		break;

		case '3' : Q = 5;
		break;

		case '4' : Q = 5;
		break;
   }  	
			CreU++;
		}
}  
// set the subject grade (pass or fail)         
void Student::setSpf(char score,int grade)
{
}

 string Student::getName(void)
{
	return name;
}  
  int Student::getSCount(void)
{
	return count;
} 
  int Student::getTCU(void)
{
	return totalCU;
} 
// end implementation section

int main() 
{ 
  Student students[4]; 
   //count,totalCU,
  int  cu[4], i, k,ch; 
  string name[4], code[4]; 
  char pf[4]; 
 ifstream inFile;
  // declare other relevant variables here 
 
       
  
  inFile.open("C:\\student.dat");
    if (!inFile.good())
    {
        cout << "File not found" << endl;
		return 1;
    }

	i = 0;
	while( (ch = inFile.peek()) != EOF)
	{
		inFile >> name[i];
		// 1st subject
		inFile >> code[i];
		inFile >> cu[i];
		inFile >> pf[i];
		i++;
	}
	inFile.close();

}
// set the subject grade (pass or fail)         
void Student::setSpf(char score,int grade)
{
}

How does that function know what subject it is supposed to work on?

// set the subject grade (pass or fail)         
void Student::setSpf(char score,int grade)
{
}

How does that function know what subject it is supposed to work on?

that's why i'm confused about it. i was thinking how am i to set it. Or should i just assign a rubbish value and let it be override in main?

Haha, no, that function of course needs to know what subject to set. Change it a bit, add a comment for your prof? The other functions do have such a parameter, so maybe he forgot?

does anyone have any idea how do i set it? the function looks like this in the beginning.

// set the subject grade (pass or fail)  
void Student::setSpf(char ,int )
{
}

also for reading from the file. i know below it will only read up to the 1st subject. As i need to read in the whole line, do i just add more variables to it?

such as from this:

i = 0;
	while( (ch = inFile.peek()) != EOF)
	{
		inFile >> name[i];
		// 1st subject
		inFile >> code[i];
		inFile >> cu[i];
		inFile >> pf[i];
		i++;
	}
	inFile.close();

to this?

i = 0;
	while( (ch = inFile.peek()) != EOF)
	{
		inFile >> name[i];
		// 1st subject
		inFile >> code[i];
		inFile >> cu[i];
		inFile >> pf[i];
                inFile >> codeW[i];
		inFile >> cuW[i];
		inFile >> pfW[i];
		i++;
	}
	inFile.close();

does anyone have any idea how do i set it? the function looks like this in the beginning.

// set the subject grade (pass or fail)  
void Student::setSpf(char ,int )
{
}

Look at my example from my earlier post:

void Student::setcount (int cnt)
{
     count = cnt;
}

int Student::getcount ()
{
     return count;
}

See the red part. You must specify a variable name. You had it before and took it out. I used a variable name that was slightly different from the member of the data member's name ("cnt" versus "count"). Then I assigned the value of the data member to be the value of the parameter passed to it:

count = cnt

Like ClockOwl said, which subject is this setting? Seems like you would need another parameter:

void setSpf (int subjectIndex, char someChar, int someInt)

Presumably you should have an setSpf function in your Subject class like this:

void setSpf (char someChar, int someInt)

So the function in the Student class could be this?

void setSpf (int subjectIndex, char someChar, int someInt)
{
    subjects[subjectIndex].setSpf (someChar, someInt);
}

That's my best guess. I'm not 100% sure what setSpf is supposed to do.

"Set pass/fail" I guess? Maybe the int is the subject indicator? Ugh, the variable names are horribly chosen. Just use that int as subject index and the char as.. P or F or something? Or grades? I dunno.

setSpf must mean "set Subject Pass/Fail".

From the first post:

void Subject::setPF(char p) 
{ 
  pf = p; 
}

So it's gotta be this (comment was from first post too:

// set the subject grade (pass or fail) 
void Student::setSpf (char passfail, int subjectindex)
{
    subjects[subjectindex].setPF (passfail);
}

I agree with ClockOwl. The variable names and the function names need to be much better.

[EDIT]
On the other hand, if it's pass/fail, shouldn't pf be a true/false boolean data member instead of a char?
[/EDIT]

Yeah, but keep in mind he didn't design the functions himself, his prof did it for him. He needs to use these functions, unaltered, I think.

it's part of the assignment and i'm limited to the functions that are given. i'm just gonna use a switch to set the grade to P or F since the file is fixed and won't be changed.

#include "STDAFX.H" 
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;

class Subject 
{ 
private: 
  string code;   
  int  cu; 
  char pf; 
public: 
  Subject(void); 
  void setCode(string); 
  void setCU(int); 
  void setPF(char);   
}; 

class Student 
{ 
private: 
  string name; 
  int count;
  int totalCU; 
  Subject subjects[4];   
public: 
Student(void); 
  void setName(string); 
  void setCount(int);  
  void setTotalCU(int); 
  void setSCode(string,int); 
  void setSCU(int,int);  
  void setSpf(char,int);   
  string getName(void);  
  int getSCount(void); 
  int getTCU(void); 
};

Subject::Subject(void) 
{ 
} 
void Subject::setCode(string c) 
{ 
  code = c; 
} 
void Subject::setCU(int u) 
{ 
  cu = u; 
} 
void Subject::setPF(char p) 
{ 
  pf = p; 
} 

Student::Student(void)
{
}
void Student::setName(string Name)
{
	name = Name;
}

// set the total count for passed subjects  
void Student::setCount(int cPass)
{
	count = cPass;
}
// set the total credit units of passed subjects      
void Student::setTotalCU(int PassTotalCU)
{
	totalCU = PassTotalCU;
}
// set the subject code for the ith subject   
void Student::setSCode(string subject,int i)
{
		i = 0;
		for (i = 0; i < 3; i++)
		{
			subject = "subject" +i;	
		}
	
} 
// set the  subject CU for the ith subject  
void Student::setSCU(int Q,int CreU)
{
	CreU = 0;
	while( CreU < 3)
		{
			switch (CreU)
   {
	   case '1' : Q = 4;
		break;

		case '2' : Q = 3;
		break;

		case '3' : Q = 5;
		break;

		case '4' : Q = 5;
		break;
   }  	
			CreU++;
		}
}  
// set the subject grade (pass or fail)         
void Student::setSpf(char score,int grade)
{
}

 string Student::getName(void)
{
	return name;
}  
  int Student::getSCount(void)
{
	return count;
} 
  int Student::getTCU(void)
{
	return totalCU;
} 
// end implementation section

int main() 
{ 
	const int loop = 4;
  Student students[4]; 
   int totalCU;
  int cu[loop],cul[loop],cull[loop],culll[loop], i, k,ch ; 
  string name[loop],newname, code[loop], codel[loop], codell[loop], codelll[loop]; 
  char pf[loop],pfl[loop],pfll[loop],pflll[loop]; 

  // declare other relevant variables here 
 
 ifstream inFile;  
  inFile.open("C:\\grades.dat");
    if (!inFile.good())
    {
        cout << "File not found" << endl;
		return 1;
    }

i = 0;
	while( (ch = inFile.peek()) != EOF)
	{
		inFile >> name[i];
		// 1st subject
		inFile >> code[i];
		inFile >> cu[i];
		inFile >> pf[i];
		//subject2
		inFile >> codel[i];
		inFile >> cul[i];
		inFile >> pfl[i];
		//subject3
		inFile >> codell[i];
		inFile >> cull[i];
		inFile >> pfll[i];
		//subject4
		inFile >> codelll[i];
		inFile >> culll[i];
		inFile >> pflll[i];
		i++;
	}
	inFile.close();

	for(i = 0; i < loop; i++)
	{
		totalCU = 0;
		cout << "Student name: " << name[i] << " " << endl;
		cout << "Course    Course    Course " << endl;
		cout << " Name     Credits   Grade " << endl;
		cout << "------    -------   ------" << endl;
		
		k = i;
 		
		while(name[k] == name[i])
		{
			cout << code[k] << "        "<< cu[k] << "         " << pf[k] << endl;
			cout << codel[k] << "        " << cul[k] << "         "<< pfl[k] << endl;
			cout << codell[k] << "        " << cull[k] << "         "<< pfll[k] << endl;
			cout << codelll[k] << "        " << culll[k] << "         " << pflll[k] << endl;

			totalCU = cu[k]+ cul[k]+ cull[k]+ culll[k];

			k++;
		}
		
		cout << "\nTotal Semester Course Credits Completed: " << totalCU<< endl;
	}

	cin.ignore();  // this line is optional
	cin.ignore();

	return 0;
}

now i'm having problems with showing the results. it keeps saying that "Unhandled exception at 0x10240027 in tester.exe: 0xC0000005: Access violation reading location 0xffffffff" anyone have any idea? i just need to fix this bug and get the data into an array and i'm done with this qns. thanks for all the help so far =)

Totally wrong approach buddy. Create 4 students, read out their data.

Example code:

cout << students[0].getVar() << endl;

Kinda like that.

i do need to count the total credit units before inserting them to the student array. and part two of the question requires me to read out from the array and display it.

You could create a function that accepts a reference to a student and outputs it's data?

void output_student(Student &stud){
//put the output code here
}
//....
int main(){
//loop each of your students here, one loop for reading in the data, the other one for outputting the data
}

problem is i not supposed to create any functions. its a bit stupid but yar it is that way.

That sucks. But still, just loop the student array then, one time for input, one time for output. So no more extra variables representing members in class Student! ;)

but for the multiple subjects its correct for me to have the multiple inputs??

// 1st subject
		inFile >> code[i];
		inFile >> cu[i];
		inFile >> pf[i];
		//subject2
		inFile >> codel[i];
		inFile >> cul[i];
		inFile >> pfl[i];
		//subject3
		inFile >> codell[i];
		inFile >> cull[i];
		inFile >> pfll[i];
		//subject4
		inFile >> codelll[i];
		inFile >> culll[i];
		inFile >> pflll[i];

do u mean this?

Student::subjects::students[4]

sorry if i'm being dumb...

Errr, ignore that last part. Sometimes I'm unclear.

Student student;
for(int subject = 0; subject < 4; subject++){
    string code;
    int cu;
    char pass_or_fail;
    inFile >> code;
    inFile >> cu;
    inFile >> pass_or_fail;
    
    //subject == loop variable
    subjects[subject].setCode(code);
    subjects[subject].setCU(cu);
    subjects[subject].setPF(pass_or_fail);
}

I meant something like that. That's the "input" loop. Now, as you can see, this one is for subjects, but you should also create one for students for their variables. Same goes for output. If you don't know how, first write the code for reading in 1 student, then simply loop that. ;)

ya ok. i can get the name to be displayed now i just have to count the scores and display it.
but something tells me that i'm inserting the code,cu and pf wrongly.

i = 0;
 k = 0;
	while( (ch = inFile.peek()) != EOF)
	{
	k = i;	
	inFile >> name;
	students[k].setName(name);
	for(k = 0; k < 4; k++){
    inFile >> code;
    inFile >> cu;
    inFile >> pf;
    
    //subject == loop variable
    students[k].setSCode(code,k);
    students[k].setSCU(cu,k);
    students[k].setSpf(pf,k);
	}
This question has already been answered. Start a new discussion instead.