Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 10-27-2012, 01:04 AM   PM User | #2
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,220
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
This doesn't make sense. You would see why this isn't working if you run it in a debugger and look at what is happening with each value.

percentage = (double)total/ occupiedTotal;

Why are you using int type here for all 3 variables? And why are you trying to typecast one of them?
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 10-27-2012, 01:31 AM   PM User | #3
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
Sorry I forgot to change that I was playing with it earlier and forgot to change it back, but it didn't work the other way around either it should've been this to (and was to begin with) :
Code:
percentage =   occupiedTotal/ (double)total;
I initially did have all doubles but my teacher wanted me to typecast it!

Last edited by pbracing33b; 10-27-2012 at 01:36 AM..
pbracing33b is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:30 PM.


Advertisement
Log in to turn off these ads.