patterncMinor
Reserving seats in a movie theater
Viewed 0 times
theaterreservingmovieseats
Problem
I'm writing a C program to reserve seats in a movie theater. I am validating the selection of seats:
This code works fine. However, how can I refactor the code for this loop? In my opinion, using
* By the way, you may want to see the full code —originally, in Spanish.
const int TOTAL_ROWS = 10; // 1 to 26 -> 'A' to 'Z'
const int SEATS_PER_ROW = 5; // 1 to 9
int seats[TOTAL_ROWS][SEATS_PER_ROW]; // reserved seats, reserved seat == 1
char codes[TOTAL_SEATS][2]; // selected seats, e.g. A1, ..., Z9
...
for (i = 0; i = 'A' &&
codes[i][0] = '1' &&
codes[i][1] <= '1' + SEATS_PER_ROW - 1) {
if (seats[codes[i][0] - 'A'][codes[i][1] - '1'] == 1) {
printf("Reserved seat. Select other.\n");
i--;
}
for (j = 0; j < i; j++) {
if (codes[j][0] == codes[i][0] && codes[j][1] == codes[i][1]) {
printf("Repeated selection. Select other.\n");
i--;
break;
}
}
} else {
printf("Invalid code for seat.\n");
i--;
}
}This code works fine. However, how can I refactor the code for this loop? In my opinion, using
i-- does not look professional.* By the way, you may want to see the full code —originally, in Spanish.
Solution
I/O
You want to read 2 characters although you use
Either increase the buffer, change the format specifier or use an alternative method of reading from keyboard e.g.
You may also want to convert the characters to upper case just to be a bit more flexible towards the user (
structure
I think the code could do with some more functions to clarify the purpose.
E.g.
loop
What other suggested and as you mentioned already decrementing i in the for loop may be a bit confusing for the reader (well it was for me at least), having a do..while loop feels more natural here where the while condition is false until user enters correct code (or changes his mind and gives up).
You want to read 2 characters although you use
scanf("%s", codes[i]) this can have the effect you are overwriting the end of the buffer when you read scanf("%s", codes[required_seats-1]); since there is always an \0 added to your two characters. Either increase the buffer, change the format specifier or use an alternative method of reading from keyboard e.g.
fgets() followed by sscanf() or simply read the characters one by one using fgetc(). The latter method has the advantage that you could prevent the user from entering invalid characters while he types.You may also want to convert the characters to upper case just to be a bit more flexible towards the user (
toupper())structure
I think the code could do with some more functions to clarify the purpose.
E.g.
int isCodeValid(char* code)
{
return code[0] >= 'A' && code[0] = '1' && code[1] <= '1' + SEATS_PER_ROW - 1;
}
int isRepeatedSelection(char* codes);
...loop
What other suggested and as you mentioned already decrementing i in the for loop may be a bit confusing for the reader (well it was for me at least), having a do..while loop feels more natural here where the while condition is false until user enters correct code (or changes his mind and gives up).
Code Snippets
int isCodeValid(char* code)
{
return code[0] >= 'A' && code[0] <= 'A' + TOTAL_ROWS - 1 &&
code[1] >= '1' && code[1] <= '1' + SEATS_PER_ROW - 1;
}
int isRepeatedSelection(char* codes);
...Context
StackExchange Code Review Q#84092, answer score: 6
Revisions (0)
No revisions yet.