We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,401 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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. I have set a break point and the percentage value is always 0 I'm not sure as to well. I have to have this in TypeCasting I would love to run all doubles but I'm not allowed.
Can someone please take a look at my code to see why this isn't working?

#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 =  occupiedTotal / double(total);

        //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;
}
2
Contributors
1
Reply
8 Hours
Discussion Span
6 Months Ago
Last Updated
2
Views
pbracing33b
Newbie Poster
12 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

line 70 is attempting to store a value that is less than zero into an integer, which can not hold such values. If you are not allowed to use doubles than multiply the result of the division by 100 and assign that to the integer. Example: (1/2) * 100 = 0.5 * 100 = 50, meaning 50%.

Ancient Dragon
Achieved Level 70
Team Colleague
32,157 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0480 seconds using 2.56MB