This is for a class. Two of the files are provided, and I need to write the third.

I'm having trouble with a constructor in a class. The constructor is called with 'Logbook testLog(month,year)' and it saves the month and year correctly. The daysInMonth function works fine.

My problem is this: when I enter a value with the putEntry function, the days that are set to 0 only go up to the month's day. If the month is two, day one and two have a value of 0, and the rest are junk (something like 21345, I think the memory address.) I THINK it is the "Logbook::Logbook (int month, int year)" function, but I'm going to try something else as well.

#include "logbook.h"
#include <windows.h>

Logbook::Logbook ()
// Constructs an empty logbook for the specified month. 
{
	SYSTEMTIME st;

	logMonth = st.wMonth;
	logYear = st.wYear;
	
	for (int day = 1 ; day <= daysInMonth() ; day++)
	{
		entry[day] = 0;
	}
}

Logbook::Logbook (int month, int year)
// Constructs an empty logbook for the specified month. 
{

	logMonth = month;
	logYear = year;
	// make sure real month/year
	int MAXDay = daysInMonth();
	for ( day = 1 ; day <= daysInMonth() ; day++ )
	{
		entry[day] = 0;
	}
}

incomplete for space... here is the rest of the code.

void Logbook::putEntry ( int day, int value )

int Logbook::getEntry ( int day ) const

int Logbook::month () const

int Logbook::year () const

int Logbook::operator [] ( int day ) const

void Logbook::operator += ( const Logbook &rightLogbook )

int Logbook::daysInMonth () const

If you need more code, please tell!


(I understand the concept of a class, but I think I'm missing something.)

Nor sure what you are trying to do here.

I would like to see logbook.h and logbook.cpp please.

here is a link to logbook.h and here is a link to test1.ccp

Here is the full (but incomplete, as in not all functions are done) code to logbook.cpp.

// 

#include "logbook.h"
#include <windows.h>

Logbook::Logbook ()
// Constructs an empty logbook for the specified month. 
{
	SYSTEMTIME st;

	logMonth = st.wMonth;
	logYear = st.wYear;
	
	for (int day = 1 ; day <= daysInMonth() ; day++)
	{
		entry[day] = 0;
	}
}

Logbook::Logbook (int month, int year)
// Constructs an empty logbook for the specified month. 
{

	logMonth = month;
	logYear = year;
	// make sure real month/year
	int MAXDay = daysInMonth();
	for ( day = 1 ; day <= daysInMonth() ; day++ )
	{
		entry[day] = 0;
	}
}

void Logbook::putEntry ( int day, int value )
{
	int day = 1;
	if (0 < day && day <= logMonth)
		entry[day] = value;
}

int Logbook::getEntry ( int day ) const
{
	int day = 1;
	if (0 < day && day <= logMonth)
		return entry[day];
}

int Logbook::month () const
{
	return logMonth;
}

int Logbook::year () const
{
	return logYear;
}

int Logbook::operator [] ( int day ) const
{
	int day = 1;
	if (0 < day && day <= daysInMonth())
		return getEntry(day);
}

void Logbook::operator += ( const Logbook &rightLogbook )
{
	if (logMonth == rightLogbook.daysInMonth())
		for (int day = 1 ; day <= daysInMonth() ; day++)
		{
			int value  = getEntry(day) + rightLogbook.getEntry(day);
			putEntry(day, value);
		}	
}

int Logbook::daysInMonth () const
{
	switch (logMonth)
	{
		// If 
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
		{
			return 31;
			break;
		}
		case 4:
		case 6:
		case 9:
		case 11:
		{
			return 30;
			break;
		}
		case 2:
		{
			if ((logYear % 4) == 0)
			{
				if ((logYear % 100) == 0)
				{
					if ((logYear % 400) == 0)
					{
						return 29;
						break;
					}
					else
					{
						return 28;
						break;
					}
				}
				else
				{
					return 29;
					break;
				}
			}
			else
			{
				return 28;
				break;
			}
		}
	}
}

Oh, that is bad code, need to remove the " int day = 1;" line in putEntry and GetEntry.

oK... were to start....

(a) daysInMonth should be a simple function

static int Days[]={31,28,31,etc};
if (logMonth==2)
    { 
         //process leap year (carefully)
     }
else
   return Days[logMonth];
}

This code is much better written as a static function that takes a number. You will need it when you write dayInWeek but not for the month that you are in.


(b) Your error in put entry is that you check the day against the logMonth (ie the 13th is never going to work!). And just in case you set a local variable called day =1 first [was that a debug line??]

(c) The constructor : No tests what if I accidently call it with Logbook A(0,0); .
then obviously you get MAXDay and promptly don't use it. But the function doesn't need to use MAXDay. Since your array is ALWAYS 31 entrys so just use:

for(int i=0;i<31;i++)
    entry[day]=0;

Finally, return exits the function, so there is no need for a break afterwards. [Actually the is no need for the horrific case statement... ].

a. I WOULD do this, but the only file I can change is logbook.cpp. The other two are provided as default.

b. That was debug! I noticed it right after I posted it.

c. MAXDay was a debug thing as well, I was trying to test to see if I could make it loop another way.


I've added your sugested changes, but I must have missed something, because I have the same problem. Here is the code, and thank you for all of your help!

// 

#include "logbook.h"
#include <windows.h>

Logbook::Logbook ()
// Constructs an empty logbook for the specified month. 
{
	SYSTEMTIME st;

	logMonth = st.wMonth;
	logYear = st.wYear;
	
	for(int i=0;i<31;i++)
		entry[day]=0;
}

Logbook::Logbook (int month, int year)
// Constructs an empty logbook for the specified month. 
{

	logMonth = month;
	logYear = year;
	
	for(int i=0;i<31;i++)
		entry[day]=0;
}

void Logbook::putEntry ( int day, int value )
{

	if (0 < day && day <= daysInMonth ())
		entry[day] = value;
}

int Logbook::getEntry ( int day ) const
{

	if (0 < day && day <= daysInMonth ())
		return entry[day];
}

int Logbook::month () const
{
	return logMonth;
}

int Logbook::year () const
{
	return logYear;
}

int Logbook::operator [] ( int day ) const
{
	int day = 1;
	if (0 < day && day <= daysInMonth())
		return getEntry(day);
}

void Logbook::operator += ( const Logbook &rightLogbook )
{
	if (logMonth == rightLogbook.daysInMonth())
		for (int day = 1 ; day <= daysInMonth() ; day++)
		{
			int value  = getEntry(day) + rightLogbook.getEntry(day);
			putEntry(day, value);
		}	
}

int Logbook::daysInMonth () const
{
	switch (logMonth)
	{
		// If 
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
		{
			return 31;
			break;
		}
		case 4:
		case 6:
		case 9:
		case 11:
		{
			return 30;
			break;
		}
		case 2:
		{
			if ((logYear % 4) == 0)
			{
				if ((logYear % 100) == 0)
				{
					if ((logYear % 400) == 0)
					{
						return 29;
						break;
					}
					else
					{
						return 28;
						break;
					}
				}
				else
				{
					return 29;
					break;
				}
			}
			else
			{
				return 28;
				break;
			}
		}
	}
}

well that sort of works..

(a) you still have a junk code calculating the number of days.

(b) you have functions that return values BUT don't your compiler should tell you about that. -- NEVER allow warnings to go through.

(c) Think about what happens if day==31 in putEntry.

(d) you are forgetting in several places that C++ arrays count from zero and not 1.

Are you sure the link to test1.cpp is the right file?

It is calling members of Logbook that are not defined in logbook.h and if I understand correctly, both files were given and you're not supposed to change them.

Thank you for the replies!

As I said, this is for a class, and test1.cpp and logbook.h were not supposed to be changed. But, to debug, I started testing different things to see where the screw up was*. Looking at test1.cpp, I added tests before getEntry/putEntry loop (all code below) I tested both functions. But still, even though my code for the constructor and Entry functions seemed fine, I got junk data for days after the month. (As in if it was month two, slots 0, 1, 2 could store data, but the slots after could not)

So I copied and pasted the text to a new Microsoft C++ Project, made a few minor code changes, and it works. No, it is not finished at all! But, I made it past the big error, so things should work out. If you have further suggestions, post away.

* I happen to think that this is the more valuable lesson than classes or constructors. I was able to test functions and eliminate problems, untill I found the root of the problem because I was willing to do 'more' than what was required of me.)

//--------------------------------------------------------------------
//
//  Laboratory 1                                           test1.cpp
//
//  Test program for the operations in the Logbook ADT
//
//--------------------------------------------------------------------

#include <iostream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include "logbook.h"

using namespace std;

void main()
{
    int month,   // Input month
        year,    // Input year
        day,     // Input day
        entry,   // Input logbook entry
        dofw,    // Day of the week
        stop,    // Signals end of test
        j;       // Loop counter

    // Create a logbook (not used in Test 4).

    cout << endl << endl
         << "Enter the month and year for the logbook month : ";
    cin >> month >> year;
    Logbook testLog(month,year);
	
	// Start Debug
	
	cout << "PUTday 1: " << endl; testLog.putEntry(1,5); cout << endl;
	cout << "PUTday 4: " << endl; testLog.putEntry(4,5); cout << endl;
	cout << "PUTday 8: " << endl; testLog.putEntry(8,5); cout << endl;
	cout << "PUTday 16: " << endl; testLog.putEntry(16,5); cout << endl << endl;
	
	cout << "day 1: " << testLog.getEntry(1) << endl;
	cout << "day 4: " << testLog.getEntry(4) << endl;
	cout << "day 8: " << testLog.getEntry(8) << endl;
	cout << "day 16: " << testLog.getEntry(16) << endl << endl;
	cout << "day 9: " << testLog.getEntry(5) << endl;
	cout << "day 9: " << testLog.getEntry(9) << endl;
	cout << "day 21: " << testLog.getEntry(21) << endl;
	cout << "day 31: " << testLog.getEntry(31) << endl;
	cout << "day 5: " << testLog.getEntry(5) << endl;
	cout << "day 8: " << testLog.getEntry(8) << endl;
	cout << "day 6: " << testLog.getEntry(6) << endl;
	cout << "day 24: " << testLog.getEntry(24) << endl;
	cout << "day 25: " << testLog.getEntry(25) << endl;	
	//End debug
	
    // Test 1 : Tests the month, year, and daysInMonth operations.

    cout << "Month : " << testLog.month() << endl;
    cout << "Year  : " << testLog.year() << endl;
    cout << "# days in month : " << testLog.daysInMonth() << endl;

    // Test 2 : Tests the putEntry and getEntry operations.

    stop = 0;
    while ( !stop )
    {
        cout << endl << "Enter day and entry (0 0 to exit Test 2) : ";
        cin >> day >> entry;
        if ( day != 0  &&  entry != 0 )
        {
           testLog.putEntry(day,entry);
           cout << "Logbook:" << endl;
           for ( day = 1 ; day <= testLog.daysInMonth() ; day++ )
           {
               cout << day << " " << testLog.getEntry(day) << '\t';
               if ( day % 5 == 0 )
                  cout << endl;
           }
           cout << endl;
        }
        else stop = 1;
    }

     // Test 3 : Tests the calendar operation.

//3  cout << endl;
//3  testLog.displayCalendar();
//3  cout << endl;

//4  // Test 4 : Tests the overloaded constructor and putEntry
//4  //          operations.
//4
//4  Logbook thisMonth;
//4  cout << endl << "Logbook for this month:" << endl;
//4  cout << "Month : " << thisMonth.month() << endl;
//4  cout << "Year  : " << thisMonth.year() << endl;
//4  cout << "# days in month : " << thisMonth.daysInMonth() << endl;
//4  thisMonth.putEntry(100);
//4  for ( day = 1 ; day <= thisMonth.daysInMonth() ; day++ )
//4  {
//4      cout << day << " " << thisMonth.getEntry(day) << '\t';
//4      if ( day % 5 == 0 )
//4         cout << endl;
//4  }
//4  cout << endl;

//5  // Test 5 : Tests the [] operation.
//5
//5  cout << "Logbook:" << endl;
//5  for ( day = 1 ; day <= testLog.daysInMonth() ; day++ )
//5  {
//5      cout << day << " " << testLog[day] << '\t';
//5      if ( day % 5 == 0 )
//5         cout << endl;
//5  }
//5  cout << endl;

//6  // Test 6 : Tests the += operation.
//6
//6  Logbook logDay100(month,year),
//6          logDay200(month,year);
//6
//6  cout << endl
//6       << "Loading logbooks logDay100 and logDay200" << endl;
//6  for ( day = 1 ; day <= logDay100.daysInMonth() ; day++ )
//6  {
//6     logDay100.putEntry(day,100*day);
//6     logDay200.putEntry(day,200*day);
//6  }
//6
//6  logDay100 += logDay200;
//6
//6  cout << "Combined logbooks:" << endl;
//6  for ( day = 1 ; day <= logDay100.daysInMonth() ; day++ )
//6  {
//6      cout << day << " " << logDay100.getEntry(day) << '\t';
//6      if ( day % 5 == 0 )
//6         cout << endl;
//6  }
//6  cout << endl;
}

Standard "logbook.h"

//--------------------------------------------------------------------
//
//  Laboratory 1                                           logbook.h
//
//  Class declaration for the Logbook ADT
//
//--------------------------------------------------------------------

class Logbook
{
  public:

    // Constructor
    Logbook ( int month, int year );        // Create a logbook

    // Logbook marking operations
    void putEntry ( int day, int value );   // Store entry for day
    int getEntry ( int day ) const;         // Return entry for day

    // General operations
    int month () const;                     // Return the month
    int year () const;                      // Return the year
    int daysInMonth () const;               // Number of days in month

    // In-lab operations
    void displayCalendar () const;          // Display as calendar
    Logbook ();                             // Default constructor
    void putEntry ( int value );            // Store entry for today
    int operator [] ( int day ) const;      // Return entry for day
    void operator += ( const Logbook &rightLogbook );
                                            // Combine logbooks
  private:

    // Facilitator (helper) function
    int leapYear () const;                  // Leap year?

    // In-lab facilitator function
    int dayOfWeek ( int day ) const;        // Return day of the week

    // Data members
    int logMonth,     // Month covered by logbook
        logYear,
        entry [32];   // Logbook entries
};

My code, "logbook.cpp"

// 

#include "logbook.h"
#include <windows.h>

Logbook::Logbook ()
// Constructs an empty logbook for the specified month. 
{
	SYSTEMTIME st;

	logMonth = st.wMonth;
	logYear = st.wYear;
	
	for(int day=0;day<32;day++)
		entry[day]=0;
}

Logbook::Logbook (int month, int year)
// Constructs an empty logbook for the specified month. 
{

	logMonth = month;
	logYear = year;
	
	for(int day=0;day<32;day++)
		entry[day]=0;
}

void Logbook::putEntry ( int day, int value )
{
	if (0 < day && day <= daysInMonth ())
		entry[day] = value;
}

int Logbook::getEntry ( int day ) const
{

	if (0 < day && day <= daysInMonth ())
		return entry[day];
}

int Logbook::month () const
{
	return logMonth;
}

int Logbook::year () const
{
	return logYear;
}

int Logbook::operator [] ( int day ) const
{

	if (0 < day && day <= daysInMonth())
		return getEntry(day);
}

void Logbook::operator += ( const Logbook &rightLogbook )
{
	if (logMonth == rightLogbook.daysInMonth())
		for (int day = 1 ; day <= daysInMonth() ; day++)
		{
			int value  = getEntry(day) + rightLogbook.getEntry(day);
			putEntry(day, value);
		}	
}

int Logbook::daysInMonth () const
{
	switch (logMonth)
	{
		// If 
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			return 31;
		case 4:
		case 6:
		case 9:
		case 11:
			return 30;
		case 2:
		{
			if ((logYear % 4) == 0)
			{
				if ((logYear % 100) == 0)
				{
					if ((logYear % 400) == 0)
						return 29;
					else
						return 28;
				}
				else
					return 29;
			}
			else
				return 28;
		}
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.