Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Program not initializing the date correctly

$
0
0
So for this assignment our teacher provided most of the code and had us implement the Initialize, GetMonthAsString, leapYear, and Adjust functions. The code compiles but it is not recognizing the date entered by the user. I don't know what to do and even the teacher didn't know what I had done wrong. I think he is new and he even stated that he doesn't know C++ all that well so I'm not sure why he is teaching this class. So I was hoping someone on here could help because I can't figure out what's wrong. I'm assuming it has to be this Initialize function:
void DateType::Initialize (int newMonth, int newDay, int newYear)
// Post: If newMonth, newDay and newYear represent a valid date,
//       year is set to newYear;
//       month is set to newMonth;
//       day is set to newDay;
//       otherwise a string exception is thrown, stating the
//       first incorrect parameter.
{
         if (newYear > 1900)
	     newYear = year;
	 else
	     throw string("Year Invalid");
		
	 if (newMonth < 13 || newMonth > 0)
	     newMonth = month;
	 else
	     throw string("Month Invalid");

	 if(newDay < daysInMonth[month] || newDay > 0)
	     newDay = day;
	 else
	     throw string("Day Invalid");
}



Just in case here is the whole Implementation program and the class:
Class:
#include <string>
#include <fstream>

using namespace std;

// Declare a class to represent the Date ADT
// This is file DateType.h.

class DateType
{
public:
	void Initialize(int newMonth, int newDay, int newYear);
	int GetMonth() const;					// returns year
	int GetYear() const;					// returns month
	int GetDay() const;						// returns day
	string GetMonthAsString() const;		// returns month as a string
	bool leapYear(int) const;
	DateType Adjust(int daysAway) const;
private:
	int  year;
	int  month;
	int  day;				
};



Implementation File:
#include "DateType.h"
#include <fstream>
#include <iostream>
using namespace std;

// Nmber of days in each month
static int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30,
                            31, 30, 31};	

// Nmaes of the months
static string conversionTable[] = {"Error", "January", "February", 
    "March", "April", "May", "June", "July", "August", "September",
    "October", "November", "December"};

void DateType::Initialize (int newMonth, int newDay, int newYear)
// Post: If newMonth, newDay and newYear represent a valid date,
//       year is set to newYear;
//       month is set to newMonth;
//       day is set to newDay;
//       otherwise a string exception is thrown, stating the
//       first incorrect parameter.
{
         if (newYear > 1900)
	    newYear = year;
	 else
	    throw string("Year Invalid");
		
	 if (newMonth < 13 || newMonth > 0)
	    newMonth = month;
	 else
	    throw string("Month Invalid");

	 if(newDay < daysInMonth[month] || newDay > 0)
	    newDay = day;
	 else
	    throw string("Day Invalid");
}
int DateType::GetMonth() const
// Accessor function for data member month.
{
   return month;
}

string DateType::GetMonthAsString() const
// Returns data member as a string
{
   return conversionTable[month];
}

int DateType::GetYear() const
// Accessor function for data member year.
{
   return year;
}

int DateType::GetDay() const
// Accessor function for data member day.
{
   return day;
}

bool DateType::leapYear(int year) const
//
// 
//
{
	if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 ==0))
		return true;
	else 
		return false;
}

DateType DateType::Adjust(int daysAway) const
// Pre:  Self has been initialized
// Post: Function value = newDate daysAway from self
{
   DateType returnDate;
   int newDay = day + daysAway;
   int newMonth = month;
   int newYear = year;
   
   bool finished = false;
   
   while (!finished)
   {
		int daysInThisMonth = daysInMonth[newMonth];
		if (newMonth == 2)
		{
			if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 ==0))
				daysInThisMonth++;
			if(newDay <= daysInThisMonth)
				finished = true;
			else
			{
				newDay = newDay - daysInThisMonth;
				newMonth = (newMonth % 12) + 1;
				if (newMonth == 1)
					newYear++;
			}
		}
	returnDate.Initialize(newMonth, newDay, newYear);
	return returnDate;
   }
}



Thanks in advance for any help!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>