A little help with a equation please

Hi all,

I have a program for school that I've started and it is very close to working but for some reason the calculation for the occupancy percentage isn't working (Line 34). Any help is appreciated.

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
using namespace std;

int main()
{

	int floors=0, rooms=0, totalrooms=0, totaloccupied=0, occupied=0, count=0;
	int pctoccupied;
	
	
	cout << "How many floors does the hotel have?   ";
	cin >> floors;
	
	while (count < floors)
		{
		count++;
		cout << "\nHow many rooms on floor " << count << "?   "  ;
		cin >> rooms;
		cout << "\nHow many of those rooms are currently occupied?  ";
		cin >> occupied;
		
			if (occupied > rooms)
				{
					cout << "The rooms occupied cannot exceed the number of rooms.\n";
					cout << "How many rooms on floor " << count << " are occupied?\n";
					cin >> occupied;
				}	 
		totaloccupied += occupied;
		totalrooms += rooms;
		
			
		}

	pctoccupied = (totaloccupied / totalrooms) * 100;
		
	cout << "The total amount of rooms is " << totalrooms << ".\n";
	cout << totaloccupied << " rooms are occupied.\n";
	cout << totalrooms - totaloccupied << " rooms are unoccupied.\n";
	cout << pctoccupied << " percent of the rooms are occupied.\n" << endl;
	return 0;
}
Last edited on
Its because it gives you decimal %. And if you use an integer it will round it to an integer. Change the variables to this

1
2
3
int floors=0, rooms=0, occupied=0, count=0;
double totaloccupied = 0, totalrooms = 0;
double pctoccupied;
I'll give it a try first thing tomorrow.

Thank you!
Thanks again for the help! The math works now but I have another issue now that I've read the problem. It seems that the floors should be already defined and only the floors 10-12 & 14-16 should be calculated (because of the hotel NOT having a 13th floor) and I've been able to adjust the code to do that but I have not been able to figure out how to skip 13 and go to 14. This is my attempt but it isn't working as it starts on the 14th floor instead of the 10th.

Any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
using namespace std;

int main()
{

	int floors=16, suites=20, totalrooms=0, count=9;
	double totaloccupied = 0, occupied = 0;
	double pctoccupied;
	
	
	while (count < floors)
		{
		
		count++;
			
		if (count = 13)
			count++; 
		

		
		//cout << "\nHow many rooms on floor " << count << "?   "  ;
		//cin >> rooms;
		cout << "\nHow many of those suites are currently occupied on floor " << count << "?  ";
		cin >> occupied;
		
			if (occupied > suites)
				{
					cout << "The rooms occupied cannot exceed the number of rooms.\n";
					cout << "How many rooms on floor " << count << " are occupied?\n";
					cin >> occupied;
				}	 
		totaloccupied += occupied;
		totalrooms += suites;
		
			
		}

	pctoccupied = (totaloccupied / totalrooms) * 100;
		
	cout << "The total amount of suites are: " << totalrooms << ".\n";
	cout << totaloccupied << " suites are occupied.\n";
	cout << totalrooms - totaloccupied << " suites are unoccupied.\n";
	cout << pctoccupied << " percent of the suites are occupied.\n" << endl;
	return 0;
}
Try this, feel free to post if you dont understand something

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int floors = 17, suites = 20, totalrooms = 0, count = 10;
	double totaloccupied = 0, occupied = 0;
	double pctoccupied;

	while (count < floors)
	{
		if (count != 13)
		{
			cout << "\nHow many of those suites are currently occupied on floor " << count << "?  ";
			cin >> occupied;

			if (occupied > suites)
			{
				cout << "The rooms occupied cannot exceed the number of rooms.\n";
				cout << "How many rooms on floor " << count << " are occupied?\n";
				cin >> occupied;
			}

			totaloccupied += occupied;
			totalrooms += suites;
		}
		count++;
	}

	pctoccupied = (totaloccupied / totalrooms) * 100;

	cout << "The total amount of suites are: " << totalrooms << ".\n";
	cout << totaloccupied << " suites are occupied.\n";
	cout << totalrooms - totaloccupied << " suites are unoccupied.\n";
	cout << pctoccupied << " percent of the suites are occupied.\n" << endl;
Thank you again and that makes total sense!!! I don't supposed you could sub for my teacher? LOL

Cheers
Probably could for the first 2 weeks, then you'd be entering things that is out of my territory :p
LOL Totally understand that! Thank again!

Cheers
Topic archived. No new replies allowed.