0

Days since 1/1/1800,that day was Wednesday. Need to convert a date in the form mo/dy/year into this single number. Need a function to print out the day of the week, and a function to print out the date in the form month day, year where the month is completely spelled out. Need to write a program that uses this class to read data from a file and print the data in a new format on the standard output device. Having trouble converting a date in the form mo/dy/year ino the single number. The code I'm stuck on is in bold bellow, thanks in advance.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

// DaysFrom1800 Class Definition
class DaysFrom1800 {
public:
    DaysFrom1800();
    int numDaysFrom1800 (int day, int month, int year);
    int StartOfYear (int year);
    int fillDaysArray (int numberOfDates, int day[], int month[], int year[], string textArray[], DaysFrom1800 numbers []);
    void printDaysSince1800 ();
private:
    int daysSince;
};

// Prototypes
bool CalculateDate (int dayNumber, int day, int month, int year);
bool leapCheck (int year);
void prettyPrint (int numberOfDates, string textArray[], DaysFrom1800 numbers[]);

int main () {
// Declare Day, Month and Year Array to store Date Values
int day[200];
int month [200];
int year [200];

int numberOfDates = 0;
DaysFrom1800 numbers[200];
DaysFrom1800 dummy;
string textArray [200];

numberOfDates = dummy.fillDaysArray (numberOfDates, day, month, year, textArray, numbers);
for (int i=0; i <numberOfDates; i++) {
    cout << month[i] << "/" << day[i] << "/" << year[i] << endl;
}
for (i=0; i < numberOfDates; i++) {
    numbers[i].printDaysSince1800();
    cout << endl;
}
for (i = 0; i <numberOfDates; i++) {
    cout << textArray[i];
    cout << endl;
}
prettyPrint(numberOfDates, textArray, numbers);
return 0;
}

// Defualt Constructer for DaysFrom1800 Class
DaysFrom1800::DaysFrom1800() : daysSince (0) {
// Nothing to be done here...
}
[B]int DaysFrom1800::StartOfYear(int year){
int leapCount =0;
int days = 0;

// For loop to find the number of leap years
for (int i=1800; i < year; i++) {
        if (leapCheck(i))
            leapCount ++;
    }
 return((year-1800-leapCount)*365) + (leapCount*366);   


int daysPassed [12] =
{
     0,   // Jan 1
    31,   // Feb 1
    59,   // Mar 1 (non leap year)
    90,   // Apr 1 (non leap year)
   120,   // ...
   151,
   181,
   212,
   243,
   273,
   304,   // ...
   334    // Dec 1 (non leap year)
};

// takes a dayNumber and calculates the day/month/year
bool CalculateDate (int dayNumber, int &day, int &month, int &year)
{
   // make a first guess at the year
   int dayNumber= days;
   int yearGuess = dayNumber/365 + 1800;

   // calculate the day number of the start of that year
   int yearStart = StartOfYear (yearGuess);
   bool isLeapYear = leapCheck(yearGuess);
   int daysLeftInYear = dayNumber - yearStart;

   while (daysLeftInYear < 0)
      {
      yearGuess--;
      yearStart = StartOfYear (yearGuess);
      isLeapYear = leapCheck(yearGuess);
      daysLeftInYear = dayNumber - yearStart;
      }

   while ((isLeapYear && daysLeftInYear >= 365) || (!isLeapYear && daysLeftInYear >= 364))
      {
      yearGuess++;
      yearStart = StartOfYear (yearGuess);
      isLeapYear = leapCheck(yearGuess);
      daysLeftInYear = dayNumber - yearStart;
      }

   // now yearGuess is the correct year and 0 <= daysLeftInYear < 365 (or 366)
   year = yearGuess;

   int m;
   int daysLeftInMonth = daysLeftInYear ;

   for (m=0; m<12; m++)
      {
      // find the first month whose starting date is larger than daysLeftInYear
      if (daysLeftInYear < daysPassed[m])
         break;

      daysLeftInMonth = daysLeftInYear - daysPassed[m];
      }

   // m = month if this is not a leap year
   // daysLeftInMonth are the number of days past the begining of the previous month

   // first take care of the non-leap year case
   if (!isLeapYear || m == 1 || m == 2)
      {
      // either not a leap year or January or Febuary of a leap year
      month = m;
      day = daysLeftInMonth + 1;
      return true;
      }

   // for leap years, the extra day subtracts one from the days left in the month
   // and might move the day into a different month
   daysLeftInMonth --;

   if (daysLeftInMonth < 0)
      {
      m --;
      daysLeftInMonth = daysLeftInYear - daysPassed[m-1];

      if (m > 2)
         daysLeftInMonth --;
      }

   month = m;
   day = daysLeftInMonth + 1;
   return true;
	}
}
[/B]
int DaysFrom1800::numDaysFrom1800 (int day, int month, int year) {
int days = StartOfYear();
bool thisYear = leapCheck(year);
switch (month) {
case 1:
    days = days + day;
    break;
case 2:
    if (day < 28)
        days = days + 31 + day;
        if (thisYear)
            days = days + 31 + 29;
        break;
case 3:
    if (thisYear)
        days = days +31+29+ day;
        else
        days = days + 31 + 28 + day;
        break;
case 4:
    if (thisYear)
        days = days + 31 + 29 + 31 + day;
        else
        days = days + 31 + 28 + 31 + day;
        break;
case 5:
    if (thisYear)
        days = days + 31 + 29 + 31 + 30 + day;
    else
        days = days +31 + 28 + 31 + 30 + day;
    break;
case 6:
    if (thisYear)
        days = days +31 + 29 + 31 + 30 + 31 + day;
    else
        days = days + 31 + 28 + 31 + 29 + 31 + day;
    break;
case 7:
    if (thisYear)
        days = days + 31 + 29 + 31 + 30 + 31 + 30 + day;
    else
        days = days + 31 + 28 + 31 + 30 + 31 + 30 + day;
    break;
case 8:
    if (thisYear)
        days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + day;
    else
        days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + day;
    break;
case 9:
    if (thisYear)
        days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + day;
    else
        days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + day;
    break;
case 10:
    if (thisYear)
        days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day;
    else
        days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day;
    break;
case 11:
    if (thisYear)
        days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + day;
    else
        days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + day;
    break;
case 12:
    if (thisYear)
        days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day;
    else
        days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day;
    break;
}

return days;

}

/*void DaysFrom1800::printDaysSince1800 () {
cout << daysSince;
}*/

int DaysFrom1800::fillDaysArray (int numberOfDates, int day[], int month[], int year[], string textArray[], DaysFrom1800 numbers []) {

// Declare Stream and Open Text File
ifstream inStream;
inStream.open("in.txt");

// Test Input File
if (inStream.fail()) {
cerr << "Can't open file!\n";
exit(1);
}

// Declare and Initialize Index Variable
int i=0;

// Temp Character Array
char temp [100];

while (!inStream.eof()) {
// Get Dates from Text File Storing Info in day, month and year variables
inStream >> month[i] >> day[i] >> year[i];
inStream.get(temp, 100, '\n');
textArray[i]=temp;

// Increase Index by One
i++;
}

// Assign Index Value to numberOfDates variable to be returned in this function
numberOfDates = i;

// For Loop to Calculate Number of Days for Each Day and Store in Object Array
/*for (i=0; i<numberOfDates; i++) {
numbers[i].daysSince = numDaysFrom1800 (day[i], month[i], year[i]);
}
// Return Intever Variable numberOfDays
return numberOfDates;
}
*/
bool leapCheck(int year)
{
   // if the year is not 0 mod 4 then it is not a leap year
   if ((year % 4) != 0)
      return false;

   // every 4'th century IS a leap year
   if ((year % 400) == 0)
      return true;

   // every 4'th year that is not a century year is a leap year
   if ((year % 100) != 0)
      return true;
   else
      return false;
}

void prettyPrint (int numberOfDates, string textArray[], DaysFrom1800 numbers[]) {
    for (int i=0; i<numberOfDates; i++) {
    cout << "On ";
    // Call function to print date from day number
    cout << textArray[i] << '(';
    numbers[i].printDaysSince1800();
    cout << ')' << endl;
    }
}

Having trouble converting a date in the form mo/dy/year into a single number

2
Contributors
1
Reply
2
Views
12 Years
Discussion Span
Last Post by jwenting
0

I see you haven't yet grapsed the concept of encapsulation of data.

Why do you keep your months, years and days outside your class?
Wouldn't it be a lot better to have an array of dates instead of 3 arrays of the parts of that array?
That would enable you to move the parser into the class as well and avoid a lot of parameter passing.

Where does your function go wrong?
It looks overly complex. If you're loosing grasp of what your code is doing usually it's a good sign you'd better redesign it.

Think about how you should go about implementing the calculation on paper, then code to do that calculation.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.