Hello everybody.
I am having problems with this program:
The program has to accept 5 dates that the user inputs..Code:#include <iostream> #include <fstream> using namespace std; class Date { private: int month; int day; int year; static char slash; public: void setDate(); void showDate(); void setMonth(); void setDay(); void setYear(); }; char Date::slash = '/'; void Date::setDate() { cin >> month; cin.get(); cin >> day; cin.get(); cin >> year; cin.get(); } void Date::setMonth() { if(month > 12 || month == 0) month = 12; else month = month; } void Date::setDay() { if(day > 31 || day == 0) day = 31; else day = day; } void Date::setYear() { if(year <= 999 || year > 2500) year = 2007; else year = year; } void Date::showDate() { cout << month << slash << day << slash << year << endl; } void main() { Date aDate[5]; cout << "Please enter five dates." << endl; cout << "You cant enter any slashes(/), only numbers(12 3 2007)" << endl; ofstream out("H:\\Dates.txt"); for(int x = 0; x < 5; ++x) { aDate[x].setDate(); aDate[x].setMonth(); aDate[x].setDay(); aDate[x].setYear(); out.write((char*)(&aDate[x]), sizeof(aDate[x])); } // end of for cout << endl << "The dates you entered were: " << endl << endl; for(int x = 0; x < 5; ++x) aDate[x].showDate(); cout << endl; system("pause"); }
Like this:
12 3 1987
11 4 2000
And so on. then the program outputs the dates entered whit slashes like this:
12/3/1987
11/4/2000
The problem is that I want to save the dates entered in a .txt file. I've done this before but never with objects. Since the data objects are type int and I think that I need to use the write() function, that only accept chars.
I need to save the dates without the slashes and then open the document's data in he program, I think I need the read() function but I cant even save what I want in the first place.
So whats wrong with my code?
Thanks to everyone for their time.