Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Problem with class within a class C++ program (http://www.go4expert.com/forums/class-class-cpp-program-t25607/)

darkstream 24Apr2011 03:13

Problem with class within a class C++ program
 
Code:

//Meetings_and_Rooms.h code//
 
#include <iostream>
#include <string>
using namespace std; // Inclusion of Header Files //
 
class Meeting // Definition of Meeting class //
{

private:

string name;
int startTime;
int finishTime; // Declaration of private data-members in Meeting Class //
public:

Meeting();
void setMeetingName(string);
string getMeetingName();
void setStartTime(int);
int getStartTime();
void setFinishTime(int);
int getFinishTime(); // Declaration of public member-functions in Meeting Class //
};
 
class Room // Definition of Room Class //
{
private:
int roomId;
Meeting meetingArray[50];
int arrayNumber; // Declaration of private data-members //
public:
Room();
void setRoomId(int);
int getRoomId();
Meeting accessMeetingArray();
void setArrayNumber(int);
int getArrayNumber();
void incrementArrayNumber();
void printRoomAssignment(); // Declaration of public member-functions //
};
 
// Meetings_and_Rooms.cpp code //
 
#include <iostream>
#include <string>
using namespace std; // Inclusion of Header Files //
#include "Meetings_and_Rooms.h"
 
 
Room::Room()
{
roomId = 0;
arrayNumber = 0;
}
 
void Room::setRoomId(int roomNum)
{roomId = roomNum;}
 
int Room::getRoomId()
{return roomId;}
 
Meeting Room::accessMeetingArray()
{return meetingArray[arrayNumber];}
 
void Room::setArrayNumber(int num)
{arrayNumber = num;}
 
int Room::getArrayNumber()
{return arrayNumber;}
 
void Room::incrementArrayNumber()
{arrayNumber++;}
 
void Room::printRoomAssignment()
{
 
int temp;
 
for(int current = 0; current <= arrayNumber; current++)
{
temp = arrayNumber;
arrayNumber = current;
 
cout<<accessMeetingArray().getStartTime()<<"--("<<accessMeetingArray().getMeetingName()<<")--"<<accessMeetingArray().getFinishTime()<<" ";
 
arrayNumber = temp;
}
} // definition of member-functions of Room class //
 
Meeting::Meeting()
{
name = " ";
startTime = 0;
finishTime = 0;
}
 
void Meeting::setMeetingName(string identity)
{name = identity;}
 
string Meeting::getMeetingName()
{return name;}
 
void Meeting::setStartTime(int begin_time)
{startTime = begin_time;}
 
int Meeting::getStartTime()
{return startTime;}
 
void Meeting::setFinishTime(int end_time)
{finishTime = end_time;}
 
int Meeting::getFinishTime()
{return finishTime;} // Definition of member-functions of Meeting Class //
 
 
// Actual program Code //
 
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#include "Meetings_and_Rooms.h"
#include <cstdlib>
#include <conio.h>
void Swap_Times(int time[], int position1, int position2); // prototype for function to swap integers in Arrays //
void Swap_Codes(string code[], int position1, int position2); // prototype for function to strings in Arrays
void BubbleSort(int startTime[], int SIZE, int finishTime[], string meetingCode[]); // prototype for a variation of a bubble sorting function //
 
int main() // Beginning of program //
{
string meeting_file;
string text_file = " "; // declaration of variable for file name, entered by user //
string meeting_code[50];
int start_time[50];
int finish_time[50]; // declaration of array to save information of meetings from file //
int counter = 0; // declaration of counter for number of meetings read from file //
int counter1 = 1; // declaration of counter for labelling Room number //
int counter2; // declaration of counter for "current meeting number" in 1st for loop //
int counter3; // declaration of counter for "current room number" in 2nd for loop //
int counter4; // declaration of counter for "current room number" in printing for loop //
int numOfExistingRooms; // declaration of variable for number of Room classes created //
int meetingAllocatedToRoom; // '0' means statement is false and '1' means statement is true, declaration of variable to store whether or not the meeting was entered into a room //
Room roomArray[50]; // array of data-type "class Room" and size 50 created //


 
// Section of Code that Reads, Stores and Organise Data from a User-Defined Text File //


 
cout<<"Please enter name of text file, remember to attach '.txt' at the end"<<endl;
cin>>text_file;
ifstream meetingPtr(text_file, ios::in);
if( !meetingPtr )
{
cout<<"\n Errored occurred in openning file"<<meeting_file;
exit(1);
}
else
{
while (meetingPtr>>meeting_code[counter]>>start_time[counter]>>finish_time[counter])
{
counter++;
}
}
meetingPtr.close();

BubbleSort(start_time, counter, finish_time, meeting_code); // organise data according to start time using a bubble formated sort //

 
// Section of Code that Assigns Meetings to a Minimun Number Rooms without Any Over-lapping of Meeting in the Same Room//
 
roomArray[0].setRoomId(counter1);
roomArray[0].accessMeetingArray().setMeetingName(meeting_code[0]);
roomArray[0].accessMeetingArray().setStartTime(start_time[0]);
roomArray[0].accessMeetingArray().setFinishTime(finish_time[0]); // Assigns meeting with the smallest start time to the first room //
counter1++; // increment counter for room id //
numOfExistingRooms = 1; // increment number of rooms existing //

for(counter2 = 1; counter2 < counter; counter2++) // nested for loops to enter the rest meetings //
{
meetingAllocatedToRoom = 0; // the statement "meeting is allocated to room" is set to false //
for(counter3 = 0; (counter3 < numOfExistingRooms) && (meetingAllocatedToRoom == 0); counter3++)
{
if (roomArray[counter3].accessMeetingArray().getFinishTime() <= start_time[counter2]) // checks existing rooms to see if meeting could be entered without an occurance of a clash //
{
roomArray[counter3].incrementArrayNumber();
roomArray[counter3].accessMeetingArray().setMeetingName(meeting_code[counter2]);
roomArray[counter3].accessMeetingArray().setStartTime(start_time[counter2]);
roomArray[counter3].accessMeetingArray().setFinishTime(finish_time[counter2]);
meetingAllocatedToRoom = 1; // if possible meeting is enter in already existing room and the statement "meeting is allocated to room" is set to true so as to exit for current loop //

}
}
if (meetingAllocatedToRoom == 0) // if meeting can not enter existing rooms without occurrance of clash then a new room is assigned for that meeting //
{
roomArray[numOfExistingRooms].setRoomId(counter1);
roomArray[numOfExistingRooms].accessMeetingArray().setMeetingName(meeting_code[counter2]);
roomArray[numOfExistingRooms].accessMeetingArray().setStartTime(start_time[counter2]);
roomArray[numOfExistingRooms].accessMeetingArray().setFinishTime(finish_time[counter2]);
numOfExistingRooms++; // number of existing rooms in incremented //
counter1++; // variable for room id is incremented //
}
}


 
 
// Section of Code that Prints Meeting Assignment to Screen //
for(counter4 = 0; counter4 < numOfExistingRooms; counter4++)
{
roomArray[counter4].printRoomAssignment(); // public printing member-function of Room used to show meetings assigned to the room //
cout<<endl; // moves to next line on screen //
}
getch();
return 0; // return 0 to main //
 
} // termination of program //
 
void Swap_Times(int time[], int position1, int position2) // definition of swap funtion for integers //
{
int temporary = time[position1];
time[position1] = time[position2];
time[position2] = temporary;
} // end of swap function for integers definition //
void Swap_Codes(string code[], int position1, int position2) // definition of swap function for strings //
{
string temporary = code[position1];
code[position1] = code[position2];
code[position2] = temporary;
} // end of swap function for strings definition //
 
void BubbleSort(int startTime[], int size, int finishTime[], string meetingCode[]) // definition of bubble sort function //
{
for(int i = 0; i < size-1; i++)
{
for(int j = 0; j < size-1; j++)
{
if (startTime[j] > startTime[j+1])
{
Swap_Times(startTime,j,j+1);
Swap_Times(finishTime,j,j+1);
Swap_Codes(meetingCode,j,j+1); // swaps meetings if meeting in array before has greater start time than that of the meeting after //
}
else if (startTime[j] == startTime[j+1])
{
if (finishTime[j] > finishTime[j+1])
{
Swap_Times(startTime,j,j+1);
Swap_Times(finishTime,j,j+1);
Swap_Codes(meetingCode,j,j+1);
} // swaps meetings if meeting in array before has same start time as the meeting after but a greater finish time //
}
} // end of 2nd for loop //
} // end of 1st for loop //
} // end of bubble sort definition //

I Have a problem with the section of code that deals with assigning the meetings to the rooms. Using a desk trace i can see that the values for the name, startTime and finishTime are changed in the class meeeting at the instant it is done but after returning to the main() function their values change back to " ", 0 and 0 respectively.


All times are GMT +5.5. The time now is 16:49.