Hi,
I'm stuck with this one question we were asked to do in class...The program is to read input from a file that contains students' ids and marks and we have to determine their grades and store these variables as arrays then print it out. Then we were to create a class called student and then test it if it's working by creating an object in main() and testing each methods of the class. Then we must create an array of classes called Student student_Info[MAXSIZE] and use that instead of the parrallel arrays. I've done most parts of it till this part where we have to delete the parrallel arrays (i.e id[MAXSIZE], marks[MAXSIZE] and grade[MAXSIZE] and create a new array for student class to be used instead. I am not sure what needs to be done next... Can anyone please help me?
Here is what I've done so far but still have errors. Note that I've included comments on the parts where I'm stuck with:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string>
const int MAX_SIZE = 10;
//Class Definition
class Student{
private:
string id;
int marks;
char grade;
public:
Student();
~Student();
void setID(string);
string getID();
void setMarks(int);
int getMarks();
void setGrade(char);
char getGrade();
}; // closing class
//class function body
Student::Student()
{
cout<< "Student Constructor called" << endl;
}
Student::~Student()
{
cout<< "Student Destructor called" << endl;
}
void Student::setID(string id)
{
this->id=id;
}
string Student::getID()
{
return this->id;
}
void Student::setMarks(int marks)
{
this->marks=marks;
}
int Student::getMarks()
{
return this->marks;
}
void Student::setGrade(char grade)
{
this->grade=grade;
}
char Student::getGrade()
{
return this->grade;
}
//Function Prototypes
char determine_grade(int smarks);
void discard_line(ifstream &in);
void print_students_records(student s[], int size) //This is the modified version of the below commented function which was used at first
//void print_students_records(string sid[], int smarks[], char sgrades[], int size);
int main()
{
//Student test; //This is used just to test whether my class was working or not
//test.setGrade('A'); //This is used just to test whether the methods in my class were working or not
Student student_Info[MAX_SIZE]; //New array of Student class created
//string id[MAX_SIZE]; //These parallel arrays are deleted
//int marks[MAX_SIZE];
//char grade[MAX_SIZE];
int total_records;
int choice;
char response;
string temp_id;
int temp_marks;
ifstream input;
cout<<"This program determines the grades of students."<<endl;
input.open("E:\\CS112\\student.txt",ios::in);
if(!input){
cerr<<"File could not be opened"<<endl;
exit(1);
}
discard_line(input);
total_records = 0;
while(input>>temp_id>>temp_marks){
id[total_records] = temp_id; //Do I need to do something here?
marks[total_records] = temp_marks;
total_records++;
}
for (int i = 0; i < total_records; i++){
grade[i] = determine_grade(marks[i]); //Do I need to do something here?
}
input.close();
//cout<< test.getGrade();
//print_students_records (id, marks, grade, total_records );
print_students_records (Student student_Info[], total_records);
system("PAUSE");
return 0;
}
//void print_students_records(string sid[], int smarks[], char sgrades[],int size) //Modified below
//Is this how this should be done? I'm still getting errors
void print_students_records(Student s[], int size){
cout<<"\n\nstudents' records"<<endl<<endl;
for (int i = 0; i < size; i++){
//cout<<"id: "<<sid[i]<<", marks: "<<smarks[i]<<", grade: "<<sgrades[i]<<endl;
cout<<"id: "<<Student s[i]<<", marks: "<<Student s[i]<<", grade: "<<Student s[i]<<endl;
}
cout<<endl;
}
char determine_grade(int smarks){
char sgrade;
if (smarks >= 80)
sgrade = 'A';
else if(smarks >= 65)
sgrade = 'B';
else if(smarks >= 50)
sgrade = 'C';
else
sgrade = 'D';
return sgrade;
}
void discard_line(ifstream &in)
{
char c;
do
in.get(c);
while (c!='\n');
}