Require some Array assistance

I need some help with my functions my program. My program is supposed to prompt the user and to ask them if they want a first class or coach seat and then mark a 1 in the array. Sets 1-5 are first class and 6-10 are coach.

I have the asking for choice part down (I think) but I'm stuck on recording the 1 to the array for whichever seat they choose. The program also needs to check if there is any room left in that seating area and if there's not then it has to ask if the person would like a seat in the other area.

Here's my code so far.....
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
#include <iostream>
using namespace std;


SeatingMenu (int);
void FirstClassSeating (int);  //Not sure if these should be void but I don't
void CoachSeating (int);       //think that they should be if I'm passing data
void BoardingPass ();          //out of them.
void InputResponse (string);
int main ()
{
    const int SIZE = 9;
    int seats [SIZE];
    int choice;
    int n;

    cout << "Would you like to fly first class or coach?" << endl;
    cout << "1. First Class" << endl << "2. Coach" << endl;
    cout << "Enter your choice:" << endl;
    cin >> choice;
    
    SeatingMenu(choice);
	FirstClassSeating (choice);


    return 0;
}


SeatingMenu(int choice)
{
    
       
        cout << "Choice: " << choice << endl; 
    if (choice == 1)
	{
		cout << "You have chosen a first class seat" << endl;
	}
    if (choice == 2) 
	{
		cout << "You have chosen a coach seat" << endl;
	}

	return choice;
}

FirstClassSeating(int choice, int n, int seats)
{
	cout << "Here I am" << endl;

	for (n = 0; n < SIZE; n++)
	{
		seats[n] = n;
	}
	return choice;
}



I'm new at arrays and functions so any help or suggestions are greatly appreciated. Thanks guys.
Remove return choice; from the functions. I'm not sure what FirstClassSeating is supposed to do, but seats should be int seats[].
FirstClassSeating is supposed to determine if there is available seat, so check if spaces 0-4 are filled in the array then if there are available seats in first class assign one to the user by marking the space in the array with a one. Coach seating is the same.
Well, you should define an int i, then loop up to n. Pass SIZE in as n. Each time the loop uh, loops, check whether seat i is available. If it is, put the user in that seat, and return. If you get past the end of the loop, say "no first class seats are left".
When I book a seat it doesn't just book one but it books them all. If I choose a coach seat here's what happens...
Would you like to fly first class or coach?
1. First Class
2. Coach
Enter your choice:
2
Choice: 2
You have chosen a coach seat
You have been booked a coach seat
You have been booked a coach seat
You have been booked a coach seat
You have been booked a coach seat
You have been booked a coach seat
Press any key to continue . . .


I'm also not quite sure how to determine if there are no seats left.


Here's what I've got now.

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
#include <iostream>
using namespace std;


void SeatingMenu (int);
void FirstClassSeating (int [], int);//Book seats for first class, write 1 to array (seats 1-5)
void CoachSeating (int[], int);    //Book seats for coach, write 1 to array (seats 6-10)
void BoardingPass ();                //Print boarding pass
void InputResponse (string);
int main ()
{
    const int SIZE = 10;
	int seats [SIZE] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int choice;
    int i;

    cout << "Would you like to fly first class or coach?" << endl;
    cout << "1. First Class" << endl << "2. Coach" << endl;
    cout << "Enter your choice:" << endl;
    cin >> choice;
    
    SeatingMenu(choice);
	if (choice == 1)
	FirstClassSeating (seats, i);
	if (choice == 2)
	CoachSeating (seats, i);


    return 0;
}


void SeatingMenu(int choice)
{
    
       
        cout << "Choice: " << choice << endl; 
    if (choice == 1)
	{
		cout << "You have chosen a first class seat" << endl;
	}
    if (choice == 2) 
	{
		cout << "You have chosen a coach seat" << endl;
	}

	 
}

void FirstClassSeating(int seats [], int i)
{
	

	for (i = 0; i < 6; i++)
	{
		seats[i] = 1;
		cout << "You have been booked a first class seat" << endl;
	}
	
}

void CoachSeating(int seats [], int i)
{
	for (i = 6; i < 11; i++)
	{
		seats[i] = 1;
		cout << "You have been booked a coach seat" << endl;
	}
}
Remove the for loops entirely, then while loop until you find an empty one. return if you can't. i is not used in main so, just make it local.
1
2
3
4
5
6
7
void FirstClassSeating(int seats []){
        int i=0;
	while(seats[i])// go through the seats
                if(i++==6){return;}//give up if you reach the sixth seat
        seats[i] = 1;
	cout << "You have been booked a first class seat" << endl;
}

EDIT: the way you had it, it would book every seat.
Last edited on
Thanks rocketboy, that get's rid of the problem of it assigning all of the seats as booked. But how would I go about telling the user that the seating section is full and ask them if they would like a seat in the other section? I've got some ideas but I don't know for sure.
I need to figure out how to ask the person if they want to book a coach seat if first class is full and the other way around but for some reason I can't get fill up first class no matter how many times I book it. Any suggestions?

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
#include <iostream>
using namespace std;


void SeatingMenu (int);
void FirstClassSeating (int [], int);
void CoachSeating (int[], int);
void BoardingPass (int, int);
void InputResponse (string);
int main ()
{
    const int SIZE = 10;
	int seats [SIZE] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int choice, anotherseat;
    int i;
	

    cout << "Would you like to fly first class or coach?" << endl;
    cout << "1. First Class" << endl << "2. Coach" << endl;
    cout << "Enter your choice:" << endl;
    cin >> choice;
    
    SeatingMenu(choice);
	if (choice == 1)
	FirstClassSeating (seats, i);
	while (i < 6)
	{
	cout << "Book another first class seat?" << endl;
	cout << "1 = Yes" << endl << "2 = No" << endl;
	cin >> anotherseat;
	if (anotherseat == 1)
		FirstClassSeating (seats, i);
	if (anotherseat == 2)
		BoardingPass (seats, i);
	if (i == 6)
		cout << "There are no more first class seats available" << endl;
	}

	
		
	if (choice == 2)
	CoachSeating (seats, i);
	

    return 0;
}


void SeatingMenu(int choice)
{
    
       
        cout << "Choice: " << choice << endl; 
    if (choice == 1)
	{
		cout << "You have chosen a first class seat" << endl;
		
	}
    if (choice == 2) 
	{
		cout << "You have chosen a coach seat" << endl;
	}

	 
}

void FirstClassSeating(int seats [], int i)
{
	int firstclass;
	seats[i] = 1;
	cout << "You have been booked a first class seat" << endl;

}

void CoachSeating(int seats [], int i)
{
        seats[i] = 1;
	cout << "You have been booked a coach seat" << endl;
}
When the user books a seat, increase i by one.
That's where I'm stuck. I put it in the FirstClassSeating function and if it's working it's not ever telling me that first class is full.
Easy fix: change this:
FirstClassSeating (seats, i);
to this:
FirstClassSeating (seats, i++);
Hard fix:
Pass i in by reference, and increment it inside the function. The reason you have to do that is that when you do this:
1
2
3
4
5
6
7
8
9
10
11
int f(int n){
    printf("n=%d\n",n);
    n++;
    printf("n=%d\n",n);
}
int main(){
    int n=0;
    printf("n=%d\n",n);
    f(n);
    printf("n=%d\n",n);
}

it will print:
n=0
n=0
n=1
n=0

When you pass a variable in normally, you make a copy. If you modify the varable you passed in, only the copy is affected.
Topic archived. No new replies allowed.