7 Replies - 15882 Views - Last Post: 12 October 2006 - 09:07 AM Rate Topic: -----

#1 livefromyadkin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 22-September 06

skip interation in loop

Posted 12 October 2006 - 06:42 AM

Ok, yeah I'm back again with more newbie questions, sorry in advance, so I have to write this program for class. It calculates hotel occupancy.

First question, "it is traditional that most hotels do not have a thirteenth floor. The loop in this program should skip the entire thirteenth interation." No really sure how to go about this and it's frustrating me.

Second question, the program as of right now only calculates the last iteration of the loop. It doesn't calculate the other interations in the loop.

I'm not really sure how to go about this but here is my code so far. I appologize if this code is so far away from correct it hurts you professional guys. But thanks in advance for taking a look! I know you guys won't let me down!

#include <iostream>
using namespace std;

int main()
{
	int floors, rooms, occupied, unoccupied, num = 0, num1 = 0;
	
	cout<< "How many floors does your hotel have? \n";
	cin>>floors;
	if (floors < 1  &&  floors > 13)
	{	cout<< "Number of floors can't be less than 1. Reenter number of floors : \n";
		cin>>floors;
	}
	while (num++ < floors)
	{	cout<< "Floor "<<num<<"\nHow many rooms are on this floor? \n";
		cin>>rooms;
		if(rooms < 10)
		{	cout<< "Number of rooms has to be more than 10. Reenter number: \n";
			cin>>rooms;
		}

		cout<< "How many rooms are occupied? \n";
		cin>>occupied;
		if(occupied > rooms)
		{	cout<< "You have more rooms occupied than you have on the floor? Interesting! Reenter number: \n";
			cin>>occupied;
		}
	}
	
	
	unoccupied = rooms - occupied;


	
	cout<< "********************************************************* \n";
	cout<< "Floors.........................................."<<floors<<endl;
	cout<< "Occupied........................................"<<occupied<<endl;
	cout<< "Unoccupied......................................"<<unoccupied<<endl;
	cout<< "********************************************************* \n";
	
		

return 0;
}
 


Is This A Good Question/Topic? 0
  • +

Replies To: skip interation in loop

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: skip interation in loop

Posted 12 October 2006 - 06:59 AM

First, you check for floors may not be working as you imagined...it is unlikely that the number enhtered will be both less than one and greater than 13. Secondly, you can skip the 13th flor by placing a check in the loop to check the floor number...something like
if(num!=13)
//your code here


Was This Post Helpful? 0
  • +
  • -

#3 livefromyadkin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 22-September 06

Re: skip interation in loop

Posted 12 October 2006 - 07:08 AM

by the way...I accidentally left the... if (floors < 1 && floors > 13). I forgot to delete that. The working code was...

  if (floors < 1) 


Thought I would clear that up.

Amadeus, thanks I'm gonna go try that now. Is there a "skip" function? Like,
if ( floors > 13)
{skip 13} ?


Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: skip interation in loop

Posted 12 October 2006 - 07:14 AM

Use something like the if construct I provided above...it will check the floor, and if it does not equal 13, then it will proceed...if it does, it will skip.
Was This Post Helpful? 0
  • +
  • -

#5 livefromyadkin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 22-September 06

Re: skip interation in loop

Posted 12 October 2006 - 07:21 AM

oooh, ok I see
Was This Post Helpful? 0
  • +
  • -

#6 Trogdor  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 15
  • View blog
  • Posts: 627
  • Joined: 06-October 06

Re: skip interation in loop

Posted 12 October 2006 - 07:37 AM

what would also work is to put:
if(num==13) continue;


At the top of the loop-block.
Was This Post Helpful? 0
  • +
  • -

#7 livefromyadkin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 22-September 06

Re: skip interation in loop

Posted 12 October 2006 - 08:55 AM

Aiight I got that part working now! Thanks guys!!! Now how do I fix the second part?

Second question, the program as of right now only calculates the last iteration of the loop. It doesn't calculate the other interations in the loop.

This post has been edited by livefromyadkin: 12 October 2006 - 08:56 AM

Was This Post Helpful? 0
  • +
  • -

#8 livefromyadkin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 22-September 06

Re: skip interation in loop

Posted 12 October 2006 - 09:07 AM

Nevermind, I finally got it! Thanks again for your help guys!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1