P
progII
Hello,
i am struggling the whole night cos of my c++ homework. deadline is very
soon. (april 4th)
i have to implement a calender.
i store every calender entry in a list. a calender entry is a struct with
entry, date, time, type.
the problem is: if i use the change-function to add a (optional) time to an
existing entry, it seems to work.(the function gets the right Record out of
the list, the output (for testing) is fine, it seems really like the
function would set the hour and minute to the Record)
but when i use the print-function, the entry hasn't changed and there is no
time added.
i have no idea, why. no idea, how to find out where the mistake is.
thank you very much in advance for trying to help.
here the 3 parts of the code:
here is the "class-part" of the header-file:
***beginn of code***
class Calendar
{
// definition of the interface
public:
// constructor
Calendar();
// descructor
~Calendar();
bool print(int day, int month, int year,
int type
);
bool add(string entry, int type,
int day, int month, int year,
int hour, int minute,
bool noTime, bool noType
);
bool change(string entry,
int oldDay, int oldMonth, int oldYear,
int newDay, int newMonth, int newYear,
int newHour, int newMinute,
bool changeTime
);
bool del(string entry,
int day, int month, int year
);
bool readFile(string fileName);
bool writeFile(string fileName);
// hidden implementation details
private:
struct Record{
int day, month, year;
int hour, minute;
int type;
string entry;
bool noTime;
bool noType;
};
//set<Record> table;
list<Record> table;
bool equal(Record a, Record b);
// Calendar ( const Calendar& q);
// Calendar& operator = ( const Calendar& q );
};
***end of code***
and here is the "change-function"
***begin of code***
bool Calendar::change(string entry,
int oldDay, int oldMonth, int oldYear,
int newDay, int newMonth, int newYear,
int newHour, int newMinute, bool changeTime)
{
Record tmp;
tmp.entry = entry;
tmp.day = oldDay;
tmp.month = oldMonth;
tmp.year = oldYear;
//runs to the Record that should be changed
list<Record>::iterator i = table.begin();
while( i != table.end() && !equal( *i, tmp) ){
++i;
}
if( i == table.end() )
{
cerr << "Error: unknown entry" << endl;
return false;
}
else{
cout << "found:" << endl;
cout << i->entry << endl;
cout << i->day << i->month << i->year << endl;
if( changeTime )
{
i->hour = newHour;
cout << "i->hour is " << i->hour << endl;
i->minute = newMinute;
cout << "i->minute is " << i->minute << endl;
}
else
{
i->day = newDay;
i->month = newMonth;
i->year = newYear;
}
return true;
}
}
***end of code***
and last but not least the "print - function"
*beginn of code***
bool Calendar:rint( int day, int month, int year,
int type)
{
for( list<Record>::iterator i = table.begin();
i != table.end(); ++i)
{
if( i->day == day && i->month == month && i->year == year
&& (i->type == type || type == ALL) )
{
cout << i->entry;
if( !i->noTime )
{
cout << " " << i->hour << " " << i->minute ;
}
cout <<endl;
}
}
return true;
}
***end of code***
i am struggling the whole night cos of my c++ homework. deadline is very
soon. (april 4th)
i have to implement a calender.
i store every calender entry in a list. a calender entry is a struct with
entry, date, time, type.
the problem is: if i use the change-function to add a (optional) time to an
existing entry, it seems to work.(the function gets the right Record out of
the list, the output (for testing) is fine, it seems really like the
function would set the hour and minute to the Record)
but when i use the print-function, the entry hasn't changed and there is no
time added.
i have no idea, why. no idea, how to find out where the mistake is.
thank you very much in advance for trying to help.
here the 3 parts of the code:
here is the "class-part" of the header-file:
***beginn of code***
class Calendar
{
// definition of the interface
public:
// constructor
Calendar();
// descructor
~Calendar();
bool print(int day, int month, int year,
int type
);
bool add(string entry, int type,
int day, int month, int year,
int hour, int minute,
bool noTime, bool noType
);
bool change(string entry,
int oldDay, int oldMonth, int oldYear,
int newDay, int newMonth, int newYear,
int newHour, int newMinute,
bool changeTime
);
bool del(string entry,
int day, int month, int year
);
bool readFile(string fileName);
bool writeFile(string fileName);
// hidden implementation details
private:
struct Record{
int day, month, year;
int hour, minute;
int type;
string entry;
bool noTime;
bool noType;
};
//set<Record> table;
list<Record> table;
bool equal(Record a, Record b);
// Calendar ( const Calendar& q);
// Calendar& operator = ( const Calendar& q );
};
***end of code***
and here is the "change-function"
***begin of code***
bool Calendar::change(string entry,
int oldDay, int oldMonth, int oldYear,
int newDay, int newMonth, int newYear,
int newHour, int newMinute, bool changeTime)
{
Record tmp;
tmp.entry = entry;
tmp.day = oldDay;
tmp.month = oldMonth;
tmp.year = oldYear;
//runs to the Record that should be changed
list<Record>::iterator i = table.begin();
while( i != table.end() && !equal( *i, tmp) ){
++i;
}
if( i == table.end() )
{
cerr << "Error: unknown entry" << endl;
return false;
}
else{
cout << "found:" << endl;
cout << i->entry << endl;
cout << i->day << i->month << i->year << endl;
if( changeTime )
{
i->hour = newHour;
cout << "i->hour is " << i->hour << endl;
i->minute = newMinute;
cout << "i->minute is " << i->minute << endl;
}
else
{
i->day = newDay;
i->month = newMonth;
i->year = newYear;
}
return true;
}
}
***end of code***
and last but not least the "print - function"
*beginn of code***
bool Calendar:rint( int day, int month, int year,
int type)
{
for( list<Record>::iterator i = table.begin();
i != table.end(); ++i)
{
if( i->day == day && i->month == month && i->year == year
&& (i->type == type || type == ALL) )
{
cout << i->entry;
if( !i->noTime )
{
cout << " " << i->hour << " " << i->minute ;
}
cout <<endl;
}
}
return true;
}
***end of code***