5 Replies - 485 Views - Last Post: 23 October 2019 - 04:33 PM Rate Topic: -----

#1 beginnerC++   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-August 19

single loop PLEASE HELP

Posted 22 October 2019 - 04:30 PM

#include <iostream>
using namespace std;
int main () //program begins with a main function.
{
 //Declare variable
 int const MAX_SUITES=120;

 int suitesonfloor,occupiedsuites = 0;
 double percentoccupied;

 //loop through each floor
 for ( int i = 10; i<=16; i++)
 {
     if (i == 13)//13th floor does not exist, so skip it
     {
         continue;
     }
  do //validate there are 0 -20 suites on floor
  {
      cout << " Enter the number of suites on floor " << i << " : ";
      cin >> suitesonfloor
  }while( suitesonfloor < 0 || suitesonfloor >20 );

    occupiedsuites = occupiedsuites + suitesonfloor; // Add suit per floor on total

 }
 //set percentage and cast to double
 percentoccupied (static_cast <double> (occupiedsuites))/ MAX_SUITES * 100;

 //Display results
 cout << "\nTotal suites in the hotel : " << MAX_SUITES <<std :: endl
 cout << " Total suites occupied in hotel : " << occupiedsuites << std ::endl
 cout << " Percentage of suites occupied : " << percentoccupied << " \% " <std:: endl;

 return 0;
}






Hi. i am trying to solve this for homework but i am quite confused. can someone help me out please? I AM STUCK


Write a program that calculates the occupancy rate of the 120 suites (20 per floor ) located on the top 6 floors of a 15-story luxury hotel .These are floors 10-12 and 14-16 because, like many hotels, there is no 13th floor. Solve the problem by using a single loop that loops once for each floor between 10 and 16 and , on each iteration, asks the user to input the number of suites occupied on that floor. After all the iterations, the program should display how many suites the hotel has, how many of them are occupied and, what percentage of them are occupied.

Input validation: Do not accept a value less than 0 or more than 20 for the number of suites occupied on a given floor

Is This A Good Question/Topic? 0
  • +

Replies To: single loop PLEASE HELP

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15357
  • View blog
  • Posts: 61,571
  • Joined: 12-June 08

Re: single loop PLEASE HELP

Posted 22 October 2019 - 04:48 PM

What is your question?
Was This Post Helpful? 0
  • +
  • -

#3 beginnerC++   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-August 19

Re: single loop PLEASE HELP

Posted 22 October 2019 - 04:54 PM

@modi123_1 the program is not running. i dont know where the mistake is . Plus i dont know where to put the condition of input validation. I posted what the program needs to do. I tried everything but cant find mistake
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15357
  • View blog
  • Posts: 61,571
  • Joined: 12-June 08

Re: single loop PLEASE HELP

Posted 22 October 2019 - 06:22 PM

The instructions indicate one loop, but you have a second. Punt that do-while.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7135
  • View blog
  • Posts: 24,239
  • Joined: 05-May 12

Re: single loop PLEASE HELP

Posted 22 October 2019 - 06:47 PM

The instructions merely say don't accept invalid values for computation. It doesn't say that you need to keep prompting until valid input is entered. This will then let you use just a single loop.
Was This Post Helpful? 0
  • +
  • -

#6 TightCoderEx   User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 01-December 12

Re: single loop PLEASE HELP

Posted 23 October 2019 - 04:33 PM

You were very close, close enough to warrant this example. There is a couple of problems with this code, so let's see if you can come up with a fix. Technically this is a single loop and that there is another loop to trap errors is coincidental in my mind. In any event, you had in your code so I included the nested loop.

#include <iostream>

using namespace std;

  const int MAX_SUITES = 120;
  
int main (void) {
	int totalSuites {0};
	int	occupied {0};
		
	// This prompting is for a VT100 compatible terminal on LINUX.
	cout << "\033[2J\033[4;16HEnter occupied suites for each floor]\n\n";

	for (int floor = 10; floor < 17; floor++) {
		if (floor == 13)
			continue;
			
		cout << "\n\t\tFloor #" << floor << "\t--> ";
		
		do {
			cin >> occupied;
			if (occupied > 0 && occupied <= 20) {
				totalSuites += occupied;
				break;
			}
			cout << "\t\t\t\t--> ";
		} while (1);
	}
	
	cout << "\n\n\tTotal   suites = " << MAX_SUITES << endl;
	cout << "\tTotal occupied = " << totalSuites << endl;
	cout << "\tVacancy   rate = " << 
			((double) totalSuites/(double) MAX_SUITES) * 100 << "%" << endl;
	
	return EXIT_SUCCESS;
}


This post has been edited by TightCoderEx: 23 October 2019 - 04:41 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1