How to Calculate Tomorrow's date, when leap years don't matter
May 7, 2012 at 5:12pm May 7, 2012 at 5:12pm UTC
I'm trying to make a program to calculate tomorrow's date, but I've come across some problems. Leap years don't matter, February always has 28 days. A loop must be used.
Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
//MydateClass.h – MyDate class definition
#include <iostream>
using std::cout;
using std::endl;
//declaration section
class MyDate
{
public :
MyDate();
void SetDate(int , int , int );
void DisplayDate();
void UpdateDate();
private :
int month;
int day;
int year;
};
//implementation section
MyDate::MyDate()
{
month = 0;
day = 0;
year = 0;
} //end of default constructor
void MyDate::SetDate(int n1,int n2, int n3)
{
month = n1;
day = n2;
year = n3;
} //end of SetDate method
void MyDate::DisplayDate()
{
cout << month << "/" << day << "/" << year;
} //end of DisplayDate method
void MyDate::UpdateDate()
{
if (day = 31; && (month = 1; || month = 3; || month = 5; || month = 7; || month = 8; || month = 10; || month = 12))
{
if (month = 12)
{
day = 1;
year = year + 1;
}
else
{
day = 1;
month = month + 1;
}
}
if (day = 30 && (month = 4|| month = 6 || month = 9 || month = 11))
{
day = 1;
month = month + 1;
}
if (day = 28 && month = 2)
{
day = 1;
month = month + 1;
}
else
{
day = day + 1;
} //end of UpdateDate method
Main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
//Date.cpp – displays an item name and a quantity in inventory
#include <iostream>
#include "c:\Users\Documents\Visual Studio 2008\Projects\Date solution\Date\MydateClass.h"
using std::cout;
using std::cin;
using std::endl;
int main()
{
//declare a date object
MyDate today;
//declare variables
int todayMonth = 0;
int todayDay = 0;
int todayYear = 0;
//get month,day, and year from user
cout << "Enter the month (1-12): " ;
cin >> todayMonth;
cout << "Enter the day (1-31): " ;
cin >> todayDay;
cout << "Enter the year: " ;
cin >> todayYear;
//use public member method to assign values to private data members
today.SetDate(todayMonth, todayDay, todayYear);
//use public member method to display values in private data members
cout << endl << "Today is " ;
today.DisplayDate();
cout << "." << endl;
today.UpdateDate();
cout << "Tomorrow is " ;
today.DisplayDate();
cout << "." << endl;
return 0;
} //end of main function
I have multiple errors when I do this, but I don't see how else to do the problem. I also do not know where to incorporate a loop.
Last edited on May 7, 2012 at 5:17pm May 7, 2012 at 5:17pm UTC
May 7, 2012 at 5:44pm May 7, 2012 at 5:44pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//declaration section
class MyDate
{
public :
MyDate();//default constructor
MyDate(int , int , int );// overloading constructor
void DisplayDate();
void UpdateDate();
private :
int month;
int day;
int year;
};
MyDate::MyDate(int n1,int n2, int n3)
{
month = n1;
day = n2;
year = n3;
}
May 7, 2012 at 6:00pm May 7, 2012 at 6:00pm UTC
1 2 3
if (day = 31; && (month = 1; || month = 3; || month = 5; || month = 7; || month = 8; || month = 10; || month = 12))
// should be:
if (day == 31 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12))
Note the use of
== and the lack of semi-colons. == is comparison. = is assignment.
Don't forget to update the other if() statements!
Last edited on May 7, 2012 at 6:02pm May 7, 2012 at 6:02pm UTC
May 7, 2012 at 11:11pm May 7, 2012 at 11:11pm UTC
I was told to incorporate a loop into the program, and this would make it more efficient. How would I do this?
Date class (Where the errors were)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
//MydateClass.h – MyDate class definition
#include <iostream>
using std::cout;
using std::endl;
//declaration section
class MyDate
{
public :
MyDate();
void SetDate(int , int , int );
void DisplayDate();
void UpdateDate();
private :
int month;
int day;
int year;
};
//implementation section
MyDate::MyDate()
{
month = 0;
day = 0;
year = 0;
} //end of default constructor
void MyDate::SetDate(int n1,int n2, int n3)
{
month = n1;
day = n2;
year = n3;
} //end of SetDate method
void MyDate::DisplayDate()
{
cout << month << "/" << day << "/" << year;
} //end of DisplayDate method
void MyDate::UpdateDate()
{
if (day == 31 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12))
{
if (month == 12)
{
month = 1;
day = 0;
year = year + 1;
}
else
{
day = 0;
month = month + 1;
}
}
if (day == 30 && (month == 4|| month == 6 || month == 9 || month == 11))
{
day = 0;
month = month + 1;
}
if (day == 28 && month == 2)
{
day = 1;
month = month + 1;
}
else
{
day = day + 1;
}
} //end of UpdateDate method
Topic archived. No new replies allowed.