HELP : hotel reservation program.

hi. i'm writing a HOTEL program as a college assignment and right now, i'm working on the function which assigns the different types of rooms to customers.
this is how it looks:

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
#include <iostream>
using namespace std;



void checkAvail (string a, string b, int c, char d)

{
    int standard [10] = {101, 102, 103, 104, 105, 106, 107, 108, 109, 110};
    int moderate [10] = {111, 112, 113, 114, 115, 116, 117, 118, 119, 120};
    int superior [10] = {201, 202, 203, 204, 205, 206, 207, 208, 209, 210};
    int deluxe   [10] = {211, 212, 213, 214, 215, 216, 217, 218, 219, 220};
    int suite  [5] = {301, 302, 303, 304, 305};
    int studio [5] = {306, 307, 308, 309, 310};
    
    // don't know how to erase the appropriate number of rooms temporarily. 
    // for example if the customer wants 5 standard rooms, the function
    // shrinks the standard array to standard [5] instead of standard [10]. 



}

int main ()

{
    
    string checkInDate; 
    string checkOutDate;
    char type;
    int rooms;
    
    
    cout << "welcome" << '\n';
    cout << "please enter check-in date: " << '\n';
    cin >> checkInDate;
    
    cout << "please enter check-out date: " << '\n';
    cin >> checkOutDate;
    
    cout << "how many rooms you want?: " << '\n';
    cin >> rooms;
    
    cout << "please choose the type of room you want: " << '\n';
    cout << "1 for STANDARD, 2 for MODERATE, 3 for SUPERIOR, 4 for DELUXE, 5 for SUITE and 6 for STUDIO: " << '\n';
    cin >> type;
    
    checkAvail (checkOutDate, checkInDate, rooms, type);
    
    
}


i thought maybe it's a good idea to get the number of the rooms from the user, and temporarily erase "that" many rooms from the appropriate array. how should i do it? any ideas?

p.s. : i also think that putting the arrays in my "main" function is more logical, because otherwise the program keeps renting the same rooms over and over again... am i right?

p.p.s : sorry. i know i'm such a newbie. i'm very new to this.
Last edited on
Firstly, having your arrays within the checkAvail() function is not a good idea from the standpoint that they only exist for the life of the function call. Either make them global in this module or put them in main.

I assume because you are trying to erase "that" many rooms you are trying to only keep the available rooms in the array? Another approach would be to have another set of arrays that keep track of the status of each room. Then your checkAvail() function could use these new arrays to mark a room that is available and whatnot.
You can't shrink or grow arrays. Their size is fixed once they are created. Instead, I suggest keeping arrays of booleans to track which ones are taken and which aren't.
1
2
3
4
int rooms[5] = {100, 101, 102, 103, 104};
bool taken[5] = {0};
// Checking code:
if (taken[i]) cout << "Room " << i+100 << " is taken!";

Another note: I'm not sure keeping an array of room numbers is that useful. Just knowing the room type (e.g. superior) and the room number (e.g. the third room) makes the translation to "204" trivial. I'd suggest simply making a translation function that takes (type, nr) and returns (roomnr), and the opposite that translates (roomnr) into (type, nr). After all, room numbers mean nothing to the system.
Last edited on
And I guess you should allow the user to book more than one type of room... And I had an idea but I'm not sure it would work as well.. Why not have all your arrays as bool? Like Gaminic said, numbers mean nothing to the system... So Why don't you simply have something like:
1
2
3
4
5
 bool standard [10];
 bool moderate [10];
 bool superior [10];
 bool deluxe   [10];
 bool suite  [5], studio [5];


Initialize all of these to false. Then as and when the user books the rooms, you change the values to true. Have a check that a booked room cannot be re-booked by someone else... Would be simpler to manage rather than 6 int arrays for rooms, and 6 corresponding bool arrays for keeping track of the rooms...

Hope this helps! :)
guys you don't know how much you helped me... thanks... i'll try it and update it tomorrow. thanks again.
i put my arrays in my main and i tried to do what Caprico told me to do. i declared 6 BOOL arrays and assigned all of them to false. then, i passed them to my function. (i read somewhere that arrays, by default are passed by reference... right?) now the thin is :


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
//
//  main.cpp
//  assignment 2
//
//  Created by Amirali Monfared on 1/6/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
using namespace std;




void checkAvail (string indate, string outdate, int rooms, char type, bool arg1[], bool arg2[], bool arg3[], bool arg4[], bool arg5[], bool arg6[])

{
    
    if (type == '1')
    {
        for (int i = 0; i <= rooms; i++)
        {
             arg1[i] = {true};    //this part is not working... is this logic, not correct? or the coding should be different?
        }
    }



}

int main ()

{
  
    bool standard [10] = {false};
    bool moderate [10] = {false};
    bool superior [10] = {false};
    bool deluxe   [10] = {false};
    bool suite  [5] = {false};
    bool studio [5] = {false};

    string checkInDate; 
    string checkOutDate;
    char type;
    int rooms;
    
    
    cout << "welcome" << '\n';
    cout << "please enter check-in date: " << '\n';
    cin >> checkInDate;
    
    cout << "please enter check-out date: " << '\n';
    cin >> checkOutDate;
    
    cout << "how many rooms you want?: " << '\n';
    cin >> rooms;
    
    cout << "please choose the type of room you want: " << '\n';
    cout << "1 for STANDARD, 2 for MODERATE, 3 for SUPERIOR, 4 for DELUXE, 5 for SUITE and 6 for STUDIO: " << '\n';
    cin >> type;
    
    checkAvail (checkInDate, checkOutDate, rooms, type, standard, moderate, superior, deluxe, suite, studio);
    
    
}


i guess i don't know how exactly i should check my rooms...
Last edited on
I'll solve your main issue soon, but just another suggestion/question... Why are you passing the check-in/check-out date to the function? It has no use for them. Ummm...

1) Lines 35-40, your initialization will not work.. you need to set each element of the array to false.. Your current code just sets the first element of the array to false (I think, I'm not sure. Someone correct me if I'm wrong)... Use loops if you want, like:
1
2
3
4
5
for (i=0; i<=9; i++)
	standard[i]=moderate[i]=superior[i]=deluxe[i]=false;

for (i=0; i<=4; i++)
	suite[i]=studio[i]=false;



2) Line 21: You should have condition as i<rooms, and not <=. Otherwise, your function books one room extra (think about it). Also, the problem that will occur is, suppose a user books say 5 rooms in deluxe. Now another user comes and wants to book 5 rooms for deluxe, you'll end up over-writing the previous users tooms (Coz you initialize i to 0 again). So to check for pre-booked rooms, have:
1
2
3
4
5
6
7
8
9
10
11
12
int i=0, j;

while (arg1[i]!=false)    //This will bring i to the next empty room.
	i++;

if (i==9)
       cout<<"Sorry, but no rooms are available in this type. Maybe you'd like to choose another type?";

else
for (j=i; j<=(rooms+i); j++)   //This will fill the next n rooms
	arg[j]=true;        //starting from the first available empty room,
                            //Rather than over-writing previous rooms. 


3) Have your entire program in the main (or the booking portion of it at least) in a do-while loop, to allow multiple users. For now, you can have a max of 5 users for simplicity. Also try to have a do-while loop for choosing multiple types of rooms. (However, this entire point 3, this is just to kindof advance your code. If you're not comfortable with too many loops, well and good, not at all an issue, it might make your code buggy and unnecessarily long. Again, someone please give a second opinion...)

Again, hope this helps... :)
Topic archived. No new replies allowed.