# Thread: Skipping An Iteration in Loops

1. ## Skipping An Iteration in Loops

Hey guys -

I'm trying to write a program for class that calculates number of unoccupied and occupied rooms in a hotel, how many floors in the hotel etc..

The problem I'm having is getting the program to skip the 13th floor, since most hotels don't have one. I tried putting in an 'if' statement telling it to continue if the value of 'floors == 13', but that didn't work. Any ideas?

Here's the source code:
Code:
```// Hotel Occupancy Calculator
// Jen Tonon
// 11-08-05
// This program calculates the occupancy rate for rooms in a hotel.

#include <iostream>
using namespace std;

int main()
{
int floors, count=1, count2=1, occupied, rooms;  //Number of floors, rooms
//and increment counters.
double total, total2;

cout << "Please enter the number of floors in the hotel\n";
cin >> floors;

while (count <= floors)
{

float rooms;
cout << "Enter the number of rooms on floor " << count << ": ";
cin >> rooms;
total += rooms;
count++;
cout << "Enter the number of occupied rooms on floor " << count2 << ": ";
cin >> occupied;
total2 += occupied;
count2++;
}
cout << "The total number of rooms in this hotel are: " << total << endl;
cout << "The total number of occupied rooms in this hotel are: " << total2 << endl;
cout << "The total number of unoccupied rooms in this hotel are: " << total - total2 << endl;
cout << "The percentage of unoccupied rooms is: " << (total - total2) / total << " %" << endl;

system("PAUSE");
return 0;
}``` 2. continue skips the remainder of the loop and starts it again. 3. I tried putting in an 'if' statement telling it to continue if the value of 'floors == 13', but that didn't work.
Was it an infinite loop? Perhaps you didn't increment the loop variable. 4. The example my book gives us is:

Code:
```int testVal=0;
while (testVal++ <10)
{
if (testVal == 4)
continue;
cout << testVal << " ";
}```
Which I can tell from this that it won't apply to me since the user is inputting a value, and the example from the book has a predefined value for their variable. 5. >> I tried putting in an 'if' statement telling it to continue if the value of 'floors == 13', but that didn't work. Any ideas?

That sounds like the correct solution. Post the code with that in it and tell us how it didn't work. dwks is probably right, you have to increment the loop variable before you continue, since in the example the increment is done in the loop control, but in your code the increment is done in the part of the code that would be skipped by the continue. 6. The thing which i understand is that you want to skip the 13th floor. You can do it like that...
Code:
```// Hotel Occupancy Calculator
// Jen Tonon
// 11-08-05
// This program calculates the occupancy rate for rooms in a hotel.

#include <iostream>
using namespace std;

int main()
{
int floors, count=1, count2=1, occupied, rooms;  //Number of floors, rooms
//and increment counters.
double total, total2;

cout << "Please enter the number of floors in the hotel\n";
cin >> floors;

while (count <= floors)
{
if (count==13)
continue;
float rooms;
cout << "Enter the number of rooms on floor " << count << ": ";
cin >> rooms;
total += rooms;
count++;
cout << "Enter the number of occupied rooms on floor " << count2 << ": ";
cin >> occupied;
total2 += occupied;
count2++;
}
cout << "The total number of rooms in this hotel are: " << total << endl;
cout << "The total number of occupied rooms in this hotel are: " << total2 << endl;
cout << "The total number of unoccupied rooms in this hotel are: " << total - total2 << endl;
cout << "The percentage of unoccupied rooms is: " << (total - total2) / total << " %" << endl;

system("PAUSE");
return 0;
}```
I think this can solve your problem. Let me know if i understand wrong so that we can have other solutions. 7. mycplus, that won't solve the problem, count isn't being incremented when the if evaluates to true as dwks indicated earlier. 8. Since I don't know where you inserted the if statement, I can't be sure what your particular problem was, but if you insert it at the beginning and increment the counter, you shouldn't have any problems. Also, you have 2 counter variables that do the same thing. If you move the incremented counter to the end of the loop, you can remove count2, making your code just a little bit cleaner and easier to read.

Code:
```while (count <= floors)
{
if (count == 13)
{
count++;
continue;
}
float rooms;
cout << "Enter the number of rooms on floor " << count << ": ";
cin >> rooms;
total += rooms;
cout << "Enter the number of occupied rooms on floor " << count << ": ";
cin >> occupied;
total2 += occupied;
count++;
}``` 9. KCarson - thank you so much, it worked like a charm!

A big thanks to everyone else for your suggestions. Slowly but surely I'm getting better at this c++ stuff..  Popular pages Recent additions 