Skipping An Iteration in Loops

This is a discussion on Skipping An Iteration in Loops within the C++ Programming forums, part of the General Programming Boards category; Hey guys - I'm trying to write a program for class that calculates number of unoccupied and occupied rooms in ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    8

    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. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    continue skips the remainder of the loop and starts it again.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    8
    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. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    3
    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. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    mycplus, that won't solve the problem, count isn't being incremented when the if evaluates to true as dwks indicated earlier.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    1
    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. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    8
    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 subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. MPI - linear pipeline solution for jacobi iteration
    By eclipt in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2006, 05:25 AM
  3. The "continue" statement in JAVA
    By Hexxx in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-26-2004, 05:19 PM
  4. 2 for loops...dunno why its skipping loop 2
    By Axolotl in forum C Programming
    Replies: 3
    Last Post: 04-03-2003, 01:07 PM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21