Today was my first time trying composition. It's for a lab project due Friday, and I have a lot of tomorrow to work on it. However, up until we have just been using one class per program. Now, my teacher wants us to use composition and arrays, despite only lecturing on them for a few minutes. Worse, there is no section on composition (and arrays in compositions) in our notes/textbook. So, could you check this code and tell me if I'm on the right track. I don't even need help with syntactical errors (yet); I just need an idea of what I need to do, and what I'm not understanding about composition.
There are five files, and an input file. And the program (as you can see from the input file) reads the student info and stores it in an array and then outputs (1) all information in a table, (2) a student searched by last name, (3) lowest gpa student information, (4) highest units gpa information.
Thank you very much.
student.h
#ifndef student_H
#define student_H
#include <string>
using namespace std;
class Student{
public:
void set_Name(string, string);
bool readStudent(ifstream &);
void printStudent () const;
Student();
string get_Last();
string get_First();
void set_Major(string);
string get_Major( );
int get_Units();
void set_Units(int);
float get_GPA();
void set_GPA(float);
private:
string First, Last;
int Units;
float GPA;
string Major;
};
#endif
Student Implementation
#include <iostream>
#include <fstream>
#include "student.h"
using namespace std;
void Student::set_Name(string first, string last){
First = first;
Last = last ;
}
bool Student::readStudent(ifstream & fin){
getline(fin, First);
if (fin){
getline(fin, Last);
getline(fin, Major);
fin >> Units ;
fin >> GPA ;
fin.ignore(10,'\n');
}
return (fin.good());
}
void Student::printStudent () const
{
cout <<"\n\tStudent information:\n";
cout << "Name: \t" << Last +", "+ First <<endl ;
cout << "Major: \t" << Major << endl;
cout << "Units : \t"<< Units <<endl;
cout << "GPA: \t" << GPA << endl <<endl;
}
Student::Student(){
Units = 0 ;
GPA = 0 ;
};
string Student::get_Last(){
return Last;
}
void Student::set_Major(string t_major){
Major = t_major;
}
int Student::get_Units(){
return Units;
}
float Student::get_GPA(){
return GPA;
}
string Student::get_First(){
return First ;
}
string Student::get_Major( ){
return Major ;
}
void Student::set_Units(int units){
Units = units ;
}
void Student::set_GPA(float gpa){
GPA = gpa ;
}
school.h
#ifndef school_H
#define school_H
#define SIZE 15
#include <string>
#include "student.h"
using namespace std;
class School{
public:
int read_list(Student list[], int size);
bool open_file(ifstream &fin);
void print_list(Student list[], int size);
int search(Student list[], int);
int low_GPA(Student list[], int);
int high_Units(Student list[], int);
private:
Student CS101[SIZE];
};
#endif
School Implementation
#include <iostream>
#include <fstream>
#include <iomanip>
#include "school.h"
using namespace std;
int School::search(Student list[], int size){
string target;
cout <<"\nPlease enter last name of student to locate:(all lower case) ";
getline(cin, target);
if (target.length()> 0){
target[0] = toupper(target[0]); // convert first character to lower case
for (int i = 0 ; i < size ; i++){
if((int)(list[i].get_Last()).find(target)>=0 )
return i ;
}
}
else
cout <<"No name was entered.\n";
return -1 ; // Student was not found on the list;
}
int School::read_list(Student list[], int size)
{
ifstream fin ;
bool flag ;
int i = 0 ;
if (open_file(fin))
{
flag = list[i].readStudent(fin);
while(flag && !fin.eof())
{
i++;
if (i < size)
flag = list[i].readStudent(fin);
else
{
cout <<"\n*********** Array is full.\n";
break ;
}
}
}
return i ;
}
bool School::open_file(ifstream &fin)
{
string f_name ;
int counter = 3 ;
bool flag = true;
do{
flag = true ;
cout <<"Please enter input file name: ";
getline(cin, f_name) ;
fin.open(f_name.c_str());
if (fin.fail())
{
counter-- ;
cout <<"\nBad file name, you have "<< counter << " chances, try again.\n" ;
fin.clear();
flag = false ;
}
}while (!flag && counter >= 0);
return flag ;
}
void School::print_list(Student list[], int size)
{
if (size > 0)
{
cout <<endl <<endl<<left<< setprecision(2) <<showpoint<<fixed;
cout << setw(50)<< "Name"<<setw(30)<<"Major"<<setw(12)<<"Units"<<"GPA\n";
cout << setfill('-') << setw(100)<<"-" << setfill(' ')<<endl;
for (int i = 0 ; i < size ; i++){
cout << setw(50)<<list[i].get_Last() + ", " + list[i].get_First() << setw(30) << list[i].get_Major();
cout << setw(12) << list[i].get_Units() << list[i].get_GPA() << endl;
}
cout <<endl <<endl << "****** end of report ******\n\n";
}
}
int School::low_GPA(Student list[], int size)
{
int lowIndex;
float first, second, lowGPA = 5.0;
if(size > 0){
for (int i = 0 ; i < size ; i++){
first = list[i].CS101.get_GPA();
second = list[i+1].CS101.get_GPA();
if (first < lowGPA && first > 0.00){
lowIndex = i;
lowGPA = list[i].CS101.get_GPA();
}
if (second < lowGPA && second > 0.00){
lowIndex = (i+1);
lowGPA = list[i+1].CS101.get_GPA();
}
}
return lowIndex;
}
}
int School::high_Units(Student list[], int size)
{
int first, second, highIndex, highUnits = 0;
if(size > 0){
for (int i = 0 ; i < size ; i++){
first = list[i].CS101.get_Units();
second = list[i+1].CS101.get_Units();
if (first > highUnits){
highIndex = i;
highUnits = list[i].CS101.get_Units();
}
if (second > highUnits){
highIndex = (i+1);
highUnits = list[i+1].CS101.get_Units();
}
}
return highIndex;
}
}
Main/Driver
#include <iostream>
#include <fstream>
#include <iomanip>
#include "student.h"
#include "school.h"
#define SIZE 15
using namespace std;
int main()
{
Student CS101[SIZE];
int size, index;
ifstream fin;
size = CS101.read_list(CS101, SIZE);
CS101.print_list(CS101,size);
if(size > 0){
index = CS101.search(CS101, size);
if (index != -1){
cout << "You searched for: " << endl;
CS101[index].printStudent();
}
else
cout << "Student is not on the list.\n" ;
index = CS101.low_GPA(CS101, size);
if (index != -1){
cout << endl << endl;
cout << "The student with the lowest GPA is: " << endl;
CS101[index].printStudent();
}
index = CS101.high_Units(CS101, size);
if (index != -1){
cout << "The student with the highest number of units is: " << endl;
CS101[index].printStudent();
}
}
fin.close();
return 0;
}
Input File: student.txt
Joe
Smith Jr.
Electrical Engineering
45 3.45
Nancy Karen
Brown
Industrial Engineering
34 4.0
Adam
Jonhson
Computer Science
56 3.89
Gorden
Khalbandy
Mechanical Engineering
89 3.28
Alan
Jackson
Computer Science
34 3.95