Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
namespace { // Locals
const char spc[] = " "; const char nl[] = "\n";
} // End of Locals...
/// YMD order ctor parameters!
struct Date
{
int year, month, day;
Date(int y = 1970, int m = 1, int d = 1)
:year(y),month(m),day(d) {}
};
ostream& operator << (ostream& os, const Date& d)
{
os << d.day << spc << d.month << spc << d.year;
return os;
}
/// Make more safe class later...
struct Student
{
string firstname;
string lastname;
string name() const { return firstname+" "+lastname; }
Date birthday;
unsigned attr;
Student(const string& fname, const string& lname,
Date& bdate, unsigned a):
firstname(fname), lastname(lname), birthday(bdate),
attr(a)
{}
string toString() const; // (home work;)
};
ostream& operator << (ostream& os, Student& st)
{
os << st.firstname + spc + st.lastname + nl;
os << st.birthday << nl;
char fch = os.fill('0'); // leading zeroes...
os << setw(8) << st.attr << nl;
os.fill(fch); // don't forget to restore fill char...
return os;
}
namespace { // Locals (continues)
class BadInput {}; // Bad Record exception
inline string& get(istream& in, string& word)
{
if (in >> word) // get next token
return word;
throw BadInput();
}
} // End of Locals
istream& operator >>(istream& in, Student& st)
{
string word;
try
{
get(in,st.firstname);
get(in,st.lastname);
get(in,word); st.birthday.day = atoi(word.c_str());
get(in,word); st.birthday.month = atoi(word.c_str());
get(in,word); st.birthday.year = atoi(word.c_str());
get(in,word); st.attr = atol(word.c_str());
}
catch(BadInput&)
{
//in.setf(ios_base::failbit); // add later...
}
return in;
}
ifstream fin("student1.txt");
Student st;
int n; // counter...
for (n = 0; fin >> st; ++n)
{
//...Process record...
}
if (fin.fail()) // Bad record occured...
...