Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a newbie learning C++ programming.
I have a project involving a House.cpp and a Room.cpp.

Room.ccp >> grabs info from room.h >> below is a snippet.
#include "Room.h"
#include <sstream>

Room::Room( RoomType type, int width, int breadth, int height )
{
	mType = type;
	mWidth = width;
	mBreadth = breadth;
	mHeight = height;
}

Room::Room()
{
	mType = BATHROOM;
	mWidth = 0;
	mBreadth = 0;
	mHeight = 0;
}

string Room::getDescription() const
{
	stringstream theStream;
	theStream
		<< getRoomName() << ": "
		<< "width=" << mWidth
		<< " breadth=" << mBreadth
		<< " height=" << mHeight
		<< "    " << getSquareFootage() << " Total Square Footage"
    << "    " << getVolume() << " Total Volume\n\n";
	return theStream.str();
}


House.cpp >> grabs info from house.h >> below is a snippet of my issue

#include <sstream>
#include "House.h"

using namespace std;

House::House()
{
	//stub
}

int House::getNumBedrooms()
{
  
	//stub
	return 0;
} 


I am trying to find the total number of rooms.


What was missing:
I put the entire Room.cpp program.

I didn't include the Room.h nor the House.h according to the project they didn't need to be changed.

Forgive me, this is the first time i post any question online and someone can probably advise if I should always include all the files? Thanks.


Here is the House Class:
class House
{
public:
	//constructor
	House();
	//destructor
	~House(){};

	bool addRoom( const Room& theRoom );
	int getNumBedrooms();
	int getNumBathrooms();
	int getNumKitchens();
	int getNumLivingrooms();

	int getTotalSquareFootage();//sum of the area of all rooms
	int getTotalVolume();//sum of the volume of all rooms
	//can this house be occupied - must have minimum number of rooms by type
	bool isValid();
	string enumerateRooms();//create a string with the descriptions of all the rooms in the house

	string getDescription();


What I have tried:

int House::getNumBedrooms()
{
  int numbed;
  Room r;
  numbed = r.getSquareFootage();
	//stub
	return numbed;
}


This returns a funky number not in the file.

HouseDriver.txt gets called from Main. main is correct and does not need to be touched as per project. I need to modify house.cpp

This is HouseDriver.txt  Col1 (Type of Room 0-kitchen, 1-bathroom, 2-Living Room, 3-bedroom), Col2 (Width), Col3 (Length), Col4 (Height)
0 10 10 7
1 5 6 7
3 15 8 7
2 20 10 10
0 12 7 8
1 10 10 6
1 11 12 7
1 4 5 8
2 10 13 9
3 11 13 8
3 12 15 8
1 12 23 12
Posted
Updated 6-Feb-23 11:43am
v4

If I was doing this, I would read each room's description into an instance of a Room object. Then I would have a collection of those Room objects as a member of a House object. For this I would probably use a std::vector for the collection so to acquire the number of rooms in the House I would call the size method of the Room vector.

I don't see how calling the getSquareFootage method of a Room object should be expected to return the number of rooms.

ETA: we can't see how you have saved the House data in your program so it virtually impossible to give you an exact answer to your question.
 
Share this answer
 
v2
Comments
NY1022 6-Feb-23 17:41pm    
Rick York, I appreciate your solution and i have updated my question. I have just started learning about Class and not familiar at all with Vector - probably more advance. What have people done before vectors? is there a traditional approach?
Rick York 6-Feb-23 21:09pm    
Yes, arrays are the traditional approach. You can either allocate a whole bunch of them to start or implement a re-allocation scheme. Even then, there are more options. You can make your own pseudo-vector things like a free pool to save on re-allocations or re-allocate them one at a time. The function for re-allocation is realloc.

I assumed you could use vectors since you are using strings. Here is some documentation on vector : https://cplusplus.com/reference/vector/vector/. The key function of interest is push_back. When I use a vector I almost always call reserve to pre-allocate about as much memory as I think I will use so I don't thrash the heap too badly with re-allocations.
merano99 7-Feb-23 18:12pm    
The alternative to a vector would be to wrap even a concatenated list in a class. But that would be unnecessary in C++ and not recommended. Rick's suggestion to use an array and reallocate memory each time would mean that everything would have to be copied multiple times. If you don't use a C++ container you have to take care of memory yourself and release it at the end.
HouseDriver.txt is called from Main.
The sentence makes little sense. Could it be that there is a file called HouseDriver.txt that is read into main?
What do the numbers mean and how are they distributed in the classes house and room? Conceptually, a class should contain all the associated data and methods to access it.

You describe here how you try to implement the method getNumBedrooms().
If you assume that a house should contain rooms it makes little sense to create single rooms in one method. It can't work like this:
C++
int House::getNumBedrooms()
{
  int numbed;
  Room r;
  numbed = r.getSquareFootage();
	//stub
	return numbed;
}

So far, you've only told us that your House class looks like this:
C++
class House
{
public:
	int getNumBedrooms() {};
};

One would expect a house to contain rooms. Where are they here?

Here, a std container such as a vector with rooms, which is located in the class House, would be advisable. Of course you need methods to fill the house with rooms. All in all the following approach would be possible:
C++
class House
{
public:
	int getNumBedrooms();
	bool addRoom(const Room& room);
private:
	vector<class Room> roomlst;
};


// edit:
The method getNumBedrooms() should probably get the number of rooms of this type. You have now indicated that the data type RoomType is also present.
It actually makes little sense to write a separate method for each room type, since the process is always the same. The list with rooms must be searched and rooms of the desired type are counted. It would make sense to first write a method that counts all rooms of a type. If desired, these could then be called in separate methods without parameters.
C++
int House::getNumBathrooms() {
	return getNumRoomType(BATHROOM);
}
// int House::getNumBedrooms()
// int House::getNumKitchens();
// int House::getNumLivingrooms();
int House::getNumRoomType(RoomType type)
{
	int cnt = 0;
	for (auto&& n : roomlst) {
		// TODO: count
	}
	return cnt;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900