Help with "airline seats" array code

I have to make a program that goes like this: You create an airline pass by inputing name, choosing class, and then choosing seat number. The program will then repeat the method until it created 10 passes. No repeats of seats can be made or else it's wrong.

I'm supposed to use array for this, but I'm not the best when it comes to arrays. This is currently the code I have.

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
// airline.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int main()
{
	int c, s[10];
	
	string name;

	cout << "Welcome to Airline Reservations System!\n";
	cout << "Please enter your name: ";
	getline(cin,name);

	cout << "Choose a class: ";
	cin >> c;
	switch(c)
	{
	case 1:
		cout << "First class" << endl;
		cout << "Seats available are 1,2,3,4,5.\n";
		cout << "Pick a number: ";
		cin >> s;
		for(int i; i < 10; i++)
		if(s <= 5)
		{
			cout << "\n";
			cout << "--------------------------\n";
		cout << "Name: " << name << endl;
		cout << "Class: " << "First class" << endl;
		cout << "Seat no.: " << s << endl;
			cout << "--------------------------\n";
		}
		else 
			cout << "Wrong number!  No seat for you!\n";
		break;
	case 2: 
		cout << "Economic class\n";
		cout << "Seats available are 6,7,8,9,10.\n";
		cout << "Pick a number: ";
		cin >> s;
		if(s >= 6)
		{
			cout << "\n";
			cout << "--------------------------\n";	
		cout << "Name: " << name << endl;
		cout << "Class: " << "Economic class" << endl;
		cout << "Seat no.: " << s << endl;
			cout << "--------------------------\n";
		}
		else 
			cout << "Wrong number!  No seat for you!\n";
		break;
	}


	system("pause");
	return 0;
}

As you can see, I can only make one pass and it was done without arrays. I can repeat this same code over and over until there are ten, but that would take forever. Can I please inquire help on how to get it to work with arrays? I hope you understand what I mean...
Last edited on
if you are only required to check for repeated seats:
1
2
3
4
5
6
7
8
9
10
11
int i,j,s[10];
for (j=0; j<10; j++)
{
  // your code getting the name, class, etc... here
  do {
    cout << "Pick a seat: ";
    cin >> s[j];
    for (i=0; i<j; i++) if (s[j]==s[i]) {cout << "Seat taken. ";break;}
  } while (i!=j);
  // the rest of your code here...
}
I got it to work and it does warn me of repeats. However, the code will not end! It keeps on asking for seat input and goes all the way more than 10! Here's how it looks like:

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
// airline.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int main()
{
	int c, s[10];
	int i, j;
	string name;

	
	
	cout << "Welcome to Airline Reservations System!\n";
	cout << "Please enter your name: ";
	getline(cin,name);

	cout << "Choose a class: ";
	cin >> c;
	for (j=0; j<10; j++)
	switch(c)
	{
	case 1:
		cout << "First class" << endl;
		cout << "Seats available are 1,2,3,4,5.\n";
		do {
    cout << "Pick a seat: ";
    cin >> s[j];
    for (i=0; i<j; i++) if (s[j]==s[i]) {cout << "Seat taken. ";
	break;}
  } while (i!=j);
		if(s[j] <= 5)
		{

			cout << "\n";
			cout << "--------------------------\n";
		cout << "Name: " << name << endl;
		cout << "Class: " << "First class" << endl;
		cout << "Seat no.: " << s << endl;
			cout << "--------------------------\n";

		}
		else 
			cout << "Wrong number!  No seat for you!\n";
		break;
	case 2: 
		cout << "Economic class\n";
		cout << "Seats available are 6,7,8,9,10.\n";
		do {
		cout << "Pick a seat number: ";
		cin >> s[j];
		for (i=0; i<j; i++) if (s[j]==s[i]) {cout << "Seat taken. ";
	break;}
  } while (i!=j);
		if(s[j] >= 6)
		{
			cout << "\n";
			cout << "--------------------------\n";	
		cout << "Name: " << name << endl;
		cout << "Class: " << "Economic class" << endl;
		cout << "Seat no.: " << s[j] << endl;
			cout << "--------------------------\n";
		}
		else 
			cout << "Wrong number!  No seat for you!\n";
		break;
	}


	system("pause");
	return 0;
}
Last edited on
A possible solution for this would be to include a break option if you use seat 0...or you could tell it to break if you tell it to check for the name input was exit or quit....although you may have to split this up into functions. I hope this helps and isn't as confusing as it now sounds to me. If it is, I can post a potential solution for you.
Okay, I got it to work properly, so thanks.

However, it only asks for seats over and over. When an airline ticket is created, it must re-ask the name and re-choose another class again before asking a new seat that doesn't repeat with the old one.
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
// airline.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"			//Not used in this code- Can be deleted.
#include <string>
#include <iostream>
using namespace std;
int main()
{
	int c, s[10];
	int i, j;
	string name;

	
	
	cout << "Welcome to Airline Reservations System!\n";
	for (j=0; j<10; j++)
	{
		cout << "Please enter your name: ";
		getline(cin,name);
	
		cout << "Choose a class: ";
		cin >> c;
	
		switch(c)
		{
		case 1:
			cout << "First class" << endl;
			cout << "Seats available are 1,2,3,4,5.\n";
			do {
	    cout << "Pick a seat: ";
	    cin >> s[j];
	    for (i=0; i<j; i++) if (s[j]==s[i]) {cout << "Seat taken. ";
		break;}
	  } while (i!=j);
  	  if(s[j] <= 5)
		{

			cout << "\n";
			cout << "--------------------------\n";
		cout << "Name: " << name << endl;
		cout << "Class: " << "First class" << endl;
		cout << "Seat no.: " << s << endl;
			cout << "--------------------------\n";

		}
		else 
			cout << "Wrong number!  No seat for you!\n";
		break;
	case 2: 
		cout << "Economic class\n";
		cout << "Seats available are 6,7,8,9,10.\n";
		do {
		cout << "Pick a seat number: ";
		cin >> s[j];
		for (i=0; i<j; i++) if (s[j]==s[i]) {cout << "Seat taken. ";
	break;}	
  } while (i!=j);
		if(s[j] >= 6)
		{
			cout << "\n";
			cout << "--------------------------\n";	
		cout << "Name: " << name << endl;
		cout << "Class: " << "Economic class" << endl;
		cout << "Seat no.: " << s[j] << endl;
			cout << "--------------------------\n";
		}
		else 
			cout << "Wrong number!  No seat for you!\n";
		break;
		default:
				break;
	}
}


	system("pause");
	return 0;
}


Here is your code slightly modified so it will ask for the name as well. I didn't verify everything worked, though I did add a default statement for choosing the class, so if you enter a non-valid class, it'll break out of the loop for you. Hope that this is what you had in mind. If not, lemme know and we'll see if we can't figure it out together.

I also added a comment, depending on whether that one include line is necessary for your "outside" program, or not. Not used in this coding, however.
~Maingeek.
Last edited on
Topic archived. No new replies allowed.