Correct output for a calender program

//This program is suppose to display the date for several date objects
//declared in the main function. But after running the program I realize.
//That it's incrementing the last month to 13. Im not sure why.






#include "stdafx.h"
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;

class Date
{
private:
int month;
int day;
int year;
static string slash;
void setMonth(int);
void setDay(int, int);
void setYear(int);
public:
void IncreaseDay(int, int, int);
void setDate(int, int, int);
void showDate();
};
string Date::slash = "/";
void Date::setDate(int Mon, int Da, int Yea)
{
setMonth(Mon);
setDay(Da, Mon);
setYear(Yea);
IncreaseDay(Mon, Da, Yea);

}
void Date::setMonth(int Mon)
{
if(Mon > 12)
month = 12;
else
month = Mon;
}
void Date::setDay(int Da, int Mon)
{
if(Mon = 2 && Da < 29)
day = Da;
else
day = 28;
if((Mon = 1 || 3 || 5 ||7 ||9 || 10 || 12) && (Da < 32))
day = Da;
else
day = 31;
if((Mon = 4 || 6 || 8 || 11) && (Da < 31))
day = Da;
else
day = 30;
}
void Date::setYear(int Yea)
{
year = Yea;
}
void Date::IncreaseDay(int Mon, int Da, int Yea)
{
if((Mon = 2) && (Da > 28))
{
month++;
day = 1;
}
else
if((Mon = 1 || 3 || 5 || 7 || 9 || 10) && (Da >= 32))
{
month++;
day = 1;
}
else
if((Mon = 4 || 6 || 8 || 11) && (Da >= 31))
{
month++;
day = 1;
}
else
if((Mon >= 12) && (Da >= 32))
{
month = 1;
day = 1;
year++;
}
}

void Date::showDate()
{
cout << "Today's date is: " << month << slash << day << slash << year << endl;
}
int main()
{
Date date;
date.setDate(3, 6, 12);
date.showDate();

Date diffDate;
diffDate.setDate(5, 33, 13);
diffDate.showDate();

Date aDate;
aDate.setDate(5, 34, 14);
aDate.showDate();

Date newDate;
newDate.setDate(12, 29, 07);
newDate.showDate();
_getch();
return 0;
}
The 13 came from these lines:
1
2
3
4
5
6
7
8
void Date::IncreaseDay(int Mon, int Da, int Yea)
{
   // You don't want to use '=', use '==' for testing
   if((Mon = 2) && (Da > 28))
   {
       month++;
       day = 1;
   }

And you have to check each value in an if statement:
1
2
3
4
5
// Not correct
if((Mon = 1 || 3 || 5 || 7 || 9 || 10) && (Da >= 32))
// Correct
if((Mon == 1 || Mon == 3 || Mon == 5 || Mon == 7 ||
                 Mon == 9 || Mon == 10) && (Da >= 32))

Make theses changes throughout your code and you get:
 ./a.out
Today's date is: 3/30/12
Today's date is: 6/1/13
Today's date is: 6/1/14
Today's date is: 12/30/7


Last edited on
Topic archived. No new replies allowed.