I need to have my output in this format for my Class Date:
12/25/10
December 25, 2012
25 December 2012
The output I'm getting right now gives me ridiculous numbers for the day, year, and monthnum variables. Any advice?
#include <iostream>
#include <string>
using namespace std;
class Date
{ public:
int day;
int monthnum;
string month;
int year;
void convert (int day, int monthnum, int year)
{
if (monthnum == 1)
{ month = "January"; }
void print()
{
cout << "The date you entered would be:\n" << monthnum << "/" << day << "/" << year << endl;
cout << month << " " << day << ", " << year << endl;
cout << day << " " << month << " " << year << endl;
cin.get();
}
};
int main()
{
int day;
int monthnum;
int year;
cout << "Please enter a day of a month no greater than 31 and no less than 1: " << flush;
cin >> day;
cout << "Please enter a numerical value for a month no greater than 12 and no less than 1: " << flush;
cin >> monthnum;
cout << "Please enter a year: " << flush;
cin >> year;
Date user1;
user1.convert(day, monthnum, year);
if (day < 1 || day > 31)
{
cout << "The day you entered is not between 1 and 31" << endl;
cin.get();
}
else if (monthnum < 1 || monthnum > 12)
{
cout << "The numerical value for month you entered is not between 1 and 12" << endl;
cin.get();
}
Please post all code in code tags - it makes it much easier to read.
That said, you declare 'day' 'monthnum' and 'year' in main, and also as members of class Date. They aren't the same variables! One set exists in main, the other set as data members of the Date object. I would recommend a 'SetDate' method, which allows you to set the day, monthnum, and year on your 'user1' object from within main(). Also you might want to change your convert function - it also has its own copies of day, monthnum, and year. Perhaps make it a static member that takes an int (monthnum) as a parameter, and returns an std::string with the date instead of saving the name in each Date object?
@Lilmish you're a beginner. I think you did great as a beginner. Only minor errors. First off all,{user1.print();} ,user1.convert(day, monthnum, year); and void print() these can all go as its not needed.
1 2 3 4 5
cout<<"The day you entered:"<<day<<endl;
cout<<"The month you entered:"<<monthnum<<endl;
cout<<"The year you entered:"<<year<<endl;
cout<<"Your born date is:"<< day << "/" << monthnum << "/" << year <<endl;
cout<<endl;
This will go within the else in your main functions.
Change the way you implemented the class statement. Sorry, I cannot help you too much cause people will have a go at me lool.