Tuesday, July 14, 2009

C++ class/array program?

For this program, you will create a student class and a main program that reads student information from a file and outputs the students' names in groups based on grades.





name grade





Name is a string type and grade is a char type from the set {A,B,C,D,E}.


Requirements:





Your main function should should read in the file until eof is reached, instantiate a new object for each student using your student class, and output the results.


Your student class consists of 2 private variables (name and grade), one constructor and the following 3 functions:


void setGrade(char);


string getName();


char getGrade();


The constructor for your class should set the student's name - student(string)


After constructing an object for a student, you need to read in the grade and call the setGrade(char) function to set the grade for that student.


To form the output, you need to call the getName() and getGrade() functions for each student.

C++ class/array program?
[UPDATED AGAIN. SORRY THERE WAS AN ERROR WITH MY CONSTRUCTORS. TRY AGAIN]





Ok, yes you can place everything in one file. Here is how:





#include %26lt;fstream%26gt;


#include %26lt;string%26gt;





using namespace std;





class Student


{


public:


Student();


Student(string someName);


void setName(string someName);


string getName();


char getGrade();


void setGrade(char someGrade);


private:


string Name;


char Grade;


};





Student::Student()


{


Name = " ";


Grade = ' ';


}





Student::Student(string someName)


{


Name = someName;


Grade = ' ';


}





void Student::setName(string someName)


{


Name = someName;


}





void Student::setGrade(char someGrade)


{


Grade = someGrade;


}





string Student::getName()


{


return Name;


}





char Student::getGrade()


{


return Grade;


}








int main()


{


ifstream inFile;


ofstream outFile;


const int MAX_STUDENTS = 50;


const int NUM_GRADES = 5;


Student arrayList[MAX_STUDENTS];


char grades[] = {'A', 'B', 'C', 'D', 'E'};


int count = 0;


string name;


char grade;





inFile.open("C:\\students.txt");





while(!inFile.eof() %26amp;%26amp; count %26lt; MAX_STUDENTS)


{


inFile %26gt;%26gt; name %26gt;%26gt; grade;


arrayList[i] = Student(name);


arrayList[i].setGrade(grade);


count++;


}





inFile.close();


outFile.open("C:\\groups.txt.");





for(int i = 0; i %26lt; NUM_GRADES; i++)


{


outFile %26lt;%26lt; grades[i] %26lt;%26lt; " ";


for(int j = 0; j %26lt; count; j++)


{


if(arrayList[j].getGrade() == grades[i])


outFile %26lt;%26lt; arrayList[j].getName() %26lt;%26lt; " ";


}


outFile %26lt;%26lt; endl;


}


outFile.close();


return 0;


}





// Please copy %26amp; run the code. It should work. Let me know


// if it works or not, please. Also, let me know if you need


// anything else.





// One more thing though, you are not checking whether or not


// opening the input %26amp; output files fails. Do you want me to


// show you how?





Hey Lisa, you said you owe me, and I agree. I need a favor from you. I want you to learn the following because you made mistakes related to these:





1) When you open a file don't close it until you are done with it. "Done with it" means that you already read data from the file or wrote data to the file.





2) All of your constructors have to have the same name as the name of the class (case sensitive). In your case, all of them have to be named "Student" not "student".





3) When your member function does not require parameters such as your getName function, here is how you declare it:





string getName();





NOT





string getName(void);





4) When your function requires a parameter such as your setGrade function, this is how it should look like:





void setGrade(char someGrade);





NOT





void setGrade(char);





Always name your parameters.
Reply:#Include %26lt;fstream.h%26gt;


#include %26lt;string.h%26gt;





void main()


{


ifstream ifs("students.txt");


string line;


Student S[100];


while(!ifs.eof()) {


getline(ifs,line);


cout %26lt;%26lt; "[ " %26lt;%26lt; line %26lt;%26lt; " ]" %26lt;%26lt; endl;


//put ur logic like


S(First 10 Charecter name , Rest all charets Number)


then call a sort Program


}


if you can wait then pls. I can write program after 5:30. as if I am in office now.


No comments:

Post a Comment