I am a beginner in c++ and am pretty horrible please help me ;(

Write your question here.
I am completely lost on how to create the loop for this program, its homework that is due tomorrow someone please point me in the right direction.
Oh and it also has an input file added on that we have to go based off of ill post it below the code.
My code:

// This program calculates the occupancy rate for a hotel. The input
// data file details which rooms are occupied. Each row of the input
// file represents one floor of the hotel. A room is represented by
// a single digit. The value of that digit is 1 if the room is occupied
// and is 0 if the room is not occupied. Because each floor can have
// a different number of rooms, the sentinal value -1 is used to
// indicate the end of data for a floor.
//
/////////////////////////////////////////////////////////////////////

#include <iostream>
#include <fstream> // for file i/o
#include <iomanip> // for output formatting
using namespace std;

int main()
{
// YOU MUST USE THESE VARIABLES ONLY
// YOU MAY NOT DECLARE ANY ADDITIONAL VARIABLES

ifstream inFile; // the input file
ofstream outFile; // the output file
int room; // 1 if the room is occupied, 0 if not, -1 if end of floor
int numOcc; // total number of occupied rooms
int floorOcc; // number of occupied rooms on a floor
int numRooms; // total number of rooms
int floorNum; // floor number
double occRate; // occupancy rate


//////////////////////////////////////////////////////////////////////
// Your code starts here

// Open the input file
inFile.open("occupants.txt");


// Check if there was an error opening the input file

if ( !inFile )
{
cout << "Error opening input file" << endl;
return -1; // Exit the program if there is an error
}

outFile.open("occupants.txt");

if ( !outFile )
{
cout << "Error opening the output file" << endl;
return -1; // exit the program indicating error
}

// Initialize the counter variables, and any other variables
// that need to be initialized.
// 1 if the room is occupied, 0 if not, -1 if end of floor
numOcc = 0; // total number of occupied rooms
// number of occupied rooms on a floor
numRooms = 0; // total number of rooms
floorNum = 1; // floor number
// occupancy rate

// Do any other work that needs to be done before the loop starts

while (!inFile.eof()) // Loop through the input file.
{
floorOcc = 0;
while (floorOcc != -1)
inFile >> floorOcc;

floorNum = 1;
while (floorNum != -1)
inFile >> floorNum;

numOcc = 0;
while (numOcc != -1)
inFile >> numOcc;

room = 1;
while (room != -1)
inFile >> room;
{
numRooms++;
if (room == 1)
inFile >> numRooms;
{
numOcc++;
floorOcc++;
}

inFile >> room;
}

// Your loop must be able to handle an input file of any length.
// In other words, you do not know how many floors the hotel has.


// For each row in the input file, count the number of rooms
// and the number of those rooms that are occupied.
// Print the floor number and number of occupied rooms on that
// floor in a nicely formatted table.
// You do not know how many rooms are on each floor.
// The end of a floor is indicated by the sentinal value -1.
// Be careful to not include the sentinal in your count!


// End of looping through the input file


// Calculate the occupancy rate.
// Watch out for integer division!
occRate = numOcc / numRooms * 100;



// Print out the result as a percentage with exactly one
// decimal point.
outFile << "The occupancy rate is: " << occRate << endl;


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

// End of your code
///////////////////////////////
return 0;
}

Extra file on hotel occupancy: (14 floors)
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
Here is an idea how those loops may work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Note: initialiize variables!
int floorNum = 0; // floor number
int numRooms = 0; // total number of rooms

...

std::string line;
while(std::getline(inFile, line))
{
  ++floorNum;
  std::stringstream ss{line}; // Note: use line for further processing
  int val;
  while((ss >> val) && (val != -1)) // This will read until the end of line (-1 is not really necessary in this scenario)
  {
    ++numRooms;
  }
}


See:
http://www.cplusplus.com/reference/string/string/?kw=string
http://www.cplusplus.com/reference/sstream/stringstream/?kw=stringstream
http://www.cplusplus.com/reference/string/string/getline/?kw=getline
Topic archived. No new replies allowed.