Unsure how to complete Array-loop program

Hello. I've been having problems finishing up what should be a relatively simple program for the past couple days, and would love a push in the right direction/pseudocode.

What the program should do, is run a loop that asks the user to book an "airplane seat" by inputting any number from 0-9. Once a seat is "booked", the loop should ask the user to book another seat. If a seat is booked again, an error message should display, and then continue asking. The loop should be able to be exited at anytime by inputting -1.

I've been stuck in a few places so far. I'm unsure how to get the loop to recognize a "-1" input as a value other than 0-9; inputting it currently brings up the exit message, but the loop/loop message continues right after.

I'm also stumped on how to get the program to mark inputted values as "taken". I came across range-based for statements, and thought they looked like what I needed; though I can't quite figure out how to modify the array values using it, and then get the modified array values back into the loop.

Some pseudocode to push me in the right direction would be appreciated; I've been trying this for too long. Apologies if my (unfinished) code is butchered; I've gone through multiple revisions trying too many different approaches. Not even sure if I'm referencing the array properly.


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

int main()
{
	int seats;
	array<int, 10> n = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

	cout << "Welcome to Engineering Airlines!\n\n";

	for (int seats : n)
	{
		cout << "Pick a seat number between 0-9 <or -1 to exit>: ";
		cin >> seats;

		if (seats >= 0 || seats <= 10)
			cout << "Your seat has been successfully booked.\n\n";
		else if (seats == -1)
			cout << "Thank you for choosing Engineering Airlines!";
	}

	for (int &nRef : n)
	{
	nRef // ???
	}

}
Last edited on
There are several problems to solve:
1) How to store that a seat has been taken?
2) How to repeatedly ask for seats?
3) How to stop asking for seats?
4) How to handle when a user asks for an already taken seat?
5) How to handle when a user asks for a non-existent seat?

For #1, an array is good to use here because you have many seats. Remember that there are really two pieces to an array. You have the index, and you have the value at that index. Since arrays start at index 0, and in your plane you have seats 0-9, those seats look an awful lot like an array index! If we treat the plane seats like array indexes then we can treat the value at that index as whether or not the seat is taken.

For #2, whenever you need to do something repeatedly, that usually means you need a loop. Otherwise you'll end up repeating yourself a lot or venturing into advanced concepts like recursion.

For #3, we know that we want to stop asking for seats once a user enters -1. Since we know we want a loop (from #2), this means we have to get out of the loop once the user enters -1. There are two ways to get out of a loop. The first is to use a break statement somewhere within the loop:
1
2
3
4
while (1) {
    if (condition) break;
    // Other code follows
}

and the second is instruct the loop to repeat only when a statement is true, like:
1
2
3
while (condition) {
    // Do stuff
}

Depending on how you think about this problem, you might prefer one option or the other. For me, I'm a fan of the first option. But, you can solve this problem with either style.

For #4, after we have the user's input we can use it to do a lookup on our array. If the user wants to take seat 3, then we look at the value at index 3 in our array. Let's say we decide that a value of 0 means the seat isn't taken, but a value of 1 means that it is. Once we extract the value, based on what it is we'll decide to either output an error message (and loop again) or mark the seat as taken, print a message, and also loop again.

For #5, you just have to make sure that what the user input is between -1 and 9. For example, if they enter 29, you can't just use that value with your array. Since index 29 doesn't exist in your array, it's not proper to look at the value at that index!
Awesome, thank you!

Took a while to get to because of other classwork, but I figured it out. Thank you so much for suggesting the method in #4; I wasn't sure how to get the program to store taken seats before.

Everything was a great help; thanks again!
Topic archived. No new replies allowed.