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!