Question:
My C++ Code Is Still Accepting Negative Numbers After Making Changes To It? Can Someone Help Me Fix It?
?
2013-10-23 09:02:11 UTC
Hey everyone with my code. I will post the instructions below as well as my code below. However, this is the problem I'm having my C++ code. When I run it, and I get that question from the command prompt "How many rooms are occupied on this floor?" I enter a negative number and it still accepts it as if it were correct. I don't want any negative number to be accepted. So, how can I fix my code so it won't accept negative numbers for that question? Also, are there any other problems I have with my code? Thank you all very much for your help.

P.S. When you post your answer, please just write out the whole code that I provided with your corrections within that code, it make it lot easier for me to understand so I could have a overall look of the code rather than just posting snippets of the corrections in your post. Thanks.

Problem: Write a program that calculates the occupancy rate for a hotel. The program should start by asking the user how many floors the hotel has. A loop should then iterate once for each floor. In each iteration, the loop should ask the user for the number of rooms on the floor and how many of them are occupied. After all the iterations, the program should display how many rooms the hotel has, how many of them are occupied, how many are unoccupied, and the percentage of rooms that are occupied. The percentage may be calculated by dividing the number of rooms occupied by the number of rooms.

NOTE: It is traditional that most hotels do not have a thirteenth floor. The loop in this program should skip the entire thirteenth iteration.

Input Validation: Do not accept a value less than one for the number of floors. Do not accept a number less than 10 for the number of rooms on a floor.

#include
#include
using namespace std;

int main()
{
//create double variables to hold input from user
double floors=0,
count=0,
rooms,
rooms_total=0,
occupied,
occupied_total=0,
unoccupied,
number=0,
percentage=0;

//When program starts, it will ask for input from user

cout << "How many floors in your hotel?\n";
cin >> floors;

while (floors < 1)//if user enters 0 or less floors, error
{
cout << "ERROR: You cannot enter 0 or less for the number of floors!\n";
cout << "PLEASE TRY AGAIN.\n";
cin >> floors;
}
while (number < floors)//loop to ask for input from user, once for each floor
{
//add one to count number during each loop
count++;

//if count == 13 (13th floor) we will stop loop, and start over at top
if (count == 13)
{
//if count is equal to 13, give the following message, then skip to top
cout <<"We will be skipping floor #13, as is common in most hotels.\n";
continue;
}
//asks user to enter number of floors
cout <<"How many rooms on floor number " << count << "? (cannot be less than 10)\n";
cin >> rooms;
while (rooms <10)//if user enters less than 10 rooms per floor- error
{
cout <<"ERROR: Cannot be less than 10 rooms! Please try again.\n";
cin >> rooms;
}
//asks users how many rooms in floor
cout <<"How many rooms are occupied on this floor " << count <<"? (cannot be less than 1)\n";
cin >> occupied;
while (occupied> 10) {

//if user enters more occupied than rooms on floor, error
cout <<"ERROR: You cannot have more occupied rooms than total rooms on floor! Please try again.\n";
cin >> occupied;
}
while (occupied < rooms) {
//if user enters less occupied than rooms on floor, error
cout <<"ERROR: You cannot have less occupied rooms than minimum rooms on floor! Please try again.\n";
cin >> occupied;
}


//add one to number during each loop
number++;
//keep running total of rooms_total and occupied_total
rooms_total+=rooms;
occupied_total+=occupied;
}

//our totals for the solution
unoccupied = rooms_total - occupied_total;
percentage = occupied_total / rooms_total *100;

//shows user the solutions
cout <<"Total Floors: " << floors << endl;
cout <<"Total Rooms: " << rooms_total << endl;
cout <<"Total Occupied: " << occupied_total << endl;
cout <<"Total Unoccupied: " << unoccupied << endl;
cout <<"Percentage of rooms occupied is " << fixed << setprecision(2) << percentage << "%.\n";

system("pause");
return 0;
}
Three answers:
?
2013-10-23 12:33:49 UTC
No reason to have a separate variables count and number. Indeed, because of floor 13, they are not going to be equal. Instead of a while loop, use a for loop for the floors.



Also, you can make most of these variables int instead of double. I suspect that might be causing your negative number issue, though it may just be an issue with cin not handling negative numbers the way you want. Perhaps you should try it with scanf.
?
2013-10-23 16:18:07 UTC
You cant have less than 0 rooms occupied.

Or

More than the number of rooms on the floor occupied.



Try

while (occupied > rooms ) {

In place of

while (occupied> 10) {

Since you can have more than 10 rooms. The limit is how many rooms are available.



and occupied < rooms to occupied <0

So you cannot have less than 0 occupancy, But you can have 0-10 occupancies.









Then, you will need to reset the number of rooms and occupied to 0 for the next floor. Other wise it will just keep using the same values over and over.



EG:

//keep running total of rooms_total and occupied_total

rooms_total+=rooms;

occupied_total+=occupied;

//Reset totals

rooms=0;

occupied=0

}





Sure you just want the answer, but that translates to "i don't know enough to make changes to my own code"



Edit:: Notice you've asked this question 3 times already and every time someone gave you the answer.... wow.... i sure wasted my time....
justme
2013-10-24 13:22:47 UTC
Its because you are using doubles for your variables (percentage is the only one that needs to be a double).



change your if's and while's to read like this:



while (floors < 1.0)

if (count == 13.0)

and so on.



Or change your variables to ints, with the exception of percentage which should be a double.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...