Seating arrangements

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+
. [solved, changed "f = positionint - 1;" to " f = positionint + 1;"

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=5, ans;
bool firstclass [5] = {0,0,0,0,0};
bool secondclass [10]= {0,0,0,0,0,0,0,0,0,0};
int fspace = 4;
int positionint = 0;
int sspace= 9;
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 >=6 )
            {
                cout << "The seating arrangements for this class is 1-5, please pick again." << endl;
            }
            if (f<=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;
				cin >> positionint;
				for (s = 0; s <=sspace; s++);
				s = positionint - 1;
					if (positionint >= 11 || positionint <= 5 )
			{
				cout << "The seating arrangements for this class is 6-10, please pick again." << endl;
			}
				if (s>=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();
            }
}
Last edited on
Here's how (i think) it should be done :) (i didn't quite understand all of it but i tried)

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
#include <iostream>
#include <stdlib.h>
#include <stdio.h>

int f=0,s=5, ans;
bool firstclass [5] = {0,0,0,0,0};
bool secondclass [10]= {0,0,0,0,0,0,0,0,0,0};
int fspace = 4;
int positionint = 0;
int sspace= 9;
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()
{
    do
    {
    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. 
        
    }
    while (cont == 'Y' or cont =='y');
    
    cout << "\t Thankyou for booking with SHARMA AIRLINE reservationss" << endl;
    return 0;

}

void first_class()
{
	cout<<"\tPlease choose a seat from 1-5"<< endl;
    cin >> positionint;
    while (positionint < 1 || positionint > 5)
    {
        cout << "The seating arrangements for this class is 1-5, please pick again." << endl;
        cin >> positionint;
    }
    if (f<=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;
}

void second_class()
{
    
	cout << "\t--Welcome to Second class--" << endl;
    cout<<"\tPlease choose a seat from 6-10"<< endl;
    cin >> positionint;
    for (s = 0; s <=sspace; s++);
        s = positionint - 1;
    
    while (positionint > 10 || positionint < 6 )
    {
        cout << "The seating arrangements for this class is 6-10, please pick again." << endl;
        
    }
    if (s>=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;
}


instead of
1
2
3
4
if (positionint >= 11 || positionint <= 5 )
{
      cout << "The seating arrangements for this class is 6-10, please pick again." << endl;
}


you should use while, to keep the user in a loop until he selects a valid value.

also
1
2
3
4
if(cont == 'y' || cont == 'Y')
            {
                main();
            }

is unnecessary because when the loop ends the user will always end up where he left in main();

i thought it would be more appropriate to use a do while loop in main() so that the program would run at least once before exiting.

Hope I was of some help to you :)
1
2
3
4
if(cont == 'y' || cont == 'Y')
            {
                main();
            }


This sends you to the START of main. It is definately necessary.
1
2
3
4
if(cont == 'y' || cont == 'Y')
            {
                main();
            }


This is a recursive call to main(). It is horrific. It does not send you to the start of main. It create a whole new main, on top of the original one, eating up another big chunk of stack, and enters that . And then again, and again, and again, and again, each time eating more stack and creating another complete stack frame with another complete copy of main().

We have loops to handle this sort of thing. Maniax demonstrates one such example in his code.
Thanks for that Moschops. :)

Why do you not even get warnings for recursive main calls? :S
The beauty and danger of C and C++; there is an implicit trust of the programmer :)
Topic archived. No new replies allowed.