I have a Date class which I am trying to test with a simple program to ask the user to input a date in a certain format which will then be put into the class. The idea is to allow the user to set a Date class from any string of text starting with the correct format.
Here is my date class header file (the implimentations of the other functions aren't relevant to this post):
#ifndef DATE_HPP_
#define DATE_HPP_
#include <iostream>
#include <cstdio>
class Date {
public:
int Year;
int Month;
int Day;
int HH;
int MM;
int ss;
Date();
int getTotalSeconds();
/*
* Overloaded Operator Functions
*/
//Assignments
Date operator=(Date input);
//Comparisons
bool operator==(Date& rhs);
bool operator!=(Date& rhs);
bool operator<(Date& rhs);
bool operator>(Date& rhs);
bool operator<=(Date& rhs);
bool operator>=(Date& rhs);
//Conversion
operator char*();
operator std::string();
//Declared as member functions
std::ostream& operator<<(std::ostream& os){
os << "operator<<: " << this->Year << '-' << this->Month << '-' << this->Day << '-' << this->HH << ':' << this->MM << ':' << this->ss;
return os;
}
std::istream& operator>>(std::istream& is){
char input[20];
is >> input;
scanf(input,"%04d-%02d-%02d-%02d:%02d:%02d",Year,Month,Day,HH,MM,ss);
is.clear();
return is;
}
};
#endif
My test program looks like this:
#include <iostream>
#include "Date.hpp"
int main(int argc, char* argv[]){
Date date;
std::cout << "Date initialized, printing: \n" << date << std::endl;
std::cout << "This is a test of the date library!\nPlease enter a date in the form YYYY-MM-DD-HH:mm:ss: ";
std::cin >> date;
std::cout << "\n\nDate reset, printing:\n" << date << std::endl << "Exit!\n";
return 0;
}
I don't know exactly what I am doing wrong. I've been looking up information on overloading operators and the operator<< works great! (I compiled and tested everything before I tried overloading operator>>) If it helps, I am using gcc on arch linux.