Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,262 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,512 people online right now. Registration is fast and FREE... Join Now!




Decimal in C++

 
Reply to this topicStart new topic

Decimal in C++

jonathan3174
2 Nov, 2006 - 12:48 PM
Post #1

New D.I.C Head
*

Joined: 26 Oct, 2006
Posts: 16


My Contributions
I was attempting to create a program that would allow a user to enter how many floors a hotel had. After determining that number, it would create a loop for that many floors...

Ex. 3 floors....

Floor 1 has 10 rooms, 5 occupied.
Floor 2 has 10 rooms, 5 occupied.
Floor 3 has 10 rooms, 5 occupied.

After completed, it would generate that there were a total of 30 rooms. 15 occupied, 15 unoccupied, and an occupany rate of 50%.

My problem occured when I attempted to take the occupied rooms divided by the total amount of rooms, because it generates a small decimal number which C++ regards as 0.

Any help would be greatly appreciated! Thanks!


CODE

#include<iostream>
using namespace std;
void main()

{
    
    int floors;
    int room;
    int total;
    int occu;
    int roomtotal = 0;
    int occutotal = 0;
    int unopen = 0;
    int unopen1 = 0;
    int occupyRate = 0;
    cout<<"How many floors does the hotel have?\n";
    cin>>floors;
    if (floors <= 0)
    {    cout<<"Please enter a number greater than 0. \n";
        cout<<"How many floors does the hotel have?\n";
        cin>>floors;
    }
    for (total = 1; total <= floors; total++)
    {
        cout<<"How many rooms on floor " <<total <<"?" <<endl;
        cin>>room;
        roomtotal += room;
        cout<<"How many of those rooms are occupied? \n";
        cin>>occu;
        occutotal += occu;
        unopen = (room - occu);
        unopen1 += unopen;
    }
    occupyRate = (occutotal/roomtotal)*100;
    cout<<"The hotel has a total of " << roomtotal <<" rooms." <<endl;
    cout<< occutotal <<" are occupied." <<endl;
    cout<< unopen1 <<" are unoccupied." <<endl;
    cout<<"The occupancy rate is " <<occupyRate <<".0%" <<endl;

}



User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Decimal In C++
2 Nov, 2006 - 01:19 PM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(jonathan3174 @ 2 Nov, 2006 - 01:48 PM) *

I was attempting to create a program that would allow a user to enter how many floors a hotel had. After determining that number, it would create a loop for that many floors...

Ex. 3 floors....

Floor 1 has 10 rooms, 5 occupied.
Floor 2 has 10 rooms, 5 occupied.
Floor 3 has 10 rooms, 5 occupied.

After completed, it would generate that there were a total of 30 rooms. 15 occupied, 15 unoccupied, and an occupany rate of 50%.

My problem occured when I attempted to take the occupied rooms divided by the total amount of rooms, because it generates a small decimal number which C++ regards as 0.

Any help would be greatly appreciated! Thanks!


CODE
    
    int floors;
    int room;
    int total;
    int occu;
    int roomtotal = 0;
    int occutotal = 0;
    int unopen = 0;
    int unopen1 = 0;
    int occupyRate = 0;


Hi

Rounding often occurs when you use an integer instead of a double or float

hope that helps
User is offlineProfile CardPM
+Quote Post

NyeNye
RE: Decimal In C++
2 Nov, 2006 - 04:31 PM
Post #3

D.I.C Head
**

Joined: 24 Sep, 2006
Posts: 248


My Contributions
besting to use a much larger data types..rather than int
User is offlineProfile CardPM
+Quote Post

horace
RE: Decimal In C++
3 Nov, 2006 - 01:04 AM
Post #4

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
the problem is in the statement
occupyRate = (occutotal/roomtotal)*100;

where the division " occutotal/roomtotal "gives a value less than 1 and as the result is an int truncates it to 0

if you multiply first, i.e.
occupyRate = (occutotal*100/roomtotal);
it fixes the problem

or you could cast one of the ints to a float, i.e.
occupyRate = ((float)occutotal/roomtotal)*100;

when the result of the division would be a float but gives the compiler warning "[Warning] converting to `int' from `float' " when you convert the result back to an int
User is offlineProfile CardPM
+Quote Post

NyeNye
RE: Decimal In C++
3 Nov, 2006 - 01:44 AM
Post #5

D.I.C Head
**

Joined: 24 Sep, 2006
Posts: 248


My Contributions
hmmm

the besting he could do is to assign or use a much larger datatype...and the logic for it..





User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Decimal In C++
3 Nov, 2006 - 05:40 AM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
I would suggest that GergoryH's suggestion is the most efficient solution...as the occupancy rate is a percentage, a simple float data type instead of integer is the way to go.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:05PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month