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 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int total_floors, floor, rooms = 0, occupied = 0, unoccupied, percentage;
cout << "How many floors does the office building have? ";
cin >> total_floors;
while (total_floors <= 0 || total_floors == 13)
{
cout << "This building does not have any sub levels or a 13th floor please try again. \n"
<< "How many floors does the office building have? ";
cin >> total_floors;
}
while (total_floors < 1 || total_floors >14)
{
cout << "Enter a floor number between 1 and 14 skipping floor 13. \n"
<< "How many floors does the office building have? ";
cin >> total_floors;
}
for (floor = 1; floor <= total_floors; floor++)
{
if (floor == 13)
continue;
cout << "How many rooms are on floor " << floor << ": ";
cin >> rooms;
cout << "How many rooms on floor " << floor << " are occupied? ";
cin >> occupied;
}
cout << setprecision(2) << fixed << showpoint;
percentage = (occupied / rooms) * 100;
unoccupied = rooms - occupied;
cout << "\nPercentage of rooms occupied is " << percentage << " percent." << endl;
cout << "There are a total of " << rooms << " rooms." << endl;
cout << "Out of the " << rooms << " rooms, " << occupied << " are occupied and "
<< unoccupied << " are unoccupied." << endl;
cout << endl;
return 0;
}
|
Last edited on
Do occupied * 100 / rooms, instead. If you try to divide integers you'll use integer division, and if x < y then x / y = 0.
Change line 44 to:
|
percentage = ((float) occupied / rooms) * 100;
|
or helios' way.
EDIT:
I want to ask you a question helios. Why does C-like type casting
((float) x) make
x float but not functional notation
(float (x))? I tested it.
Also the C++
static_cast<type>(expression) does the same as the functional notation.
Is it because the C-style type casting does not error check?
Last edited on
IINM, T(x) and (T)x are equivalent as long as T is not a two-word type name, such as 'unsigned int'.
Hmmm... well gives different results for me.
Using GNU g++ -std=c++14.
Last edited on