C++ seating arrangements

Hi guys, im making an airline reservation system where a user is able to choose a there own seat. But there are two classes, first class and second class. First class can hold seats from 1-5, second class is 6-10.

If a user picks seat number 6 in first class, i want it to tell the user there is an error and that the seating arrangements is 1-5.

I have written the code so far and at the moment, if a user picks a seat higher than 6, it will display the error message but it will also display the message saying "your seat number is; "

I want it to completely say no, you cannot pick this seat, im a beginner programmer and this is an assignment for work, this is the remaining part of the code so once i get this, the rest of my code is simple enough. Heres my 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
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
118
119
120
#include<iostream>
using namespace std;
int f=1,s=6,ans, mainmenu;
bool firstclass [5]= {0};
bool secondclass [5]= {0};
int fspace = 5;
char check,con, position;
int main()
{
mainmenu:
	cout <<"\t--Hello, welcome to AIRLINE RESERVATION--" << endl;
	cout <<"--Please select the following:" << endl;
	cout <<"--[1] For a First-class seat" << endl;
	cout <<"--[2] For a Second-class seat" << endl;
	cout <<"--[3] Quit program.\n\n" << endl;
	do
	{
		cout<<"\tWhich class would you like?" << endl;
		cin>>ans;
		{
			if (ans == 1)
				goto firstClass;
		}
		{
			if (ans == 2)
				goto secondClass;
		}
		if (ans == 1 && f<=5)
		{
firstClass:
			cout << "\t--Welcome to first class--" << endl;
			cout<<"\tPlease choose a seat"<< endl;
			cin >> position;
			f = position - 1;
			if (firstclass [f] == 0) 
			{
				firstclass [f] = 1;
				cout << "Your seat number is: " << position << endl;
			}
			else
			{
				cout << "This seat is already booked" << endl;
			}
			if (f >= fspace)
			{
				cout << "The seating arrangements for this class is 1-5, please pick again." << endl;
			}
			cout<<"\t--Would You Like To Continue? (y/n)--\n";
			cin>>check;
			{
				if (check == 'n')
					goto mainmenu;
			}
		}
		else if (ans == 1 && f>5)
		{
			cout<< "\tUnfortunately, the First class is now full." << endl;
			cout<<"\tDo you want a seat in the Second classs? (y/n)\n";
			cin>>con;
			if ((con == 'Y' || con =='y') && s<=10)
			{
secondClass:
				cout<<"Your seat number is "<<s<<".\n";
				s++;
				cout<<"Do you want to continue(y/n)\n";
				cin>>check;
			}
			else if ((con == 'Y' || con =='y') && s>10)
			{
				cout<<"Sorry,all seats are taken!!\n\n";
				cout<<"The next flight leaves in 3 hours;)!\n";
				return 0;
			}
			else if (con != 'y' || con != 'Y')
			{
				cout <<"The next flight leaves in 3 hours!\n";
				return 0;
			}
		}
		if (ans == 2 && s<=10)
		{
			cout<<"Your seat number is "<<s<<".\n\n";
			s++;
			cout<<"Do you want to continue? (y/n)\n";
			cin>>check;
		}
		else if (ans == 2 && s>10)
		{
			cout<<"\t\tSorry, but the second class is now full!\n";
			cout<<"\tWould you like a seat in first class?(y/n)\n";
			cin>>con;
			if ((con == 'Y' || con =='y') && f<=5)
			{
				cout<<"Your seat number is "<<f<<".\n";
				cout<<"Do you want to continue?(y/n)\n";
				cin>>check;
			}
			else if ((con == 'Y' || con =='y') && f>5)
			{
				cout<<"Soryy,all seats are taken!!\n\n";
				cout<<"The next flight leaves in 3 hours!\n";
				return 0;
			}
			else if (con != 'y' || con != 'Y')
			{
				cout<<"The next flight leaves in 3 hours!\n";
				return 0;
			}
		}
		else if (ans>3)
		{
			cout<<"Incorrect Input!!\n\n\n";
			cout<<"\n";
			cout<<"\n";
			return main();
		}
	}
	while (check == 'y' || check == 'Y');
	return 0;
}
Don't let them pick it in the first place:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
		{
firstClass:
			cout << "\t--Welcome to first class--" << endl;
                        position = 20; //Just so that position does not pass the while loop test first time. 
                        while(position < 0 || position > 5) //Then we keep asking until they pick a valid answer. 
                        {
                            cout<<"\tPlease choose a seat"<< endl;
			    cin >> position;
                            if(position > 5 || position < 0) //Tell them no if they choose an invalid answer.
                            {
                                 cout << "The seating arrangements for this class is 1-5, please pick again." << endl;
                            }
                        }
			f = position - 1;


You could use a do-while loop here, or a for loop also. I just like straight up while loops personally.
Last edited on
Hi thanks for the reply, and sorry for the late reply, erm with this ive added your part in, but it doesnt seem to work the way i want it, its constantly asking for a seat and then sayin ""The seating arrangements for this class is 1-5, please pick again."

maybe i done it wrong, heres my 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int f=0,s=6,ans, mainmenu;
bool firstclass [5]= {};
bool secondclass [10]= {};
int fspace = 4;
int sspace = 6;
char check,con, position;
int main()
{
mainmenu:
	cout <<"\t--Hello, welcome to SHARMA AIRLINE RESERVATION--" << endl;
	cout <<"--Please select the following:" << endl;
	cout <<"--[1] For a First-class seat" << endl;
	cout <<"--[2] For a Second-class seat" << endl;
	cout <<"--[3] Quit program.\n\n" << endl;
	do
	{
		cout<<"\tWhich class would you like?" << endl;
		cin>>ans;
		{
			if (ans == 1)
				goto firstClass;
		}
		{
			if (ans == 2)
				goto secondClass;
		}
		//if (ans == 1 && f<=5)
		if (ans ==1)
		{
firstClass:
			cout << "\t--Welcome to first class--" << endl;
			position =20;
			while(position < 0 || position > 5)
			{
				cout <<"\tplease choose a seat"<< endl;
				cin >> position;
				if(position > 5 || position << 0)
				{
					cout << "THe seating arrangesments is 1-5 "<< endl;
				}
			}
			f = position - 1;
			if (f<=5)
			{
				if (firstclass [f] == 0)
				{
				firstclass [f] = 1;
				cout << "Your seat number is: " << position << endl;
				}
			else
			{
				cout << "This seat is already booked" << endl;
			}
			}
			else if (f > fspace)
			{
				cout << "The seating arrangements for this class is 1-5, please pick again." << endl;
			}
	
			cout<<"\t--Would You Like To Continue? (y/n)--\n";
			cin>>check;
			{
				if (check == 'n')
					goto mainmenu;
			}
		}
		else if (ans == 1 && f>5)
		{
			cout<< "\tUnfortunately, the First class is now full." << endl;
			cout<<"\tDo you want a seat in the Second classs? (y/n)\n";
			cin>>con;
			if ((con == 'Y' || con =='y') && s<=10)
			{
secondClass:
				cout << "\t--Welcome to Second class--" << endl;
				cout<<"\tPlease choose a seat from 6-10"<< endl;
				cin >> position;
				//for (s = 0; s <=sspace; s++)
				s = position - 48;
				if (s<10)
				{
				if (secondclass [s] == 0) 
				{
				secondclass [s] = 1;
				cout << "Your seat number is: " << position << endl;
				
				}
			else
			{
				cout << "This seat is already booked" << endl;
			}
				}
			else if (s < sspace)
			{
				cout << "The seating arrangements for this class is 6-5, please pick again." << endl;
			}
			cout<<"\t--Would You Like To Continue? (y/n)--\n";
			cin>>check;
			}
			else if ((con == 'Y' || con =='y') && s>10)
			{
				cout<<"Sorry,all seats are taken!!\n\n";
				cout<<"The next flight leaves in 3 hours;)!\n";
				return 0;
			}
			else if (con != 'y' || con != 'Y')
			{
				cout <<"The next flight leaves in 3 hours!\n";
				return 0;
			}
		}
		else if (ans == 2 && s>10)
		{
			cout<<"\t\tSorry, but the second class is now full!\n";
			cout<<"\tWould you like a seat in first class?(y/n)\n";
			cin>>con;
			if ((con == 'Y' || con =='y') && f<=5)
			{
				goto firstClass;
				}
			if ((con == 'Y' || con =='y') && f>5)
			{
				cout<<"Soryy,all seats are taken!!\n\n";
				cout<<"\tWould you like a seat in secondclass class?(y/n)\n";
				cin >> con;
				if ((con == 'Y' || con =='y') && s>=6)
				{
					goto secondClass;
				}
				cout<<"The next flight leaves in 3 hours!\n";
				return 0;
			}
			else if (con != 'y' || con != 'Y')
			{
				cout<<"The next flight leaves in 3 hours!\n";
				return 0;
			}
		}
		else if (ans>3)
		{
			cout<<"Incorrect Input!!\n\n\n";
			cout<<"\n";
			cout<<"\n";
			return main();
		}
	}
	while (check == 'y' || check == 'Y');
	return 0;
}


ignore the second class part, i want the first class part to work first. Thankyou.
Last edited on
1
2
3
4
5
6
7
8
9
10
position =20;
			while(position < 0 || position > 5)
			{
				cout <<"\tplease choose a seat"<< endl;
				cin >> position;
				if(position > 5 || position << 0)
				{
					cout << "THe seating arrangesments is 1-5 "<< endl;
				}
			}


Should look like this:

1
2
3
4
5
6
7
8
9
10
position =20;
			while(position < 0 || position > 5)
			{
				cout <<"\tplease choose a seat"<< endl;
				cin >> position;
				if(position > 5 || position < 0)
				{
					cout << "The seating arrangements is 1-5 "<< endl;
				}
			}
Still getting the result i mentioned before, just a constant loop
I forgot that you are using a char for the number input. Maybe it is not a good idea to keep re-using this char to put numbers in and instead to declare a seperate variable for this part. It's not like another int is taking up any memory in real terms.

You can solve it like this (and keep your code the same pretty much):
1
2
3
4
5
6
7
8
9
10
position = 33; //This is actually '!'
			while(position < 49 || position > 53)
			{
				cout <<"\tplease choose a seat"<< endl;
				cin >> position;
				if(position > 53 || position < 49)
				{
					cout << "The seating arrangements is 1-5 "<< endl;
				}
			}


Although, I would suggest in future just avoiding type conversions where they are not needed.
1
2
int pos = 0; //Use this for integer stuff
char position; //And this for character stuff. 


It will take up only a few bytes of memory.
Topic archived. No new replies allowed.