Any assistance is really appreciated. I am working on a program using classes (first timer). The program I am trying to make has a class called Date. It is suppose to display the date in format 01/01/17. But in the program, I need to get the date set a new date and update format to January 1st 2017. I need to do the same thing again but with format 1 January 2017. If any one has experience this I would appreciate it. I placed what I have so far below.
Please update your post closing the code tags with /code instead of \code (the other slash).
I think you are missing an s in your current line 29.
In your current line 60, you probably want to cout the returned string. Actually line 59, 60 and 61 could be a single line: cout << "The current date is \n" << d1.getdate() <<"\n\n";
Finally, are you sure you are supposed to write your own "Date" class with a hard coded string?
If I were a teacher (which I am not) I would like a student to get the current date and display that in the prescribed formats. http://www.cplusplus.com/reference/ctime/strftime/ http://www.cplusplus.com/reference/chrono/system_clock/now/
Anyway, if you have to change the format, having it as a string is not the most convenient representation, consider a variable that contains the day of the month, a variable that contains the month and a variable that contains the year.
class Date{
public:
Date(int d, int m, int y)
{
day = d;
month = m;
year = y;
d1 = std::to_string(day) + "/"+std::to_string(month)+"/"+std::to_string(year);
// alternatively you could also use http://www.cplusplus.com/reference/cstdio/snprintf/
}
private:
int day;
int month;
int year;
string d1;
};
If you need to change the format it doesn't help if you only use strings. In fact, if your Date class only contains a string and a set and get function, you could just delete it and use a regular sting.
Also, if you have a get function and a set function, probably your variable should not be public.
So I do need to be able to do a getDate, setDate and display date. Would I set that up in the actually class? Or would I use the initialize program? Nico, I used yours for this example. Thank you. Would it look like below?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class Date{
public:
Date(int d, int m, int y)
{
day = d;
month = m;
year = y;
d1 = std::to_string(day) + "/"+std::to_string(month)+"/"+std::to_string(year);
void getDate();
void setDate();
void displayDate();
}
private:
int day;
int month;
int year;
string d1;
};
class Date{
public:
Date(int d, int m, int y) // this is a function implementation, you can't define other functions inside of it.
// Maybe it would have been more clear if I only put a declaration here and put the implemantation later on (next to the implementation of getDate()) but I was lazy.
{
day = d;
month = m;
year = y;
// the above makes sure that the three variables in the Date class have the proper values from now on.
// the line below is one way (of many possible ways) to create a date string using those variables
d1 = std::to_string(day) + "/"+std::to_string(month)+"/"+std::to_string(year);
}
void getDate(); // you still need to implement this function somewhere
void setDate(); // you still need to implement this function somewhere
void displayDate(); // you still need to implement this function somewhere
private:
int day;
int month;
int year;
string d1;
};