Seating arrangements in my program

Im having the a problem with my seating arrangments with in the class ranges.
In first class, it is only mean to book 1-5, if i enter 6, it will come up with the message:
"The seating arrangements for this class is 1-5, please pick again."
Your seat number is : 6

it shouldn't be doing this, it should just tell the user that the seating arrangment is for this class is 1-5, and thats it and not book the seat. But the strange thing is, it wont book >=7 it will deny 7+.

The only issue remaining is now:
Also, in second class, im having a similar problem only im ive stopped it from booking 5=<, but im having the same issue with first class on the second class when booking 11+. Any ideas as to why this is happening?

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
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
int f=0,s=9, ans;
bool firstclass [5] = {0,0,0,0,0};
bool secondclass [5]= {};
int fspace = 5;
int positionint = 0 ;
int sspace= 8;
char check, con, position;

using namespace std;

//Function declarations
void first_class();
void second_class();

//Variable declarations
short int answer = 0;
char cont = 'y'; //So we do not quit immediately! The user can make this to n (no) later on. 

int main()
{
    if(cont == 'n' || cont == 'N') //If the user chooses not to continue after booking a seat, then we will quit. 
    {
        cout << "\t Thankyou for booking with SHARMA AIRLINE reservationss" << endl;
        return 0;
    }
    cout << "\t---Welcome to the SHARMA AIRLINE booking system---" << endl;
    cout << "\t---Which class would you like?---" << endl;
	cout << "\t---(1) For First-Class---" << endl;
	cout << "\t---(2) For Economy-Class---" << endl;
    cin >> answer;
    if(answer == 1)
    {
        first_class(); //Function call.
    }
    else if(answer == 2)
    {
        second_class(); //Function call.
    }
    else
    {
        return 0; //Any other number quits. 
    }
}

void first_class()
{
	cout<<"\tPlease choose a seat from 1-5"<< endl;
            cin >> positionint;
            f = positionint - 1;
            if (positionint > 5)
            {
                cout << "The seating arrangements for this class is 1-5, please pick again." << endl;
            }
            if (positionint <=5)
            {
                if (firstclass [f] == 0)//seat is free
                {
                    firstclass [f] = 1;
                    cout << "Your seat number is: " << positionint << endl;
                }
				else
                {
                    cout << "This seat is already booked" << endl;
				}
            }
    cout << "Would you like to book another seat (y/n)? " << endl;
    cin >> cont;
	if(cont == 'y' || cont == 'Y')
            {
                main();
            }
}

void second_class()
{
  
	cout << "\t--Welcome to Second class--" << endl;
				cout<<"\tPlease choose a seat from 6-10"<< endl;
				s = positionint - 1;
				cin >> positionint;
				
					if (positionint <= 5 || positionint >= 11 )
			{
				cout << "The seating arrangements for this class is 6-10, please pick again." << endl;
			}
				if (positionint >= 6)
				{
				if (secondclass [s] == 0) 
				{
				secondclass [s] = 1;
				cout << "Your seat number is: " << positionint << endl;
				}
			else
			{
				cout << "This seat is already booked" << endl;
			}
				}

    cout << "Would you like to book another seat (y/n)? " << endl;
    cin >> cont;
		if(cont == 'y' || cont == 'Y')
            {
                main();
            }
}


EDIT: the code has been updated.
Last edited on
closed account (o1vk4iN6)
Arrays start at 0, you're asking them to input a number from 1-5 right? The array only goes from 0 to 4. Not only that but you are adding one to the number they input.

I input 1, f == 2.

You probably want to subtract one instead, like so:

f = positionint - 1;

Also you shouldn't be calling main from first_class(). You are essentially creating a recursive function, so you are using up more memory on the stack that you need to.

do something more like this:

1
2
3
4
5
6
7
8
9
10

int main()
{
    char input;

    do {
        first_class();
    } while( cin >> input && input == 'y' );
}
You should try running an example in your head, for all cases. In this code, that means the user either enters [1,5] or >5. It's best if you can find some type of 'most useful value', which is most likely to break the code if there is an error. Since part of your trouble is mapping a seat id to an array index, it makes sense to check the outer values (0, 1, 5, 6). I'll check two, because your mistake is obvious.

If positionint = 5 in first class, then 'f' becomes 6. Since you're trying to adjust the seating id to the array index, it should be f = positionint -1;, because you're mapping [1,5] to [0,4]. Then, you should check for positionint > 5 (seat id) or f > 4 (array index). Since those are equivalent (if positionint > 5, then f > 4), you can drop the second if and replace it with an 'else'.

If positionint = 0, then 'f' should become -1 (once you correct your code on line 52, because array index = seat id - 1). Currently, you only check for too high values, but input mistakes can go both ways. Since 0 <= 5 (or -1 <= 4), it'll pass the check, but still cause errors because you're writing to array[-1].


Lastly, this might simply be because your code is unfinished, but currently there is no 'book another seat'. The question will appear, but the answer is ignored. You should put a loop in your main!
Ok, ive changed line 52 to f= positionint -1; but im back to square one, im able to book seats 1-6 when it shouldn't be doing that. Its almost as if the array is 6 spaces long. I understand a little of what both of you had said, i have only been doing this for 8 weeks so im a bit of a beginner.

edit: ok i changed line 57 to from if (f<=5) to if (positionint <=5) this seemed to correct my problem. I have applied this to the second class, but i am still able to book past 10 seats, under 6 has been solved.
Last edited on
Get rid of the second ifs (line 57 & line 90) and replace them by 'else'. Lines 53 and 86 check if the input is valid, so line 57 and 90 don't have to.

Now, line 90 checks for s >= 6, which is wrong. s has to be [5, 9]. Line 53 ensures that s is [5, 9], so checking again just causes confusion and errors.
When i put the else statements in the lines you've asked me to, it takes me back to the same problem i was having earlier with the seating arrangments. Maybe im doing it wrong or something
It shouldn't. Think about it:
1
2
3
4
5
6
if (positionint outside of bounds) {
    display error message;
}
else {
    book seat;
}
By the way, what's the point of line 84?
There isn't any point, that for statement breaks my program, ill update my program because that one is kind of out dated. I will try your "else" solution now.

edit update: the change from if to else statements put me back 2-3 steps. its booking beyond the seating arrangments.
Last edited on
The only issue im having now is second class is booking above seat 10. That is it.
You're not doing what I suggested. Lines 89 and 57 should be else statements...
Topic archived. No new replies allowed.