C++ NO VECTORS PLEASE (Please answer in code, it\'s easier to understand what is
ID: 3840840 • 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. USING A SEPARATE CLASS Print out information from customer & rooms (Specifically from functions: "printRoomInfoToFile(fstream &fout)" & "printRoomInfo()")
2. AND WITH THE SAME CLASS FROM #1, READ information FROM "Customer.txt" FILE TO CUSTOMER CLASS AND ROOM CLASS (READS INFORMATION FROM FILE AND USES INFORMATION IN PROGRAM)
(I WANT TO SEE WHAT THIS PROGRAM WOULD BE LIKE IF THERE WAS A SEPARATE CLASS THAT HANDLES ALL THE FILE INPUT/OUTPUT)
/* 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
Following is the required 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<< " ";
}
};
class displayInformation
{
public:
void WriteInformation( Customer* cust, int total_cust ){
//Opening file for writing
fstream fout("customers.txt", ios::out);
//Iterating over each customer
for( int i=0; i< total_cust; i++ ){
//Printing customer data to file
fout << " Customer " << (i+1) << " Info: ";
cust[i].printToFile(fout);
fout << " ";
}
cout << " Results have been written to file customers.txt... ";
//Closing file
fout.close();
}
int ReadInformation( Customer* cust ){
//Opening file for reading
ifstream in("customers.txt");
string timepass;
int size_of_array = 0;
while( getline(in,timepass) ){
getline(in,timepass);
getline(in,timepass);
getline(in,timepass);
size_t size = timepass.find("Name: ");
string name = timepass.substr( size + 6, (timepass.size()-size-7) );
getline(in,timepass);
getline(in,timepass);
size = timepass.find("ooms: ");
int total_rooms = stoi( timepass.substr( size + 6, (timepass.size()-size-7) ) );
Room* rooms = new Room[total_rooms];
for( int i = 0; i < total_rooms; i++ ){
getline(in,timepass);
getline(in,timepass);
size = timepass.find("Name: ");
string room_name = timepass.substr( size + 6, (timepass.size()-size-7) );
getline(in,timepass);
size = timepass.find("ngth: ");
int length = stoi(timepass.substr( size + 6, (timepass.size()-size-7) ) );
getline(in,timepass);
size = timepass.find("idth: ");
int width = stoi(timepass.substr( size + 6, (timepass.size()-size-7) ) );
getline(in,timepass);
size = timepass.find("ight: ");
int height = stoi(timepass.substr( size + 6, (timepass.size()-size-7) ) );
getline(in,timepass);
rooms[i] = Room(room_name, length, width, height);
}
getline(in,timepass);
getline(in,timepass);
getline(in,timepass);
getline(in,timepass);
getline(in,timepass);
cust[ size_of_array ] = Customer( name, rooms, total_rooms );
size_of_array++;
}
return size_of_array;
}
};
//Main function
int main()
{
string customerName;
string roomName;
double length;
double width;
double height;
int roomCnt;
int i, j;
//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;
}
displayInformation D;
D.WriteInformation( customers, 5 );
Customer newCustomer[5];
int total_customers = D.ReadInformation( newCustomer );
D.WriteInformation( newCustomer, total_customers );
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.