How to convert "MM/DD/YYYY to "MonthName day, year" factoring leap years?

Oct 7, 2014 at 1:18am
I've only had 4 classes about learning C++ so far so I am a very novice programmer. My prof. handed out this assignment today and I am completely lost. I have to write a program that only accepts a date b/w 1/1/1901 and 12/31/2099 from the keyboard and then convert it from "month/day/year" to "month-name day, year" and print the date in that new format. For example "11/12/2013" read from keyboard to "November 12, 2013". I also have to factor in leap years (which is about the only thing I know how to do right now). He said we must use both 'if' and 'switch' statements, and no arrays.

Each class I take with my prof. is 1.5 hrs lecture and 1.5 hrs practicing with C++. He jams so much information in each session it's hard to understand everything. Not to mention he has a very thick accent....

Last edited on Oct 7, 2014 at 1:21am
Oct 7, 2014 at 1:22am
1
2
3
4
5
6
7
string month;
if(m == 1)
    month = "January";
....

if(m == 12)
    month = "December";


I assume you know how to read input and print output.
Oct 7, 2014 at 1:30am
@shadowCODE

Yes, that makes sense. Now, another quick question. How do I get it to read the slashes when in input a date?
Oct 7, 2014 at 1:33am
@shadowCODE

Never mind i figured that part out already. However, I'm still confused on how to generate an if statement for dates in between 1/1/1901 and 12/31/2099
Oct 7, 2014 at 1:45am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool datecheck(int m, int d, int y)
{
    if(m < 1 || m > 12)
    {
         cout<<"invalid month"<<endl;
         return false;
     }
     if(d < 0 || d > 31)
     {
         cout<<"Invalid day"<<endl;
         return false;
     }
     if(y < 1901 || year > 2099)
     {
          cout<<"invalid year"<<endl;
          return false;
     }
      
     return true;
}


1
2
3
4
5
6
7
8
9
10
int main()
{
   ....
  //after input of date

   if(datecheck(m,d,y))
       ..continue...
    else
       ..take proper action
}


If your prof. does not want functions, you can aswell just put the datecheck logic in main
Oct 7, 2014 at 1:56am
its about slash position,

.1.3....
.1..4....
..2.4....
..2..5....

then convert first one or two character into monthName
Oct 7, 2014 at 2:32am
Thank you!
Topic archived. No new replies allowed.