Hello,
trying to write a simple date class and convert the user input date from something like 02 01 1987 to 02JAN1987. Any help would be greatlly appreciated.
Aquaman
Printable View
Hello,
trying to write a simple date class and convert the user input date from something like 02 01 1987 to 02JAN1987. Any help would be greatlly appreciated.
Aquaman
Can you post the code that you have so far?
time_t now; char * loctime;
struct tm *ptr;
time(&now);
ptr = localtime(&now);
loctime = asctime(ptr);
printf("\nThe local time is: %s",loctime);
// Should print something like:
// The local time is: Sat Sep 15 16:49:12 2001
Make sure you include this header file -> time.h
#include <iostream>
using namespace std;
class date {
int day;
int month;
int year;
enum month {JAN = 1, FEB = 2, MAR = 3, APR = 4, MAY = 5, JUN = 6, JUL = 7,
AUG = 8, SEP = 9, OCT = 10, NOV = 11, DEC = 12};
public:
Date( int = 1, int = 1, int = 2000 ); // default constructor
void print();
};
date::date(int dd, int mm, int yyyy)
{
day = dd;
month = mm;
year = yyyy;
}
void date::print()
{
cout << "The date is:" << endl;
cout << "Day: " << day << endl;
cout << "Month: " << month << endl;
cout << "Year: " << year << endl;
}
typedef date Date;
int main()
{
int day, month, year;
Date today;
cout << "Enter today's date in DD MM YYYY format: ";
cin >> day >> month >> year;
// today.init(day, month, year);
// today.print();
exit(0);
}
Instead of making an enum for the months, you could make an array of strings to hold the name of each month:
char month[][] = { "JAN", "FEB", "MAR", ... etc ... };
Then, to print it out, you make sure that the value for the month is 1 <= month <= 12, so then you would decrement that value by one, and print the corresponding month name:
int _month;
cin >> _month;
cout << month[_month - 1];
Two more comments:
On this line: ' Date( int = 1, int = 1, int = 2000 ); // default constructor'
The word Date is capitalized, but it isn't anywhere else. To get the program to compile correctly, make sure they are both spelled exactly the same (even capitalization).
Also, you have a variable named month and an enum named month. One of these should change otherwise the compiler might become 'confused'.
Zach,
thank you for your suggestions. But I am still getting errors. Is the syntax correct for this line of code? char month[][] = {"asddas...
Also the print suggestion you gave me, should that go in the the body of the void date::print() or int main()
thanks again
Michael