By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

containers iterators pointers. please help (urgent)

P: n/a
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::print( 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***
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
SORRY, SORRY!

I found my stupid mistake, so it was no mysterium, just a stupid wrong
thought.
So you don't need to read all the bad code.

(I wish I could erase the post).

cu

"progII" <no******@students.cc.tut.fi> schrieb im Newsbeitrag
news:d2**********@news.cc.tut.fi...
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::print( 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***

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.