Please help! I'm new to programming and my Program is outputting user's input twice.

Right now It looks like this when its outputted:

Floor Room Available Occupied Occupancy Rate

1 15 10 5 33.33%

2 15 10 5 33.33%

QUESTION: I realized an array is needed but what would it look like if I want the user inputs reciprocated in my table? This is what my code looks like now.

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    //Variable Definitions
    int Floors;
    double Rooms, occupied;
    char choice;
    int count = 1;

   //Retrieving Floors of Hotel from User
    cout << "\nFirst, tell us how many floors does the hotel have: " << endl;
    cin >> Floors;

            //Not accepting an answer floors less than 1
            if (Floors > 0) {

                //Using For loop to ask the user to enter the total number of rooms for each floor.
                for (; count <= Floors; count++) {

                    cout << "How many rooms does floor " <<count << " have: " << endl;
                    cin >> Rooms;

                    //User should enter Y or N on whether any rooms are occupied, if enter anything other than those
                    // letters, need to re-enter right choice.
                        cout << "Are any of the rooms occupied on this floor? (Enter Y for Yes or N for No): " << endl;
                    cin >> choice;

                        if (choice == 'Y') {
                            cout << "Enter amount of rooms occupied: " << endl;
                            cin >> occupied;
                        }

                            else if (choice != 'Y' && choice != 'N') {
                            cout << "Wrong Answer Choice, please answer with Y or N: " << endl;
                            cin >> choice;
                            cout << "Re-Enter amount of rooms occupied: " << endl;
                            cin >> occupied;
                            }

                        if (occupied > Rooms) {
                        cout << "Error: The amount occupied cant surpass total number of rooms on that floor. Please re-enter choice: ";
                        cin >> occupied;

                        }

                }

                //Formatting Table
                cout << setw(10) << "Floor" << setw(10) << "Room" << setw(10) << "\tAvailable"
                     << setw(10) << "Occupied" << setw(15)<< "\tOccupancy Rate " << setw(10)<< endl;
                cout << "    ------------------------------------------------------------------";

                //!!!Can't get this part to output data, repeating data.
                for (int x = 1; x <= Floors; x++) {

                    cout << "\n" << setw(10) << x << setw(10);
                    cout << Rooms << setw(10);
                    cout << Rooms - occupied << setw(10);
                    cout << occupied << setw(10);
                    cout << "\t" <<  (occupied / Rooms) * 100 << "%" << setw(10) << endl;
                }
            }

            //This is printed if user enter invalid floors from beginning
            else {
                    cout << "\nIncorrect: Please enter floor numbers more than 1: " << endl;
                    cin >> Floors;
            }

    return 0;

}

Recommended Answers

All 5 Replies

Is it repeating "Floors" times - meaning is the input being displayed as many times as you have Floors?

I'm guessing you want to display the available, occupied, etc, per floor. You are correct in your assumption that you will need arrays to do this. The problem is in the first loop when you are reading in the data to the variables, the data of the previous floor is being overwritten with the data of the last floor.

For example, let's assume floor 1 has five rooms and floor 2 has three rooms. The variable Rooms will equal 3 even though there are five rooms on floor one. When doing a cout on Rooms, it will display 3.

To get you started, you will need to define floors before you declare your array.

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    //Variable Definitions
    int Floors;
    char choice;
    int count = 1;

   //Retrieving Floors of Hotel from User
    cout << "\nFirst, tell us how many floors does the hotel have: " << endl;
    cin >> Floors;

   //declare arrays
   double Rooms[Floors+1], occupied[Floors+1];

Floors[1] will correspond with floor 1, Floors[2] will be the second floor, etc.

Floors[0] will not be used. Notice the [Floors+1] in the declaration? That will increase the size of the array so we can disregard starting at 0. It looks like your loops are already set up for this.

@Kes166 Correct me if I am wrong, but I am pretty sure that in Standard C++, array declarations must use a compile-time constant for the array size. I know that the g++ compiler will allow C99 style dynamic arrays by default, but it isn't standard C++ (unless it was added since I last checked, which could well be the case).

In any case, I seem to recall that even in C, a dynamic array size has to be a constant known at the entry point of the stack frame, meaning that you can't use something from user input unless it is passed as a function parameter. I would need to check to see if I am right however.

This means that if the OP needs to size the arrays based on user input, they need to either:

  • Allocate an over-sized array and hope that it is enough - this terrible idea was a common practice in general in the past, and remains one of the largest sources of bugs and security holes (in the form of buffer overruns). I would avoid this if you have any choice about it.
  • Use explicit dynamic memory allocation using the new and delete keywords.

    bool* occupied = new bool[rooms];    // why would you use double for marking occupied rooms?
                                                             // also, note that 'occupied' is a pointer
    
    // then later...
    delete[] occupied;

This is a lot better, but does mean that you have to watch how you use the allocated array - you need to be careful to hold onto the pointer to the allocated array as long as you need it, and the delete it as soon as you no longer need it. Also, heap memory allocation is usually not taught in courses until the end, meaning that the professor would have some words with you about why you were jumping ahead like that.

This is the recommended solution for most cases, as it handles the sizing for you, but again, if your course hasn't gotten to that point it may not be what the professor expects you to do.

Since this sounds like it might be meant as a project specifically for teaching how to use arrays, the only really option I see, as bad as it is, is the 'make a big array and hope it works' option. You might want to go over this with your professor a bit more, however.

I will add that you only need a single integer value for the number of rooms on each floor, while (as I showed above) you would need just a true/false map of the rooms which are occupied on each floor. Since these values need to go together for each floor, you would be better off using a struct to hold them together, and simply have floors be an array (or vector) of that struct type. While this is probably not going to help with this assignment, consider this a preview of things you'll cover later. Also, this isn't tested code, just a thumbnail sketch of how it would work, so you probably don't want to try and use it as-is.

struct Floor 
{
    int rooms;
    std::vector<bool> occupancy_map;
};

std::vector<Floor> floors;

    // .... later ...
    int stories;
    std::cin >> stories;
    floors.resize(stories);

    // later still, in the loop that gets the information about each floor
    for (std::vector<Floor>::iterator floor = floors.begin(); floor < floors.end(); floor++)
    {
        std::cin >> floor.rooms;
        floor.occupancy_map.resize(floor.rooms);
        for (std::vector<bool>::iterator room = floor.occupancy_map.begin(); floor < floor.occupancy_map.end(); room++)
        {
             // get the occupied rooms
        }
    }

At this point, we would need more information about the assignment and the problems you're having with it to give any further advice.

You're right, it needs to be declared dynamically which I missed and can be done using a pointer. This code should allow for user input to declare an array of the appropriate size.

int Floors;
cin >> Floors;
double* Room = new  double[Floors+1];

Thanks for the correction! :)

OK... not sure if you read the rest of what I said to the OP or not, but whatever.

I will try this, Thank you everyone!!

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.