locked
using continue statement RRS feed

  • Question

  • I wrote this code for homework.  It's pretty basic but I am having a hard time getting the continue statement to fit in.

    The code is suppose to ask for the amount of floors and iterate and ask how many rooms on each floor and of those rooms, how many are occupied. There is suppose to be an input validation on the floors of < 1 and the rooms of < 10. When it gets to the 13th iteration it is suppose to jump to 14.   Everything works BUT i can't figure how to write and implement the  continue statement.  Any help would be appreciated.  Here is my code:

     

    # include <iostream>
    # include <iomanip>
    
    using namespace std;
    
    int main ()
    
    {
    
    	int floors=0,
    		rooms=0,
    		totalR=0,
    		occupied=0,
    		totalOcc=0,
    		count=1,
    		count1=1;
    
    
    	double percent_occupied;
    
    
    
    	cout<<"Enter how many floors does your hotel have: ";
    	cin>>floors;
    
    	while(floors < 1 ) //input validation
    	{
    		
    
    		cout<<"Invalid entry, enter a number one or larger: ";
    		cin>>floors;
    	}
    
    
    
    
    
    
    
    
    	while (count <= floors)
    	{
    
    		
    
    
    		cout<<"Enter the number of rooms per floor "<<count<<": ";
    
    
    		cin>>rooms;
    
    
    		while(rooms<10)//input validation for rooms
    		{
    			cout<<"You entered and invalid number of rooms, enter more than 10 rooms: ";
    			cin>>rooms;
    
    		}
    
    		
    		totalR+=rooms;		
    		count++;
    
    
    		cout<<"How many rooms are occupied on floor "<<count1<<" ? ";
    		cin>>occupied;
    
    		totalOcc+=occupied;
    		count1++;
    	}
    
    
    	cout<<"Total rooms occupied: "<<totalOcc<<endl; //total occupied
    	
    	cout<<"Total rooms: : "<<totalR<<endl; //total rooms
    	
    	cout<<"total unoccupied rooms: "<<totalR-totalOcc<<endl;//unoccupied rooms
    	
    	cout<<setprecision(2)<<fixed;  //making sure the decimal set to two places
    	
    	cout<<"occupied rate is: "<<double(totalOcc)/double(totalR)*100<<" %\n"; 
    	//promoted to double and got percentage
    
    
    
    
    
    
    
    
    
    
    
    
    
    	return 0;
    
    }

    • Edited by eddie174 Monday, February 6, 2012 5:27 AM
    Monday, February 6, 2012 5:22 AM

Answers

  • It's upto you you can use while ,do while{} and for .And it's all depend how and where you are performing check for 13th floor . What you want to do on 13th floor . I am not sure . Inside your program if you just want to use continue use it with proper condition .

    like

    while()

    {

          while()

              {

                          if(xyz==13)

                            continue; //it will proceed with immediate while loop

              }

    }

    or you can use for watever you want.

    Thanks


    Rupesh Shukla
    • Marked as answer by eddie174 Wednesday, February 8, 2012 2:03 AM
    Monday, February 6, 2012 11:03 PM
  • Not sure where to incorporate it in my code. The exercise calls for the 13 floor to be bypassed since most buildings don't have one (superstition). I fixed the math now I am just banging my head as t where to place it so it will work.

    assume floor 2 have only 11 room and floor one has 15 room

    so you can say

    if((floor==2)&&(room>11))

     continue;

    Hope this make sense .Now you have to try it.

    Thanks


    Rupesh Shukla

    • Marked as answer by eddie174 Wednesday, February 8, 2012 2:03 AM
    Tuesday, February 7, 2012 3:12 AM
  • why not learn to use the switch statement

    http://www.cplusplus.com/forum/beginner/607/


    Windows MVP 2010-11, XP, Vista, 7. Expanding into Windows Server 2008 R2, SQL Server, SharePoint, Cloud, Virtualization etc. etc.

    Hardcore Games, Legendary is the only Way to Play

    Developer | Windows IT | Chess | Economics | Vegan Advocate | PC Reviews

    • Marked as answer by eddie174 Wednesday, February 8, 2012 2:03 AM
    Tuesday, February 7, 2012 3:15 AM
  • Not sure where to incorporate it in my code. The exercise calls for the 13 floor to be bypassed since most buildings don't have one (superstition). I fixed the math now I am just banging my head as t where to place it so it will work.
    I don't usually give too many details when helping
    students with their homework, as it can easily be
    construed as cheating and encourages students to
    take the easy way out by getting others to solve
    their assignments for them. But I'll stretch the
    limits a little in this case, if only to bring the
    thread to a happy end.

    To be able to use a continue statement effectively
    here, as required by your assignment, you need to
    construct your loop in such a way that it will begin
    the processing for each new floor at the *start* of
    the loop for processing floors. Then you check for
    floor 13 as early as possible and if true then start
    another iteration of the loop immediately. If it's
    not 13 then proceed through the loop's code to
    process that floor's data.

    Consider some of these possible changes:

    (1) Start the floor count variable at 0 (not at 1).

    (2) *In the conditional statement* itself (whether a
    while() or for()), preincrement the floor count
    variable. Then the very first thing in the loop
    code you should test for floor 13. If it's equal
    then just issue a continue command. That will
    cause the next iteration of the loop, which will
    cause the floor number to increment immediately.
    Note that in this design you should *not* be
    incrementing the floor number anywhere else in
    the loop code.

    - Wayne

    • Proposed as answer by Helen Zhao Tuesday, February 7, 2012 6:33 AM
    • Marked as answer by eddie174 Wednesday, February 8, 2012 2:03 AM
    Tuesday, February 7, 2012 4:44 AM
  • Hi eddie174,

    Thanks for your post in the MSDN Forum.

    According to your codes in the posts, I found there is an issue. That is, the occupied rooms may be more than all the rooms on the same floor. So I suggest you add codes like below to avoid this situation after " cin>>occupied; ":

    while(occupied>rooms)
    {
       cout<<" occupied rooms cannot be more than rooms on this floor";
       cin>>occupied;
    }	

    As far as I know, the continue statement forces transfer of control to the controlling expression of the smallest enclosing do, for, or while loop. I suppose you just want skip the 13th floor. I suggest you add codes as follows at the beginning of the outermost while-loop:

    if(count==13)
    {
       count++;
       continue;
    }

    I hope this reply can help you. If you have any other questions, please feel free to let us know.

    Best regards,


    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us

    • Proposed as answer by Helen Zhao Tuesday, February 7, 2012 7:03 AM
    • Marked as answer by eddie174 Wednesday, February 8, 2012 2:03 AM
    Tuesday, February 7, 2012 7:03 AM
  • Thanks

    I fixed the math. I realized it after I ran the program and the room totals were off. It was calculating by the total floors I entered. so I changed the math too:

    totalR+=rooms

    totalOcc+=ocupied 


    • Edited by eddie174 Thursday, February 9, 2012 6:41 PM
    • Marked as answer by eddie174 Thursday, February 9, 2012 7:01 PM
    Thursday, February 9, 2012 6:40 PM

All replies

  • Why do you need to use the continue keyword?

    If your program is working well, then you probably do not need to use it.

    For your information, continue is used to branch to the begining of the loop -

     

    while (<condition)
    {
        The statement of this line will execute.
        continue;      // Move to the beginning of the loop.
        The statement on this line will never be executed.
    }
    

     


    «_Superman_»
    Microsoft MVP (Visual C++)

    Polymorphism in C
    Monday, February 6, 2012 5:32 AM
  • The homework question has wants the 13th iteration bypassed since most hotels/building don't have 13th floors.

    I tried putting at the beginninf of the loop using the statement

    while(count++<floors) //not sure what to put here??

    {

       if(floors==13)

         continue;

    }

    it doesn't work. Since I used the count++ it skips floors.

     

     

     

    Monday, February 6, 2012 6:05 AM
  • Try this (instead of while):

     

    for( int count = 1; count <= floors; ++count)

    {

        if(count == 13) continue;

        . . . .

    }

     

    Remove the other count++.

    Monday, February 6, 2012 6:52 AM
  •      while(++count<=floors)

       {

                   if(count==13) continue;

      }

    Thanks

     


    Rupesh Shukla
    Monday, February 6, 2012 4:38 PM
  • I tried that and it will start with the highest floor instead of the first floor.

    Monday, February 6, 2012 5:37 PM
  • the for statement works good instead of my initial while statement. however for somereason it is not going from the 12th floor to the 13th floor??
    Monday, February 6, 2012 5:39 PM
  • I tried that and it will start with the highest floor instead of the first floor.


    See it doesn't matter whether you are using a while loop a for loop or do while . They all have their own benefits .But they work in same manner . If something is getting wrong then you have to check your code . I am sure something is missing there . Put break point and start debugging your code. In your code what is that Count1++ .why are you using that count.Can you show your actual code what are you doing and how .

     

    thanks


    Rupesh Shukla
    Monday, February 6, 2012 7:03 PM
  • I understand what you are saying. I took the count1 out since after looking at it I really didn't need to count I just needed to stuff the total to use later. Thus I used occupied and added for totalOcc.  I am new to this but I checked each section of my code and it seems to work.  I guess something needs to be either changed or moved??no idea.

    Here is my current code with some adjustments and annotations.

     

    # include <iostream>
    # include <iomanip>
    
    using namespace std;
    
    int main ()
    
    {
    
    	int floors=0,     //floors
    		rooms=0,       //rooms
    		totalR=0,      //total rooms
    		occupied=0,   //occupied per floor
    		totalOcc=0,  //total occupied
    		count=1;
    		
    
    
    	double percent_occupied;  //percent of rooms occupied
    
    
    
    	cout<<"Enter how many floors does your hotel have: ";
    	cin>>floors;
    
    	
    	while(floors < 1 ) //input validation
    	{
    		
    
    		cout<<"Invalid entry, enter a number one or larger: ";
    		cin>>floors;
    
    		
    	}
    
    
    
    
    	
    
    
    
    		while (count <= floors)
    		{
    		
    			
    
    
    			cout<<"Enter the number of rooms per floor "<<count<<": ";
    
    
    			cin>>rooms;
    		
    
    		while(rooms<10)//input validation for rooms
    		{
    			cout<<"You entered and invalid number of rooms, enter more than 10 rooms: ";
    			cin>>rooms;  //reentering rooms
    
    		}
    
    
    			totalR+=rooms;		//add rooms
    			count++;   //count rooms
    
    
    			cout<<"How many rooms are occupied on this floor  ? ";
    			cin>>occupied;                                     //input how many are occupied
    
    			totalOcc+=occupied;   //total all occupied
    
    		}
    	
    
    	cout<<"Total rooms occupied: "<<totalOcc<<endl; //total occupied
    
    	cout<<"Total rooms: : "<<totalR<<endl; //total rooms
    
    	cout<<"total unoccupied rooms: "<<totalR-totalOcc<<endl;//unoccupied rooms
    
    	cout<<setprecision(2)<<fixed;  //making sure the decimal set to two places
    
    	cout<<"occupied rate is: "<<double(totalOcc)/double(totalR)*100<<" %\n"; 
    	//promoted to double and got percentage
    
    
    
    
    
    
    
    
    
    
    
    
    
    	return 0;
    
    }
    

     

    Monday, February 6, 2012 7:52 PM
  • The only thing i can see room calculation is wrong .

    Enter No of Floor 2;

    Room Per Florr 11;

    So total no of room will be  = Room Per Florr * No of Floor;

    Thanks


    Rupesh Shukla
    Monday, February 6, 2012 8:19 PM
  • Thanks for pointing that out thats an easy fix.

    Where would I place the continue statement to get it to continue over the 13th floor without counting it?

    Would I use the while loop you proposed earlier?

     

    Thanks

    Eddie

     

    Monday, February 6, 2012 8:42 PM
  • It's upto you you can use while ,do while{} and for .And it's all depend how and where you are performing check for 13th floor . What you want to do on 13th floor . I am not sure . Inside your program if you just want to use continue use it with proper condition .

    like

    while()

    {

          while()

              {

                          if(xyz==13)

                            continue; //it will proceed with immediate while loop

              }

    }

    or you can use for watever you want.

    Thanks


    Rupesh Shukla
    • Marked as answer by eddie174 Wednesday, February 8, 2012 2:03 AM
    Monday, February 6, 2012 11:03 PM
  • Unfortunately I have to include it in my code. The code when it runs must iterate over the 13th floor since most buildings don't have a 13th floor. It's just an exercise but it is kicking my ***. Eddie
    Monday, February 6, 2012 11:58 PM
  • Not sure where to incorporate it in my code. The exercise calls for the 13 floor to be bypassed since most buildings don't have one (superstition). I fixed the math now I am just banging my head as t where to place it so it will work.
    Tuesday, February 7, 2012 12:01 AM
  • Not sure where to incorporate it in my code. The exercise calls for the 13 floor to be bypassed since most buildings don't have one (superstition). I fixed the math now I am just banging my head as t where to place it so it will work.

    assume floor 2 have only 11 room and floor one has 15 room

    so you can say

    if((floor==2)&&(room>11))

     continue;

    Hope this make sense .Now you have to try it.

    Thanks


    Rupesh Shukla

    • Marked as answer by eddie174 Wednesday, February 8, 2012 2:03 AM
    Tuesday, February 7, 2012 3:12 AM
  • Not sure where to incorporate it in my code. The exercise calls for the 13 floor to be bypassed since most buildings don't have one (superstition). I fixed the math now I am just banging my head as t where to place it so it will work.
    I don't usually give too many details when helping
    students with their homework, as it can easily be
    construed as cheating and encourages students to
    take the easy way out by getting others to solve
    their assignments for them. But I'll stretch the
    limits a little in this case, if only to bring the
    thread to a happy end.

    To be able to use a continue statement effectively
    here, as required by your assignment, you need to
    construct your loop in such a way that it will begin
    the processing for each new floor at the *start* of
    the loop for processing floors. Then you check for
    floor 13 as early as possible and if true then start
    another iteration of the loop immediately. If it's
    not 13 then proceed through the loop's code to
    process that floor's data.

    Consider some of these possible changes:

    (1) Start the floor count variable at 0 (not at 1).

    (2) *In the conditional statement* itself (whether a
    while() or for()), preincrement the floor count
    variable. Then the very first thing in the loop
    code you should test for floor 13. If it's equal
    then just issue a continue command. That will
    cause the next iteration of the loop, which will
    cause the floor number to increment immediately.
    Note that in this design you should *not* be
    incrementing the floor number anywhere else in
    the loop code.

    - Wayne

    • Proposed as answer by Helen Zhao Tuesday, February 7, 2012 6:33 AM
    • Marked as answer by eddie174 Wednesday, February 8, 2012 2:03 AM
    Tuesday, February 7, 2012 4:44 AM
  • Hi eddie174,

    Thanks for your post in the MSDN Forum.

    According to your codes in the posts, I found there is an issue. That is, the occupied rooms may be more than all the rooms on the same floor. So I suggest you add codes like below to avoid this situation after " cin>>occupied; ":

    while(occupied>rooms)
    {
       cout<<" occupied rooms cannot be more than rooms on this floor";
       cin>>occupied;
    }	

    As far as I know, the continue statement forces transfer of control to the controlling expression of the smallest enclosing do, for, or while loop. I suppose you just want skip the 13th floor. I suggest you add codes as follows at the beginning of the outermost while-loop:

    if(count==13)
    {
       count++;
       continue;
    }

    I hope this reply can help you. If you have any other questions, please feel free to let us know.

    Best regards,


    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us

    • Proposed as answer by Helen Zhao Tuesday, February 7, 2012 7:03 AM
    • Marked as answer by eddie174 Wednesday, February 8, 2012 2:03 AM
    Tuesday, February 7, 2012 7:03 AM
  • I want to thank everyone for your patience with me I finally got it.

    Here is the correct working code:

    # include <iostream>
    # include <iomanip>
    
    using namespace std;
    
    int main ()
    
    {
    
    	int floors=0,     //floors
    		rooms=0,       //rooms
    		totalR=0,      //total rooms
    		occupied=0,   //occupied per floor
    		totalOcc=0,  //total occupied
    		count=0;
    
    
    
    	double percent_occupied;  //percent of rooms occupied
    
    
    
    	cout<<"Enter how many floors does your hotel have: ";
    	cin>>floors;
    
    
    	while(floors < 1 ) //input validation
    	{
    
    
    		cout<<"Invalid entry, enter a number one or larger: ";
    		cin>>floors;
    
    
    
    
    	}
    
    
    
    
    
    
    
    
    	while (count < floors)
    	{
    		count++;
    
    		if (count==13)
    			continue;
    
    
    
    		cout<<"Enter the number of rooms per floor "<<count<<": ";
    
    
    		cin>>rooms;
    
    
    		while(rooms<10)//input validation for rooms
    		{
    			cout<<"You entered and invalid number of rooms, enter more than 10 rooms: ";
    			cin>>rooms;  //reentering rooms
    
    		}
    
    		
    
    		totalR=rooms*floors;		//add rooms
    
    
    
    
    		cout<<"How many rooms are occupied on this floor? ";
    		cin>>occupied;                                     //input how many are occupied
    
    	
    		totalOcc=occupied*floors;   //total all occupied
    
    		while(occupied>rooms)
    		{
    			cout<<"Occupied rooms cannot be more than rooms on this floor, enter new number: ";
    			cin>>occupied;
    			
    			
    
    		}
    	}
    
    
    	cout<<"Total rooms occupied: "<<totalOcc<<endl; //total occupied
    
    	cout<<"Total rooms: : "<<totalR<<endl; //total rooms
    
    	cout<<"total unoccupied rooms: "<<totalR-totalOcc<<endl;//unoccupied rooms
    
    	cout<<setprecision(2)<<fixed;  //making sure the decimal set to two places
    
    	cout<<"occupied rate is: "<<double(totalOcc)/double(totalR)*100.0<<" %\n"; 
    	//promoted to double and got percentage
    
    
    
    
    
    
    
    
    
    
    
    
    
    	return 0;
    
    }

    Wednesday, February 8, 2012 2:03 AM
  • The calculations for totalR and totalOcc are incorrect.  Furthermore, you should not compute totalOcc until after you have validated occupied.
    Wednesday, February 8, 2012 7:39 PM
  • Thanks

    I fixed the math. I realized it after I ran the program and the room totals were off. It was calculating by the total floors I entered. so I changed the math too:

    totalR+=rooms

    totalOcc+=ocupied 


    • Edited by eddie174 Thursday, February 9, 2012 6:41 PM
    • Marked as answer by eddie174 Thursday, February 9, 2012 7:01 PM
    Thursday, February 9, 2012 6:40 PM