1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#include <iostream>
#include <fstream>
#include <string>
#include "School.h"
void mygetline(istream& is, string &str);
using namespace std;
School::School()
{
set_name("Blank");
m_num_students = 0;
m_num_teachers = 0;
m_record_number = 0;
}
School::School(string name, int record_number)
{
set_name(name);
m_num_students = 0;
m_num_teachers = 0;
set_record_number(record_number);
}
School::School(string name, int num_students, int num_teachers, int record_number)
{
set_name(name);
set_num_students(num_students);
set_num_teachers(num_teachers);
set_record_number(record_number);
}
string School::get_name() const
{
return m_name;
}
int School::get_num_students() const
{
return m_num_students;
}
int School::get_num_teachers() const
{
return m_num_teachers;
}
int School::get_record_number() const
{
return m_record_number;
}
void School::set_name(string name)
{
unsigned int len = name.length();
if(len > 25)
len = 25;
name.copy(m_name,len);
m_name[len] = '\0';
}
void School::set_num_students(int num_students)
{
if(num_students >= 0)
m_num_students = num_students;
else
cout << "Error, students can not be negative." << endl;
}
void School::set_num_teachers(int num_teachers)
{
if(num_teachers >= 0)
m_num_teachers = num_teachers;
else
cout << "Error, teachers can not be negative." << endl;
}
void School::set_record_number(int record_number)
{
if(record_number >= 0)
m_record_number = record_number;
else
cout << "Error, record number has to be 0 or greater." << endl;
}
bool School::write_record(fstream& os)
{
os.seekg(m_record_number * sizeof(School));
os.write(reinterpret_cast<char*>(this), sizeof(School));
return true;
}
bool School::read_record(fstream& is)
{
is.seekg(m_record_number * sizeof(School));
is.read(reinterpret_cast<char*>(this), sizeof(School));
return true;
}
ostream& operator << (ostream& os, const School& aSchool)
{
os << "School name: " << aSchool.get_name() << endl;
os << "# of students: " << aSchool.get_num_students() << endl;
os << "# of teachers: " << aSchool.get_num_teachers() << endl;
os << "Record # : " << aSchool.get_record_number() << endl;
return os;
}
istream& operator >> (istream& is, School& aSchool)
{
string name;
int students, teachers, record_number;
if(is == cin)
cout << "Enter name of school: ";
mygetline(is, name);
if(is.eof() == true)
return is;
if(is == cin)
cout << "Enter number of students: ";
is >> students;
if(is == cin)
cout << "Enter number of teachers: ";
is >> teachers;
if(is == cin)
cout << "Enter record number: ";
is >> record_number;
aSchool.set_name(name);
aSchool.set_num_students(students);
aSchool.set_num_teachers(teachers);
aSchool.set_record_number(record_number);
return is;
}
void mygetline(istream& is, string &str)
{
str = ""; // remove old contents
if (is != cin && is.peek() == '\n') // skip to next line if needed
is.get();
char ch;
while((ch=is.get()) != '\n' && ch != EOF) // keep reading chars until
str += ch; // end of line or file reached
}
|