Reading Input Files

I am currently working on a homework assignment where I have to create a program that will read the numbers off of a file representing a hotel's occupancy. Depending on those number I should give the user the number of floors in the hotel, the number of rooms occupied in each floor and the occupancy rate. I am stuck with getting the number of rooms occupied per floor. I believe my error is in that I must reset the number of rooms that have been accumulated. The output is only correct if the line starts with a 1 in the file line(which means the room is occupied). The 0 in the file means the room is empty and the -1 means that it is the end of the floor.

#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;

int main()
{
// These are the only variables you will need.
// Do not declare any more.

ifstream inFile; // the input file
int room; // 1 if the room is occupied, 0 if not, -1 if end of floor
int numOcc; // counter to keep track of the total number of occupied rooms in the hotel
int floorOcc; // counter for the number of occupied rooms on a floor
int numRooms; // counter for the total number of rooms in the hotel
int floorNum; // counter to keep track of the floor number
double occRate; // double to store the final occupancy rate


inFile.open("occupancy.txt"); // Opens the input file



if(!inFile)
{
cout << "Error opening the file." << endl;
cout << "Terminating program." << endl;
return -1;
}



numOcc = 0;
floorNum =1;
numRooms =0;


cout << setw(11) << "Floor" <<setw(3)<< "|";
cout << setw(11) << right << "Occupants" << endl;
cout << setw(25) << setfill('-') << '-' << endl;


while (inFile >> room)// Loop through the input file.
{


floorOcc = 0;
while (room != -1)
{
numOcc++;
inFile >> room;


if (room == 1)
{

numRooms++;
floorOcc++;
numOcc += room;

}
else if (room == 0)
{
numRooms++;
}


}
cout << floorNum << " ";


if(room == -1)
{

cout << floorOcc << endl;
floorNum++;

}


cout << setw(11) << right << numOcc << endl;


// The end of a floor is indicated by the sentinal value -1.



// End of looping through the input file
}

//occRate = (numOcc / numRooms) * 100;


cout<< fixed << setprecision(2);
cout << occRate << "%";/

inFile.close();// Close the input file


cout << endl;



return 0;
}



This is what the file contains:
1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 1 0 0 0 0 1 -1
1 0 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 -1
1 1 1 0 1 1 1 1 1 0 0 0 1 1 1 1 0 1 0 1 0 1 1 1 -1
1 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 1 0 0 0 0 -1
0 1 1 1 1 0 0 -1
0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 -1
1 0 0 0 0 0 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1 1 -1
1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 0 -1
0 0 0 0 0 0 1 1 1 1 1 1 0 1 0 1 0 1 0 0 0 -1
1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 -1
0 0 0 0 0 0 1 1 1 1 1 -1
1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 -1
1 1 1 1 1 1 1 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 1 1 1 0 0 0 -1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 -1

And this is my output:
Floor | Occupants
-------------------------
1 18
2 11
3 16
4 5
5 4
6 8
7 10
8 14
9 9
10 15
11 5
12 9
13 14
14 13

Last edited on
Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
numOcc = 0;
floorNum = 1;
numRooms = 0;

floorOcc = 0 ; // initialise outside the loop
               // reset only when we get to a new floor

while( inFile >> room ) // loop through the input file.
{
    if( room != -1 ) ++numRooms ; // a room in the hotel

    if( room == 1 ) ++floorOcc ; // an occupied room on this floor

    else if( room == -1 ) // this is the end of this floor
    {
        // print statistics for this floor
        cout << "floor# " << floorNum
             << "  occupied: " << numOcc << '\n' ;

        // update total number of occupied rooms in  the hotel
        numOcc += floorOcc ;

        ++floorNum ; // get to the next floor
        floorOcc = 0 ; // reset count for the new floor to zero
    }

    else if( room != 0 ) // must be 0 if not 1 or -1
    {
        cout << "error in input data: " << room << '\n' ;
        return 1 ; // error return from main
    }
}

if( room == -1 ) // the last entry read must have been -1
{
    if( numRooms > 0 )
    {
        occRate = double(numOcc) / numRooms ; // avoid integer division

        cout << "\nsummary:\n---------\n"
             << "total number of rooms in the hotel: " << numRooms << '\n'
             << "of which occupied rooms: " << numOcc << '\n'
             << "occupancy rate: " << occRate << '\n' ;
    }

    else std::cout << "unfortunately, this hotel does not have any rooms!\n" ;
}

else
{
    cout << "error in input data: last entry is not -1\n" ;
    return 1 ; // error return from main
}

Topic archived. No new replies allowed.