Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Problem 7. (20 points) Create Rooms.txt with contents shown as follows by no

ID: 3714708 • Letter: C

Question

C++

Problem 7. (20 points) Create Rooms.txt with contents shown as follows by notep ad and save in the aining in feet and inches same program folder. Write a program that length and width for each room in a house. Length and width are expressed using the format indicated in the example below. Write a C++ progr in square feet) of the house by adding up the areas for each room. will read a file named "Rooms.txt" cont the am to calculate the total area Display the total area on the screen. As sume that there are an unknown number of rooms Example file (Rooms.txt") Width Length 141-6.5" 13-8.0" 16-1.5" Room 2 3 4 12-O.5" 10-6.0" 11-11.5" 145.5" 12-O" 5

Explanation / Answer

#include<iostream>
#include <fstream> // For ifstream
#include <stdlib.h> // For function exit()
#define MAX 100
using namespace std;

// Structure Room definition to store room information
struct Room
{
// To store room number
int roomNo;
// To store feet of length
float feetL;
// To store inches of length
float inchL;
// To store feet of width
float feetW;
// To store inches of length
float inchW;
};// End of structure

/*
* Function definition to read file contents and stores it in room array of objects
* Room room[] -> array of structure objects to store each room length and width (foot and inches)
* string fileName -> file name to read the contents of the file
*/
int readFile(Room room[], string fileName)
{
// File stream object declared
ifstream fileRead;
// To store heading
string heading;
// To store symbol foot and inches
string symbol;

// Record counter
int counter = 0;

// Open the file for reading
fileRead.open(fileName.c_str());

// Check that file can be opened or not
// is_open() function returns true if a file is open and associated with this stream object.
// Otherwise returns false.
if(!fileRead.is_open())
{
// Displays error message
cout<<" Error: Unable to open the file! "<<fileName<<" Please re - enter the file name.";
}// End of if condition

// Reads the heading
getline(fileRead, heading);

// Loops till not end of the file
// eof() function returns true if the stream's eofbit error state flag is set (which signals that the End-of-File has been reached
// by the last input operation). Otherwise returns false.
while(!fileRead.eof())
{
// Reads the room number and stores in data member roomNo at the counter index position of the room object
fileRead>>room[counter].roomNo;
// Reads the room feet for length and stores in data member feetL at the counter index position of the room object
fileRead>>room[counter].feetL;
// Reads the feet symbol
fileRead>>symbol;

// Reads the room inches for length and stores in data member inchL at the counter index position of the room object
fileRead>>room[counter].inchL;
// Reads the inches symbol
fileRead>>symbol;

// Reads the room feet for width and stores in data member feetW at the counter index position of the room object
fileRead>>room[counter].feetW;
// Reads the feet symbol
fileRead>>symbol;

// Reads the room inches for width and stores in data member inchW at the counter index position of the room object
fileRead>>room[counter].inchW;
// Reads the inches symbol
fileRead>>symbol;
// Increase the record counter by one
counter++;
}// End of while condition*/

// Close the file
fileRead.close();
// Returns number of records
return counter;
}// End of function

/*
* Function definition to calculate area of each room and total area
* Room room[] -> array of structure objects to store each room length and width (foot and inches)
* int len -> Number of records
* float roomArea[] -> To store each room calculated area
* float &totalArea -> To store total area. Passed by reference to get the result in main without returning result
*/
void calculare(Room room[], int len, float roomArea[], float &totalArea)
{
// To store calculated feet for length and width
float feetL, feetW;
// To store calculated total area
totalArea = 0;

// loops till number of records
for(int x = 0; x < len; x++)
{
// Converts the each room number length in inches to feet and adds it to feet
feetL = room[x].feetL + (room[x].inchL / 12.0f);
// Converts the each room number width in inches to feet and adds it to feet
feetW = room[x].feetW + (room[x].inchW / 12.0f);
// Calculate the area of each room
roomArea[x] = feetL * feetW;
// Calculates the total area
totalArea += roomArea[x];
}// End of for loop
}// End of function

/*
* Function definition to display each room information with area and total area
* Room room[] -> array of structure objects to store each room length and width (foot and inches)
* int len -> Number of records
* float roomArea[] -> Contains each room calculated area
* float &totalArea -> Contains total area.
*/
void displayRoom(Room room[], int len, float roomArea[], float totalArea)
{
// loops till number of records
for(int x = 0; x < len; x++)
{
// Displays room number
cout<<" Room Number: "<<room[x].roomNo;
// Displays each room length and width
cout<<" Feet Length: "<<room[x].feetL<<" Inches Length: "<<room[x].inchL;
cout<<" Feet Width: "<<room[x].feetW<<" Inches Width: "<<room[x].inchW;
// Displays each room calculated area
cout<<" Room Area: "<<roomArea[x];
}// End of for loop
// Displays total area
cout<<" Total Area = "<<totalArea;
}// End of function

// main function definition
int main()
{
// Creates an array of objects of Room
Room room[MAX];
// Calls the function to read the file contents and store the data in array of object room
// Stores the return value as number of records
int counter = readFile(room, "Rooms.txt");
// Creates an array of type float of size number of records to store each room area
float roomArea[counter];
// To store total area
float totalArea;
// Calls the function to calculate each room area and total area
calculare(room, counter, roomArea, totalArea);
// Calls the function to display each room information with total area
displayRoom(room, counter, roomArea, totalArea);
}// End of main function

Sample Output:


Room Number: 1
Feet Length: 14 Inches Length: -6.5 Feet Width: 9 Inches Width: -2.1
Room Area: 118.77
Room Number: 2
Feet Length: 13 Inches Length: -8 Feet Width: 12 Inches Width: -0.5
Room Area: 147.486
Room Number: 3
Feet Length: 16 Inches Length: -1.5 Feet Width: 11 Inches Width: -4.4
Room Area: 168.804
Room Number: 4
Feet Length: 12 Inches Length: -0 Feet Width: 10 Inches Width: -6
Room Area: 114
Room Number: 5
Feet Length: 11 Inches Length: -11.5 Feet Width: 14 Inches Width: -5.5
Room Area: 135.981

Total Area = 685.041

Rooms.txt file contents

Room Length Width

1 14' -6.5'' 9' -2.1''

2 13' -8.0'' 12' -0.5''

3 16' -1.5'' 11' -4.4''

4 12' -0'' 10' -6.0''

5 11' -11.5'' 14' -5.5''

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote