I am having a hard time with my homework and I was wondering if someone could help.
I have to create a program that uses a while loop to ask questions about a hotel. It first ask the number of the top floor then asks How many rooms are on the floor? and How many of those rooms are occupied?. Then at the end it displays the total number of rooms, the total of occupied rooms, the total of empty rooms, the occupancy rate, and the "Heartbreak floor" which is the floor with the least occupied rooms and the smallest number. Plus the program has to skip floor 6, just like most hotels don't have a floor 13.
My first problem is that I need in invalid option, that does not display the rest of the information when you enter 0. I have gotten it to displays Invaild but it also displays 0 total rooms 0 occupied etc which I don't want.
The second problem is that I have figured out how to display the least rooms occupied with an if < statement but I'm not sure how to display the floor number also.
The third problem is that I'm not sure how to skip floor 6. I tried saying while ((topfloor>=1) && (topfloor != 6)) but this only resulted in the program running once and then stopping.
I think that's all my problems. If anyone could help I would appreciate it. Thanks, SSR

PS I know the occupancy rate looks weird but that was the only way I could get it to display the tenth decimal place, otherwise it would just display 0.

Here is the code:

#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{

int floor = 0;
int highnum = 20000;
double occupancy;
int topfloor, occupiedrooms, rooms, roomtotal, occupiedroomstotal,
emptyrooms;

cout << "What is the number of the top floor? ";
cin >> topfloor;

if (topfloor<1)
cout <<"Invalid number of floors. Please try again."<<endl;

while (topfloor>=1)
{
floor = floor +1;
cout <<"How many rooms are on floor " <<floor <<"? ";
cin >> rooms;
cout <<"How many rooms are occupied? ";
cin >> occupiedrooms;

if (occupiedrooms< highnum)
highnum = occupiedrooms;

roomtotal += rooms;
occupiedroomstotal += occupiedrooms;
emptyrooms = roomtotal-occupiedroomstotal;
topfloor = topfloor - 1;
}

cout << "The hotel has a total of "<< roomtotal <<" rooms."<<endl;
cout << occupiedroomstotal <<" are occupied." <<endl;
cout << emptyrooms <<" are empty." <<endl;

occupancy =(occupiedroomstotal*10000)/roomtotal;
occupancy = occupancy/100;

cout << "The occupancy rate is "
<<setprecision(1)<<showpoint<<fixed<<occupancy <<"%." <<endl;
cout << "The Heartbreak floor is: " <<floor <<" with only " <<highnum <<" occupied rooms." <<endl;
return 0;
}

Code tags please.

[code]

// paste code here

[/code]

Makes things much easier to read.

cout << "What is the number of the top floor? ";
cin >> topfloor;

if (topfloor<1)
cout <<"Invalid number of floors. Please try again."<<endl;

You say "Please try again" here, but you give the user no chance to try again. You need this in a while loop or a do-while loop so it asks the user again if there is bad input.

do
{
cout << "What is the number of the top floor? ";
cin >> topfloor;

if (topfloor<1)
cout <<"Invalid number of floors. Please try again."<<endl;
}
while (topfloor < 1);

Actually I don't think that worked because it started to ask about the number of occupied rooms which isn't what I want. if topfloor<1 then I want it to fall through the loop and not execute any of the steps at all. I also figured out how to get the floor number along with the lowest number of occupied rooms, I added an if statement that looks like this:

int highfloor = 0;
if (occupiedrooms< highnum)
highfloor = floor;

Actually I don't think that worked because it started to ask about the number of occupied rooms which isn't what I want. if topfloor<1 then I want it to fall through the loop and not execute any of the steps at all. I also figured out how to get the floor number along with the lowest number of occupied rooms, I added an if statement that looks like this:

int highfloor = 0;
if (occupiedrooms< highnum)
highfloor = floor;

It started to ask you about the number of occupied rooms after you entered a top floor less than 1? It shouldn't have. With my revision it keeps asking you for the top floor until you give it a legal top floor. You'll never get to that second question if you don't answer the first one correctly in my revision. Do you want to keep asking for a top floor until they give you a good one or not? If not, what is supposed to happen when they enter a bad number? Is the program supposed to end? Without my do-while loop, you say "Try again" and never ask them for a top floor again.

I tried it and it does work like you said. I think I didn't explain myself very well though.
if (topfloor<1) then it needs to display invalid and not show the total of the room, occupied, and empty rooms. Which is currently doing. I've tried setting an invalid message after the while loop but at that point the topfloor is already = to 0 because it has -1 each time through the loop till it got to 0.
I have solved the other two problems.

I tried it and it does work like you said. I think I didn't explain myself very well though.
if (topfloor<1) then it needs to display invalid and not show the total of the room, occupied, and empty rooms. Which is currently doing. I've tried setting an invalid message after the while loop but at that point the topfloor is already = to 0 because it has -1 each time through the loop till it got to 0.
I have solved the other two problems.

So the program is supposed to end when they enter a top floor less than 1? How about just ending the program right then with return 0 ?

if (topfloor<1)
{
     cout <<"Invalid number of floors." <<endl;
     return 0;
}

Otherwise if you want to keep prompting till you get a good value, do the loop I suggested, which I thought you wanted with the "Try again" display.

Or if you only want to prompt once, but then go through the whole program even if they enter a bad value, but skip the code at the end of the program, you can set a boolean flag:

bool haveValidTopFloor = true;
if (topfloor<1)
{
     cout <<"Invalid number of floors." <<endl;
     haveValidTopFloor = false;
}

then later, take whatever code you want executed only if the top floor was originally valid and put it in an if statement:

if (haveValidTopFloor)
{
     // code to execute only if user originally
     // entered a valid top floor.
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.