help with class

hi am finished with my functions for the class but i am having trouble figuring out how to implement them in main o get the results i want my program is suppose to
Your program should display the following menu of alternatives Please type 1 for "Smoking" and Please type 2 for "Non-Smoking". If the person types 1, your program should assign a seat in the smoking section. If the person types 2, your program should assign a seat in the non-smoking section. Your program should print a boarding pass indicating the person's seat number and whether it is in the smoking or non-smoking section of the plane.
Your program should, of course, never assign a seat that has already been assigned. When either section is full, your program should ask the person if it is acceptable to be placed in the other section (if there is available seating). If yes, then make the appropriate seat assignment. If no, then print the message "Next flight leaves in 3 hours". This process should repeat until the user decides to quit the program.

so far this is all my functions
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
#include "reservation.h"
#include <iostream>
#include <time.h>
using namespace std;

Reservation::Reservation()
{
    seats = 10;
    plane[seats] = { 0 };
}

int Reservation::getSeats()
{
    return seats;
}

int Reservation::reserveSmoking(int num)
{

    for (int i = 0; i < seats; i++) {

        cout << "Please select from one(0) to four(4): ";
        cin >> num;
        while (num <0 ||num >4) {
            cout << "Please select from one(0) to four(4): ";
            cin >> num;
        }
 reduceAvailableSeats();
        if (plane[i] == i) {

            return num;
        }
        else {
            i++;
        }

    }
    return -1;


}

int Reservation::reserveNonSmoking(int num)
{

    while (num < 5 || num > 9) {
        cout << "Please select from one(5) to nine(9): ";
        cin >> num;
    }

    for (int i = 0; i < seats; i++) {
        cout << "Please select from one(1) to four(4): ";
        cin >> num;
        while (num < 5 || num > 9) {
            cout << "Please select from one(5) to nine(9): ";
            cin >> num;
        }
        if (plane[i] == i) {
            return num;
        }
        else {
            i++;
        }
    }
    return -1;
}
int Reservation::reserveRandomSmoking()
{
    for (int i = 0; i < seats; i++) {
        srand(time(0));
        seats = rand() % 5;
        if (plane[i] == i) {
            return seats;
        }
        else {
            i = i + 1;
        }
    }
    return 0;
}
int Reservation::reserveRandomNonSmoking()
{
    for (int i = 0; i < seats; i++) {
        srand(time(0));
        seats = 6 + rand() % 5;
        if (plane[i] == i) {
            return seats;
        }
        else {
            i = i + 1;
        }
    }
    return 0;
}
bool Reservation::availableSmoking()
{
    int count = 0;
    for (int i = 0; i < 5; i++)
        if (plane[i] != 0)
            count++;
    return count < 5;
}
bool Reservation::availableNonSmoking()
{

    int count = 0;
    for (int i = 0; i < 5; i++)
        if (plane[i] != 0)
            count++;
    return count < 5;
}

void Reservation::reduceAvailableSeats()
{  while(seats<=0)
    seats--;

}

this is my main so far , like i said im unsure how to set up the functions so i get my result

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
    Reservation object;
    int answer;
    cout << "please type 1 for smoking or please type two for non-smoking";

        while(object.availableSmoking()) {

            cin >> answer;
            if (answer == 1) {
    cout << "you are in the smoking section and your seat number is" << object.reserveSmoking(answer) << endl;



        }



    }

this is my .h
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
#ifndef RESERVATION_H
#define RESERVATION_H


class Reservation
{
public:
    Reservation();



    int getSeats();
    int reserveSmoking(int);
    int reserveRandomSmoking();
    int reserveRandomNonSmoking();
    int reserveNonSmoking(int);
    bool availableNonSmoking();
    bool availableSmoking();



private:
    void reduceAvailableSeats();
    int seats;
    int plane[10];




};

#endif // RESERVATION_H



Last edited on
you are making it too hard on yourself. you should probably just have a count of available seats of each type and return that to know how many you have (eg available smoking returned) and decrement it when you sell a ticket. It will work your way, but its a lot more complex.

time is <ctime> you used a C header by accident.

as for setting it up, it looks like you need to handle all kinds of cases, here is a rough outline in pseudo-code
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
int answer;

    if(!object.availableSmoking() && (!object.availableNonSmoking()) cout "there are no seats left words"; 
//do something to stop program or whatever for full plane?
    else //plane not full 
    if(!object.availableSmoking()) 
    {
       cout "there are no smoking seats left words";
       do you want to try another flight or take a nonsmoking seat actions
     }

    if(!object.availableNonSmoking()) 
{
      cout "there are no non smoking seats left words";
 do you want to try another flight or take a smoking seat actions
}
   if one of the types is not available above, then you should have assigned them a seat of the only available type (or given up and sent them away) and this part is now not valid. 

 else both types are available, so get their choice as you were doing, somewhat rewired:  
       
   do
    {
        cout << "please type 1 for smoking or please type 2 for non-smoking\n";
        cin >> answer;

      if (answer == 1 && object.availablesmoking()) 
       {
         cout << "you are in the smoking section and your seat number is" << object.reserveSmoking() << endl;
       }
      etc....   
    }
   while (answer != 1 && answer !=2);



of course you can do it other ways, like ask them what type they want, see if its there, if not ask if the other type is ok, same idea, different order of logic and output words.
Last edited on
1
2
3
4
5
Reservation::Reservation()
{
    seats = 10;
    plane[seats] = { 0 }; 
}
out of bounds access.
that's not the way to initialise an array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int Reservation::reserveSmoking(int num)
{
    for (int i = 0; i < seats; i++) {
        cout << "Please select from one(0) to four(4): ";
        cin >> num;
        while (num <0 ||num >4) {
            cout << "Please select from one(0) to four(4): ";
            cin >> num;
        }
 reduceAvailableSeats();
        if (plane[i] == i) { //¿what?
            return num;
        }
        else {
            i++; //¿how many times do you want to increase i?
        }
    }
    return -1;
}
you ignore the parameter
try to do all input/output operations on main()
¿where do you ever set `plane' values?
same errors in nonSmoking, besides a bad copy-paste

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int Reservation::reserveRandomNonSmoking()
{
    for (int i = 0; i < seats; i++) {
        srand(time(0));
        seats = 6 + rand() % 5;
        if (plane[i] == i) {
            return seats;
        }
        else {
            i = i + 1;
        }
    }
    return 0;
}
same errors as before.
¿how many meanings does `seats' have?
you use it as the limit on the loop and also to get a random number
srand() should be called just one time in your entire program

1
2
3
4
void Reservation::reduceAvailableSeats()
{  while(seats<=0)
    seats--;
}
get pen an paper and simulate that function
try with seats equal to 5, 1, and 0


Also, ¿don't you realise that your code for smoking is almost the same as for nonsmoking?
¿doesn't that tingle your vinyl antennas?
Topic archived. No new replies allowed.