EnSanity
October 22nd, 2006, 11:05 PM
Hey everyone, first time poster on the forum. I'm in need of some help. I'm writing an appointment book console application. I have two classes the appointment class which stores the member variables of the time, date, and description of the appointment, and I have the appointmentbook class which has all the functionality in it. I have created a dynamic array of the appointment class, and one of the functions of the program is to be able to sort the appointments by date in the array. I keep getting a memory fault when my sort is executed and im not sure why. My add function with the sort in it looks like this
void AppointmentBook::add(string date, string time, string description) //add function with arguments date,time,description of type string passed from the main.cc
{
bool check = true;
y = 0;
if (num == 0)
{
appt[num].setDate(date); //call the setDate function from the current instance of the appt object
appt[num].setTime(time);//call the setTime function from the current instance of the appt object
appt[num].setDescription(description);//call the setDescription function from the current instance of the appt object
cout << appt[y].getDate();
}
else{
while (check){
if (date > appt[y].getDate()){
y++;
}
else{
check = false;
}
}
for(int g = num-1; g <= y; g--){
appt[g+1].setDate(appt[g].getDate());
appt[g+1].setTime(appt[g].getTime());
appt[g+1].setDescription(appt[g].getDescription());
}
appt[y].setDate(date);
appt[y].setTime(time);
appt[y].setDescription(description);
}
num++;
}
num is the variable that keeps track of the number of appointments in the array, and so if what i think is correct, my code does this...
the add function is passed a new appointment, it checks to see if num == 0 which it is since its the first, so it sets appt[0]'s member variables and increments num. next appointment that is added it calls the add function and goes to the else{} block of code. It evaluates whether date is greater than appt[0], and if it is it adds one to y making y = 1 or keeps y = 0. When date is no longer > appt[y].getDate() it sets check to false and exits the loop. it is here however in the appt[y].getDate() that it incurs its memory fault. Is that because when there is only [0] set in the array and it is greater it looks to find appt[1] which doesnt exist yet? do i need to add a if (y >= num){check = false;}? Thank you so much for your time! - Jeff
void AppointmentBook::add(string date, string time, string description) //add function with arguments date,time,description of type string passed from the main.cc
{
bool check = true;
y = 0;
if (num == 0)
{
appt[num].setDate(date); //call the setDate function from the current instance of the appt object
appt[num].setTime(time);//call the setTime function from the current instance of the appt object
appt[num].setDescription(description);//call the setDescription function from the current instance of the appt object
cout << appt[y].getDate();
}
else{
while (check){
if (date > appt[y].getDate()){
y++;
}
else{
check = false;
}
}
for(int g = num-1; g <= y; g--){
appt[g+1].setDate(appt[g].getDate());
appt[g+1].setTime(appt[g].getTime());
appt[g+1].setDescription(appt[g].getDescription());
}
appt[y].setDate(date);
appt[y].setTime(time);
appt[y].setDescription(description);
}
num++;
}
num is the variable that keeps track of the number of appointments in the array, and so if what i think is correct, my code does this...
the add function is passed a new appointment, it checks to see if num == 0 which it is since its the first, so it sets appt[0]'s member variables and increments num. next appointment that is added it calls the add function and goes to the else{} block of code. It evaluates whether date is greater than appt[0], and if it is it adds one to y making y = 1 or keeps y = 0. When date is no longer > appt[y].getDate() it sets check to false and exits the loop. it is here however in the appt[y].getDate() that it incurs its memory fault. Is that because when there is only [0] set in the array and it is greater it looks to find appt[1] which doesnt exist yet? do i need to add a if (y >= num){check = false;}? Thank you so much for your time! - Jeff