C++ NO VECTORS PLEASE (Please answer in code, it\'s easier to understand what is
ID: 3840679 • Letter: C
Question
C++
NO VECTORS PLEASE (Please answer in code, it's easier to understand what is happening when in code. English not first language. Comments are really helpful)
Given this program:
1. Print out information from customer & rooms USING A SEPARATE CLASS? (Specifically functions: "printRoomInfoToFile(fstream &fout)" & "printRoomInfo()")
2. AND WITH THE SAME CLASS FROM #1, READ information FROM FILE TO CUSTOMER CLASS AND ROOM CLASS?
/* C++ Program that reads data about Customer Room Information */
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Room class definition
class Room
{
//Private instance variables
private:
string roomName;
double length;
double width;
double height;
public:
//Default Constructor
Room()
{
roomName = "";
length = 0;
width = 0;
height = 0;
}
//Parameter Constructor
Room(string rName, double len, double wid, double hei)
{
roomName = rName;
length = len;
width = wid;
height = hei;
}
//Function that returns area
double getArea()
{
//Area = 2h(l+w)
return ( (2 * height) * (length + width) );
}
//Function that returns room information
void printRoomInfo()
{
cout << " Room Name: " << roomName << " Length: " << length << " Width: " << width<< " Height: " << height << " ";
}
//Function that prints room information to file
void printRoomInfoToFile(fstream &fout)
{
fout << " Room Name: " << roomName << " Length: " << length << " Width: " << width<< " Height: " << height << " ";
}
};
//Class definition
class Customer
{
//Private instance variables
private:
string customerName;
//Four rooms
Room *rooms;
//Number of rooms
int noOfRooms;
public:
//Default Constructor
Customer()
{
customerName = "";
}
//Parameter Constructor
Customer(string cName, Room roomsArr[], int noRooms)
{
int i;
customerName = cName;
//Assigning number of rooms
noOfRooms = noRooms;
//Allocating space
rooms = new Room[noOfRooms];
//Iterating over rooms
for(i=0; i<noOfRooms; i++)
{
//Copying objects
rooms[i] = roomsArr[i];
}
}
//Function that prints details of Customer to Console
void print()
{
int i;
double totalArea = 0;
//Printing customer name
cout << " Customer Name: " << customerName << " ";
cout << " Total number of rooms: " << noOfRooms<< " ";
//Iterating over rooms
for(i=0; i<noOfRooms; i++)
{
//Printing room info
rooms[i].printRoomInfo();
//Calculating area
totalArea += rooms[i].getArea();
}
//Printing total area
cout << " Total area of all rooms: " << totalArea<< " ";
}
//Function that prints details of Customer to file
void printToFile(fstream& fout)
{
int i;
double totalArea = 0;
//Printing customer name
fout << " Customer Name: " << customerName << " ";
fout << " Total number of rooms: " << noOfRooms<< " ";
//Iterating over rooms
for(i=0; i<noOfRooms; i++)
{
//Printing room info
rooms[i].printRoomInfoToFile(fout);
//Calculating area
totalArea += rooms[i].getArea();
}
//Printing total area
fout << " Total area of all rooms: " << totalArea<< " ";
}
};
//Main function
int main()
{
string customerName;
string roomName;
double length;
double width;
double height;
int roomCnt;
int i, j;
//Opening file for writing
fstream fout("customers.txt", ios::out);
//Array of customers
Customer customers[5];
//Iterating over each index of array
for(i=0; i<5; i++)
{
//Reading customer data
cout << " Enter Customer Name: " ;
cin >> customerName;
//Reading number of rooms
cout << " Enter number of rooms: ";
cin >> roomCnt;
//Room Array
Room *rooms;
//Allocating space
rooms = new Room[roomCnt];
//Reading room information
for(j=0; j<roomCnt; j++)
{
cout << " Room #" << (j+1) << " <Room Name> <Length> <Width> <Height>: ";
cin >> roomName >> length >> width >> height;
//Creating room object
Room roomObj(roomName, length, width, height);
//Storing room object into array
rooms[j] = roomObj;
}
//Storing in array
Customer obj(customerName, rooms, roomCnt);
//Storing customers
customers[i] = obj;
}
//Iterating over each customer
for(i=0; i<5; i++)
{
//Printing customer data to file
fout << " Customer " << (i+1) << " Info: ";
customers[i].printToFile(fout);
fout << " ";
}
cout << " Results have been written to file customers.txt... ";
//Closing file
fout.close();
return 0;
}
Using as reference for study.
Thanks!
Explanation / Answer
The following C++ code is doing the specific task. The function printRoomInfo() prints the room information on the console. The other function printRoomInfoToFile() prints the desired data to the file mentioned in fout.
Code:
/* C++ Program that reads data about Customer Room Information */
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Room class definition
class Room
{
//Private instance variables
private:
string roomName;
double length;
double width;
double height;
public:
//Default Constructor
Room()
{
roomName = "";
length = 0;
width = 0;
height = 0;
}
//Parameter Constructor
Room(string rName, double len, double wid, double hei)
{
roomName = rName;
length = len;
width = wid;
height = hei;
}
//Function that returns area
double getArea()
{
//Area = 2h(l+w)
return ( (2 * height) * (length + width) );
}
//Function that returns room information
void printRoomInfo()
{
cout << " Room Name: " << roomName << " Length: " << length << " Width: " << width<< " Height: " << height << " ";
}
//Function that prints room information to file
void printRoomInfoToFile(fstream &fout)
{
fout << " Room Name: " << roomName << " Length: " << length << " Width: " << width<< " Height: " << height << " ";
}
};
//Class definition
class Customer
{
//Private instance variables
private:
string customerName;
//Four rooms
Room *rooms;
//Number of rooms
int noOfRooms;
public:
//Default Constructor
Customer()
{
customerName = "";
}
//Parameter Constructor
Customer(string cName, Room roomsArr[], int noRooms)
{
int i;
customerName = cName;
//Assigning number of rooms
noOfRooms = noRooms;
//Allocating space
rooms = new Room[noOfRooms];
//Iterating over rooms
for(i=0; i<noOfRooms; i++)
{
//Copying objects
rooms[i] = roomsArr[i];
}
}
//Function that prints details of Customer to Console
void print()
{
int i;
double totalArea = 0;
//Printing customer name
cout << " Customer Name: " << customerName << " ";
cout << " Total number of rooms: " << noOfRooms<< " ";
//Iterating over rooms
for(i=0; i<noOfRooms; i++)
{
//Printing room info
rooms[i].printRoomInfo();
//Calculating area
totalArea += rooms[i].getArea();
}
//Printing total area
cout << " Total area of all rooms: " << totalArea<< " ";
}
//Function that prints details of Customer to file
void printToFile(fstream& fout)
{
int i;
double totalArea = 0;
//Printing customer name
fout << " Customer Name: " << customerName << " ";
fout << " Total number of rooms: " << noOfRooms<< " ";
//Iterating over rooms
for(i=0; i<noOfRooms; i++)
{
//Printing room info
rooms[i].printRoomInfoToFile(fout);
//Calculating area
totalArea += rooms[i].getArea();
}
//Printing total area
fout << " Total area of all rooms: " << totalArea<< " ";
}
};
//Main function
int main()
{
string customerName;
string roomName;
double length;
double width;
double height;
int roomCnt;
int i, j;
//Opening file for writing
fstream fout("customers.txt", ios::out);
//Array of customers
Customer customers[5];
//Iterating over each index of array
for(i=0; i<5; i++)
{
//Reading customer data
cout << " Enter Customer Name: " ;
cin >> customerName;
//Reading number of rooms
cout << " Enter number of rooms: ";
cin >> roomCnt;
//Room Array
Room *rooms;
//Allocating space
rooms = new Room[roomCnt];
//Reading room information
for(j=0; j<roomCnt; j++)
{
cout << " Room #" << (j+1) << " <Room Name> <Length> <Width> <Height>: ";
cin >> roomName >> length >> width >> height;
//Creating room object
Room roomObj(roomName, length, width, height);
//Storing room object into array
rooms[j] = roomObj;
}
//Storing in array
Customer obj(customerName, rooms, roomCnt);
//Storing customers
customers[i] = obj;
}
//Iterating over each customer
for(i=0; i<5; i++)
{
//Printing customer data to file
fout << " Customer " << (i+1) << " Info: ";
customers[i].printToFile(fout);
fout << " ";
}
cout << " Results have been written to file customers.txt... ";
//Closing file
fout.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.