Thread: TypeCasting
View Single Post
Old 10-27-2012, 12:23 AM   PM User | #1
pbracing33b
New Coder

 
Join Date: Mar 2012
Posts: 70
Thanks: 6
Thanked 0 Times in 0 Posts
pbracing33b is an unknown quantity at this point
TypeCasting

I have a program that is suppose to find the percentage of occupied rooms in the hotel. I have most of my program running, but when I try to typecast my percentage the percentage isn't coming out right and I am not sure as to why.
Can someone please take a look at my code to see why this isn't working?
Code:
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	//Varibles
	int occupied = 0,floors = 0,rooms = 0, heartBreak = 0;
	int percentage = 0, total = 0, occupiedTotal = 0,unoccupied = 0, unoccupiedTotal = 0;
	int mostempty = 0, HBfloor;

	//Input
	cout << "How many floors does the hotel have? ";
	cin >> floors;
	
	while (floors < 1 )   // Validate input
		{
		cout << "Please enter floors greater than or equal to ten.\n";
		cout << "How many floors does the hotel have? ";
		cin >> floors;
		}
	
	//Run a for loop to get the amount of rooms, and it's occupancy rate!
	for (int cnt = 1; cnt<=floors; cnt++)
	{
		if (cnt == 2)//Skip Floor two
		{
			continue;
		}
		
		cout << "How many rooms are on floor "<<cnt<<"?";
		cin >> rooms;
		
		
		while (rooms < 10)   // Validate input
		{
			cout << "Please enter rooms greater than or equal to ten.\n";
			cout << "How many rooms are on floor "<<cnt<<"?";
			cin >> rooms;
		}
		total += rooms;//Accumulator

		cout<<"How many of those are occupied?";
		cin>> occupied;
		
		
		while(occupied > rooms)//validate input
		{
			cout<<"Error, Number of occupied rooms cannot exceed rooms listed \n";
			cout<<"How many of those are occupied?";
			cin>> occupied;
		}
		occupiedTotal += occupied;//Accumulator
		unoccupied = rooms - occupied;
		if (unoccupied > mostempty)//find the floor with the least amount of rooms occupied
		{
			mostempty = unoccupied;
			HBfloor = cnt;
		}
		
		
		
	}
		
		//Processing Section
		unoccupied =  total - occupiedTotal;
		percentage =   (double)total/ occupiedTotal;
		
		//output		
		cout<<"The hotel has a total " <<total<< " rooms!"<<endl;
		cout<<occupiedTotal<< " are occupied."<<endl;
		cout<<unoccupied<<" are empty."<<endl;
		cout << fixed << showpoint << setprecision(1);
		cout<<"The occupancy rate is "<<percentage<<"%"<<endl;
		cout<<"The heartbreak floor is "<<HBfloor<<" with "<<mostempty<<" empty rooms." <<endl;
		system("pause");
		return 0;
}
pbracing33b is offline   Reply With Quote