Working w/ Vectors

Hi, I wrote a program for hotel occupancy using vectors. Everything seems to run the way it's suppose to however I was wondering if someone could please critique my work or tell me if there's a more efficient way of doing this since I am still very new to vectors. Thank you
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  //I will be writing a program that calculates the occupancy rate for a hotel. The program will start by asking user how many floors the hotel has. A for loop should then iterate once for each floor. In each iteration, the loop will ask the user for the number of rooms on the floor and how many of them are occupied. After all the iterations, the program will 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 for each individual floor. It will also display the total amounts of rooms, the total amount of rooms occuiped, the total amount of rooms unoccupied and the total percentage.  Once the program display the necessary information it will ask the user if he/she wants to input date for another hotel.  The Program will then provide the user with the file once the program has been successfully complete.

//NOTE:  For this particular problem you must use vectors only.

#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
using namespace std;
int main()
{
    string date;
    string Hotel;
    char Option;
    int Floors = 0;
    
    
    int Roomz = 0;
    int RoomzOccupied = 0;
    int RoomzUnOccupied = 0;
    int Percent = 0;

    vector <int> Rooms;
    vector <int> RoomsOccupied;
    vector <int> RoomsUnOccupied;
    vector <double> Percentage;
    
    
    do
    {
        int TotalRooms = 0;
        int TotalRoomsOccupied = 0;
        int TotalRoomsUnOccupied = 0;
        double TotalPercentage = 0.0;
        
        cout << "Input date: ";
        cin.ignore();
        getline(cin, date);
        cout << "Input Hotel: ";
        getline(cin, Hotel);
        
        
        cout << "Input number of floors in hotel: ";
        cin >> Floors;
        
        while (Floors < 1)
        {
            cout << "Sorry, you may not have less than 1 floor. Please re-enter answer ";
            cin >> Floors;
        }
        
        for (int i = 0; i < Floors; i++)
        {
            cout << "Input Number of Rooms for Floor #" << i + 1 << " ";
            cin >> Roomz;
          
            while (Roomz < 0) //Look out for this
            {
                cout << "The amount of rooms must be greater than zero.  Please re-enter # of rooms below: ";
                cin >>Roomz;
                Rooms.push_back(Roomz);
            }
              Rooms.push_back(Roomz);
            
            cout << "Input Number of Rooms occupied for Floor #" << i + 1 << " ";
            cin >> RoomzOccupied;
            
            while (RoomzOccupied < 0)
            {
                cout << "The amount of rooms occupied cannot be less than zero. ";
                cin >> RoomzOccupied;
                RoomsOccupied.push_back(RoomzOccupied);
            }
          
            
            
            
            while (RoomzOccupied > Roomz) //Look out for this
            {
                cout << "The amount of room occupied cannot be greater than the number of rooms.  Please re-input number ";
                cin >>RoomzOccupied;
                 RoomsOccupied.push_back(RoomzOccupied);
            }
            
            RoomsOccupied.push_back(RoomzOccupied);
            
        }
        
        for (int i =0; i<Floors; i++)
        {
            RoomzUnOccupied = Roomz -  RoomzOccupied;
            RoomsUnOccupied.push_back(RoomzUnOccupied);
            RoomsUnOccupied[i] = (Rooms[i] - RoomsOccupied[i]);
            Percent = static_cast<double>(RoomzOccupied)/static_cast<double>(Roomz);
            Percentage.push_back(Percent);
            Percentage[i] = static_cast<double>(RoomsOccupied[i])/static_cast<double>(Rooms[i]);
        }
        cout << endl;
        cout << "This chart below represents the stats for " <<Hotel << " Hotel on " << date << endl << endl;
        cout << "Floor: " << setw(5) << "Rooms: " <<setw(6) << "Occupied Rooms: " << setw(6) << "Unoccupied Rooms: " << setw(6) << "Percentage of Occupied Rooms \n";
        
        
        for (int i = 0; i<Floors; i++)
        {
            
            
            cout << setw(3);
            cout << i+1 << setw(6) << Rooms[i] << setw(11) << RoomsOccupied[i] << setw(17) << RoomsUnOccupied[i] << setw(25) << (Percentage[i] * 100)<< "%";
            cout << endl<<endl;
            cout << "-----------------------------------------------------------------------------------------------------\n\n";
            
            
        }
        for (int i = 0; i<Floors; i++)
        {
            TotalRooms += Rooms[i];
            TotalRoomsOccupied += RoomsOccupied[i];
        }
        TotalRoomsUnOccupied = TotalRooms - TotalRoomsOccupied;
        TotalPercentage = static_cast<double>(TotalRoomsOccupied)/static_cast<double>(TotalRooms);
        
        cout << "Total Amount:\n\n";
        cout << "Total Rooms: " << TotalRooms << endl;
        cout << "Total Occupied Rooms: " << TotalRoomsOccupied << endl;
        cout << "Total Unoccupied Rooms: " << TotalRoomsUnOccupied << endl;
        cout << "Total Percentage: %" << setprecision(2) << fixed << (TotalPercentage*100) << endl;
        cout << endl << endl;
        
        cout << "Would you like to continue the program (Y/N)? ";
        cin >> Option;
        
        while (Option != 'y' && Option != 'Y' && Option != 'N' && Option != 'n' )
        {
            
            cout << "Please input Y or N.  Thank you \n";
            cin >> Option;
            
            
        }
        
        
    }
    while (Option == 'Y' || Option == 'y');
        
        
    
    
    
    
    
    
    return 0;
}


Last edited on
> Everything seems to run the way it's suppose to
your testing is awful
1
2
3
4
5
6
            while (Roomz < 0)
            {
                cout << "The amount of rooms must be greater than zero.  Please re-enter # of rooms below: ";
                cin >>Roomz;
                Rooms.push_back(Roomz); //¡¿?!
            }
¿why are you inserting possible invalid values into the vector?

1
2
3
4
5
           while (RoomzOccupied < 0)
           //...

            while (RoomzOccupied > Roomz) //you may bypass this with negative values
           //... 



1
2
3
            RoomzUnOccupied = Roomz -  RoomzOccupied;
            RoomsUnOccupied.push_back(RoomzUnOccupied);
            RoomsUnOccupied[i] = (Rooms[i] - RoomsOccupied[i]);
awful variable naming
also, ¿what's the point of the first computation, if you are going to overwrite it inmediately?
No need to be rude. Just answer the question the like a normal person next time...


kdrewes wrote:
No need to be rude. Just answer the question the like a normal person next time...


Tone is often difficult to interpret on a forum, ne555 is very knowledgeable and experienced (you might want to read the bio), and he is really trying to help . Perhaps you could consider developing a better tolerance for criticism good or bad?
Last edited on
Consider writing many small functions (each function doing one thing) instead of one gigantic main.

For instance:

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>

int get_int( std::string prompt, int min_value = 0, int max_value = 1'000'000'000 )
{
    int value ;
    std::cout << prompt << " [" << min_value << '-' << max_value << "]: " ;

    if( std::cin >> value ) // if the user entered a number
    {

        if( value >= min_value && value <= max_value ) return value ;
        else std::cout << "error: value is out of range. try again.\n" ;
    }

    else // input failure: non-numeric input
    {
        std::cout << "invalid input: not a number. try again\n" ;
        std::cin.clear() ; // clear the failed state of stdin
        std::cin.ignore( 1000, '\n' ) ; // throw away this bad input line
    }

    return get_int( prompt, min_value, max_value ) ;
}

void print_occupancy( const std::vector<int>& total_rooms, const std::vector<int>& occupied_rooms )
{
    if( total_rooms.size() != occupied_rooms.size() )
    {
        std::cout << "invalid rooms information\n" ;
        return ;
    }

    std::cout << "Floor  Rooms  Occupied Rooms  Unoccupied Rooms  % occupied Rooms\n"
              << std::fixed << std::setprecision(2) ;

    for( std::size_t i = 0 ; i < total_rooms.size() ; ++i )
    {
        const int unoccupied_rooms = total_rooms[i] - occupied_rooms[i] ;
        const double percent_occupancy = occupied_rooms[i] * 100.0 / total_rooms[i] ;
        std::cout << std::setw(4) << i+1
                  << std::setw(7) << total_rooms[i]
                  << std::setw(12) << occupied_rooms[i]
                  << std::setw(17) << unoccupied_rooms
                  << std::setw(20) << percent_occupancy << '\n' ;
    }

}

void run_once()
{
    std::string date ;
    std::cout << "date: " ;
    std::getline( std::cin, date ) ;

    std::string hotel ;
    std::cout << "hotel: " ;
    std::getline( std::cin, hotel ) ;

    const int nfloors = get_int( "enter number of floors", 1, 1'000 ) ;

    std::vector<int> total_rooms ;
    std::vector<int> occupied_rooms ;
    for( int i = 0 ; i < nfloors ; ++i )
    {
        std::string floor_number = std::to_string(i+1) ;

        const int nrooms =  get_int( "number of rooms in floor #" + floor_number, 1, 1000 ) ;
        total_rooms.push_back(nrooms) ;

        const int n_occupied_rooms = get_int( "number of rooms occupied in floor #" + floor_number, 0, nrooms ) ;
        occupied_rooms.push_back(n_occupied_rooms) ;
    }

    std::cout << "stats for Hotel " << hotel << " on date " << date << '\n' ;
    print_occupancy( total_rooms, occupied_rooms ) ;
}

bool run_again()
{
    std::cout << "run once again (y/n)? " ;
    char c ;
    return std::cin >> c && ( c == 'Y' || c == 'y' ) ;
}

int main()
{
    do run_once() ;
    while( run_again() ) ;
}
Thank you JLBorges, much appreciated.
Topic archived. No new replies allowed.