A
acheron05
Hi,
I've been writing a program for another school assignment but I am
having trouble working out how to overload the < and << operators. The
program is designed to read data from a file, create Date objects,
place them in a vector, sort them and then output the results. The <
operator needs to be overloaded to allow the container filled with the
Date objects to be sorted and the insertion operator << needs to be
overloaded to output the sorted elements to cout. What am I doing
wrong?
Here is what I have done so far. I've got the vector and fstream
working properly I believe.
Thanks for any help.... I'm really stuck.....
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
// Use this enumeration to symbolically represent the months of the
// year. Note that JAN is initialized to 1 because by default,
// enumerations start at 0.
enum months { JAN=1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV,
DEC };
// Date class from Assignment 1. This needs to be modified for this
assignment
class Date
{
public:
// This is the only constructor we allow
Date(int y, int m, int d) : year(y), month(m), day(d) { cout<<y<<"
"<<m<<" "<<d<<endl; }
// Destructor does nothing, but I've put it here anyway
~Date() {}
// Check that the year, month, and day corresponds to a valid date
bool isValid(void);
// Check for a leap year
bool isLeapYear(void);
friend bool operator < (const Date& d1, const Date& d2);
friend ostream& operator << (ostream& os, const Date& d);
private:
int year, month, day;
};
// Use this to display who you are
void displayInfo(void);
int main(int argc, char* argv[])
{
// Display who you are
displayInfo();
// Your code here
char endLine[10];
int y;
int m;
int d;
string filename = "/a4-input.txt";
vector<Date> DateItems;
ifstream fin(filename.c_str());
while(!fin.eof())
{
fin >> y;
fin >> m;
fin >> d;
fin.getline(endLine, 10);
Date DateRecord(y, m, d);
DateItems.push_back(DateRecord);
}
fin.close();
sort(DateItems.begin(), DateItems.end());
for(unsigned int i = 0; i < DateItems.size(); i++)
{
cout<<DateItems;
}
return 0;
}
//------------------------------------------------------------
// supply your information in the function below .
void displayInfo()
{
cout << "----------------------------------------" << endl;
cout << "Assignment 4 Semester 1 2006" << endl;
cout << " Submitted by: Duck, Donald, 000000000" << endl;
cout << "----------------------------------------" << endl;
}
bool operator < (const Date& d1, const Date& d2)
{
if(d1 < d2)
{
return true;
}
else
{
return false;
}
}
ostream& operator << (ostream& os, const Date& d)
{
return os;
}
// Is the date a leap year? You do not need to modify this function.
bool Date::isLeapYear(void)
{
return (year%400==0) || (year%4==0 && year%100!=0);
}
// Is the date valid? You do not need to modify this.
bool Date::isValid(void)
{
// Test in the special case of February
if (month==FEB)
{
// Is this a leap year?
if ( isLeapYear() )
return (day>=1 && day<=29); // Feb has 29 days
else
return (day>=1 && day<=28); // Otherwise, 28 days
}
// Test if the case is a month with 31 days
else if (month==JAN || month==MAR || month==MAY || month==JUL ||
month==AUG || month==OCT || month==DEC)
{
return (day>=1 and day<=31);
}
// Test in the case of a 30 day month
else if (month==APR || month==JUN || month==SEP || month==NOV)
return (day>=1 and day<=30);
// Otherwise this is an invalid month
else
return false;
}
I've been writing a program for another school assignment but I am
having trouble working out how to overload the < and << operators. The
program is designed to read data from a file, create Date objects,
place them in a vector, sort them and then output the results. The <
operator needs to be overloaded to allow the container filled with the
Date objects to be sorted and the insertion operator << needs to be
overloaded to output the sorted elements to cout. What am I doing
wrong?
Here is what I have done so far. I've got the vector and fstream
working properly I believe.
Thanks for any help.... I'm really stuck.....
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
// Use this enumeration to symbolically represent the months of the
// year. Note that JAN is initialized to 1 because by default,
// enumerations start at 0.
enum months { JAN=1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV,
DEC };
// Date class from Assignment 1. This needs to be modified for this
assignment
class Date
{
public:
// This is the only constructor we allow
Date(int y, int m, int d) : year(y), month(m), day(d) { cout<<y<<"
"<<m<<" "<<d<<endl; }
// Destructor does nothing, but I've put it here anyway
~Date() {}
// Check that the year, month, and day corresponds to a valid date
bool isValid(void);
// Check for a leap year
bool isLeapYear(void);
friend bool operator < (const Date& d1, const Date& d2);
friend ostream& operator << (ostream& os, const Date& d);
private:
int year, month, day;
};
// Use this to display who you are
void displayInfo(void);
int main(int argc, char* argv[])
{
// Display who you are
displayInfo();
// Your code here
char endLine[10];
int y;
int m;
int d;
string filename = "/a4-input.txt";
vector<Date> DateItems;
ifstream fin(filename.c_str());
while(!fin.eof())
{
fin >> y;
fin >> m;
fin >> d;
fin.getline(endLine, 10);
Date DateRecord(y, m, d);
DateItems.push_back(DateRecord);
}
fin.close();
sort(DateItems.begin(), DateItems.end());
for(unsigned int i = 0; i < DateItems.size(); i++)
{
cout<<DateItems;
}
return 0;
}
//------------------------------------------------------------
// supply your information in the function below .
void displayInfo()
{
cout << "----------------------------------------" << endl;
cout << "Assignment 4 Semester 1 2006" << endl;
cout << " Submitted by: Duck, Donald, 000000000" << endl;
cout << "----------------------------------------" << endl;
}
bool operator < (const Date& d1, const Date& d2)
{
if(d1 < d2)
{
return true;
}
else
{
return false;
}
}
ostream& operator << (ostream& os, const Date& d)
{
return os;
}
// Is the date a leap year? You do not need to modify this function.
bool Date::isLeapYear(void)
{
return (year%400==0) || (year%4==0 && year%100!=0);
}
// Is the date valid? You do not need to modify this.
bool Date::isValid(void)
{
// Test in the special case of February
if (month==FEB)
{
// Is this a leap year?
if ( isLeapYear() )
return (day>=1 && day<=29); // Feb has 29 days
else
return (day>=1 && day<=28); // Otherwise, 28 days
}
// Test if the case is a month with 31 days
else if (month==JAN || month==MAR || month==MAY || month==JUL ||
month==AUG || month==OCT || month==DEC)
{
return (day>=1 and day<=31);
}
// Test in the case of a 30 day month
else if (month==APR || month==JUN || month==SEP || month==NOV)
return (day>=1 and day<=30);
// Otherwise this is an invalid month
else
return false;
}