You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言二维数组座位预约异常:修改后座位状态不更新求助

Fixing Your Seat Reservation System Issues

Hey there! I see you're having trouble getting your seat reservation system to update seat values correctly, plus there's a printing glitch. Let's break down each problem and fix them step by step.

Key Issues in Your Code

1. Incorrect Array Passing to researve Function

In your main function, you called researve(s[3][5]); — this is passing a single, out-of-bounds element of your 2D array instead of the array itself. 2D arrays in C are passed as pointers, so you need to pass the array name directly: researve(s);.

2. print_seats Can't Access the Seat Array

Your print_seats function has no parameters, so it can't see the s array defined in main. We need to pass the seat array to this function so it can print the current state of seats.

3. Wrong Return Statement in researve

The line return s[3][5]; accesses an element outside your array's bounds (your array is 3 rows × 5 columns, indices range from 0-2 for rows and 0-4 for columns). Since we modify the array directly via pointer, this function doesn't need to return anything — we can change its return type to void.

4. Misplaced Labels & Incorrect Error Message

The succed : and fail : labels are unused here, and your else branch prints a success message even when the seat is already taken. That's confusing! We'll fix the error message to reflect the actual issue.

5. No Input Validation

Right now, if a user enters a row/column outside 0-2 or 0-4, your code will access memory outside the array, causing undefined behavior. We'll add a check for valid input.

Corrected Full Code

#include <stdio.h>

// Pass the seat array as a parameter to access it
void print_seats(int s[3][5]) {
    int i, j;
    printf(" |0 1 2 3 4\n");
    printf("---------------\n");
    for (i = 0; i < 3; i++) {
        printf("%d|", i);
        for (j = 0; j < 5; j++) {
            printf("%2d ", s[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

// Change return type to void since we don't need to return a value
void reseve(int s[3][5]) {
    int row = 0, col = 0;
    printf("선택된 메뉴=예약하기\n\n");
    printf("예약을 원하는 자리는?(행 열) :");
    scanf("%d %d", &row, &col);

    // Validate input to avoid out-of-bounds access
    if (row < 0 || row >= 3 || col < 0 || col >=5) {
        printf("잘못된 자리 번호입니다. 행은 0-2, 열은 0-4 사이로 입력하세요.\n\n");
        return;
    }

    if (s[row][col] == 0) {
        printf("예약이 완료되었습니다\n\n");
        s[row][col] = 1;
    } else {
        printf("해당 자리는 이미 예약되어 있습니다.\n\n");
    }
}

int main(void) {
    int s[3][5] = {{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}};
    
    printf("선택된 메뉴=좌석 확인하기\n\n");
    print_seats(s); // Pass array to print current seats
    reseve(s); // Pass array correctly to modify seats
    print_seats(s); // Print updated seat state

    return 0;
}

How This Fixes Your Problem

  • Seat reservations now modify the original array in main because we pass the array pointer correctly.
  • print_seats can now access and print the latest state of seats.
  • Input validation prevents crashes from invalid row/column entries.
  • Error messages are accurate, so users know when a seat is already taken.

内容的提问来源于stack exchange,提问作者Eby

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 17:47:36