please help with this loop

i wrote this code and it works but it only asks the user to pick a seat once and then it assigns the seat and the code ends.i want it to ask over again untill my seats are filled and i can implement the top code.
this is the 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 main(){
    Reservation object;
    if (!object.availableSmoking() && !object.availableNonSmoking()) {
        cout << "the plane is full";
        cout << "the next flight leaves in 3 hours";
    }

    else {
        if (!object.availableSmoking()) {

            cout << "there are no smoking seats available";
            cout << "would you like to be seated in the non smoking section?";
        }
        if (!object.availableNonSmoking()) {
            cout << "there are no non smoking seats available";
            cout << "would you like to be seated in the smoking section";
        }
    }

    int answer;

    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(answer) << endl;
        }

        if (answer == 2 && object.availableNonSmoking()) {
            cout << "you are in the non-smoking section and your seat number is" << object.reserveNonSmoking(answer) << endl;
        }
    } while (answer != 1 && answer != 2);
}
Last edited on
> while (answer != 1 && answer != 2)
If you type in 1, is this expression true or false?
Turn your main() into a different function. Create a new main() that calls your function. The only real trick here was passing the Reserveation object as a parameter.
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
int reserveASeat(Reservation &object){
    if (!object.availableSmoking() && !object.availableNonSmoking()) {
        cout << "the plane is full";
        cout << "the next flight leaves in 3 hours";
    }

    else {
        if (!object.availableSmoking()) {

            cout << "there are no smoking seats available";
            cout << "would you like to be seated in the non smoking section?";
        }
        if (!object.availableNonSmoking()) {
            cout << "there are no non smoking seats available";
            cout << "would you like to be seated in the smoking section";
        }
    }

    int answer;

    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(answer) << endl;
        }

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

int main()
{
    Reservation object;
    do {
        reserveASeat(object);
    } while (object.availableSmoking() || object.availableNonSmoking());
}

Topic archived. No new replies allowed.