C段错误-哪里/什么?

时间:2019-04-04 00:15:46

标签: c

我有这个程序可以处理为乘客分配座位的问题。

我无法找出导致我的段错误的原因。我正在使用一系列结构,这可能就是问题所在。

我认为取消引用某些struct成员可能是一个问题,但我找不到位置。

这是我的代码:

struct seat
{
  // Max Name of 32 chars
  char name[32];

  // Seat Number
  int sNum;

  // Seat taken = 1, open = 0
  int taken;
};

// Function headers
void seat(struct seat plane[][10]);
void mani(struct seat plane[][10]);
void pass(int seat, char name[], int class);

int main()
{
// My airline plane is 6 seats per row, 10 rows
// Row 1/2 are First Class
// Row 3/4 are Business Class

  // Init counter variables to keep track of the number
  // of First Class/Business seats already taken
  // also the user input var
  int input, ticketin, class1 = 0, class2 = 0, class3=0, sNum, i;
  char namein[32];

  // Vars for pass function
  char passname[32];
  int passclass;
  int passseat;

  // Init a 2d array 6 colums 10 rows
  struct seat plane[6][10];

  for (i=0; i<sizeof(plane); i++)
  {
    plane[i]->sNum = i+1;
    plane[i]->taken = 0;
  }

  // Begin user input loop
  // Menu with 3 options:
  // Display the seating chart, indicating taken seats
  // Display the manifest
  // Display a boarding pass - seat number, name, class
  do
  {
    do
    {
      // Prompt user for ticket selection
      printf("Please type 1 for \"First Class\"\n");
      printf("Please type 2 for \"Business Class\"\n");
      printf("Please type 3 for \"Economy Class\"\n");

      scanf(" %d", &ticketin);

      // Check for valid input
      if (ticketin == 1)
      {
        // Check to make sure first class is not full
        if (class1 < 12)
    {
          class1 += 1;
          printf("First class is open!\n");
    } else {
          printf("First class is full, please choose another class\n");
    }

      } else if (ticketin == 2)
      { 
        // Check to make sure business class is not full
        if (class2 < 12)
    {
          class2 += 1;
          printf("Business class is open!\n");
    } else {
          printf("Business class is full, please choose another class\n");

          if (class1 < 12)
      {
            printf("Upgrade to First Class by entering 1");
      }
    }

      } else if (ticketin == 3)
      {
        // Check to make sure business class is not full
        if (class3 < 12)
    {
          class3 += 1;
          printf("Economy class is open!\n");
    } else 
    {
          printf("Economy class is full, please choose another class\n");

          if (class1 < 12)
      {
            printf("Upgrade to First Class by entering 1");
      }

          if (class2 < 12)
      {
            printf("Upgrade to Business Class by entering 2");
      }
    }
      } else
      {
        ticketin = 4;
      }

      // Prompt the user for their name
      printf("Please input your name:\n");
      scanf(" %s", namein);

    } while (ticketin == 4);


    // Handle loading the new passenger into plane array
    switch (ticketin)
    {
      case 1:
        for (i=0; i<12; i)
        {
          if (plane[i]->taken == 0)
          {
            plane[i]->taken = 1;
            strcpy(plane[i]->name, namein);
            sNum = plane[i]->sNum;
          } else
          {
            i++;
          }
        }

      case 2:
        for (i=12; i<24; i)
        {
          if (plane[i]->taken == 0)
          {
            plane[i]->taken = 1;
            strcpy(plane[i]->name, namein);
            sNum = plane[i]->sNum;
          } else
          {
            i++;
          }
        }

      case 3:
        for (i=24; i<60; i)
        {
          if (plane[i]->taken == 0)
          {
            plane[i]->taken = 1;
            strcpy(plane[i]->name, namein);
            sNum = plane[i]->sNum;
          } else
          {
            i++;
          }
        }
    }


    printf("Menu Options: \n");
    printf("(1) Display the seating chart\n");
    printf("(2) Display the passenger manifest\n");
    printf("(3) Display a boarding pass\n");
    printf("(4) Quit\n");

    // Prompt user for their selection
    printf("Please enter your menu selection:\n");
    scanf(" %d", &input);

    // Switch case handling function calls
    switch (input)
    {
      case 1:
        seat(plane);
        break;

      case 2:
        mani(plane);
        break;

      case 3:
        printf("Please input a seat number\n");
        scanf(" %d", &passseat);
        if (passseat < 12)
    {
          passclass = 1;
    } else if (passseat < 24)
    {
          passclass = 2;
    } else
    {
          passclass = 3;
    }
        pass(passseat, plane[passseat-1]->name, passclass);
        break;

      default:
        printf("invalid menu option or quitting\n");
        break;
    }

  } while (input != 4);
}

// Display seating chart function
void seat(struct seat plane[][10])
{
  int i, sNum;
  for (i=0; i<sizeof(plane); i++)
  {
    if (plane[i]->taken == 1)
    {
      printf("Seat %d is taken\n", i++);
    } else
    {
      printf("Seat %d is not taken\n", i++);
    }
  }

}

// Display Manifest function
void mani(struct seat plane[][10])
{
  int i;

  for (i=0; i<sizeof(plane); i++)
  {
    if (plane[i]->taken == 1)
    {
      printf("Passenger %s in seat %d\n", plane[i]->name, i++);
    }
  }

}

// Display boarding pass function
void pass(int seat, char name[], int class)
{
  printf("Boarding pass for %s\n", name);
  printf("Seat Number: %d\n", seat);

  switch (class)
  {
    case 1:
      printf("First Class");
      break;

    case 2:
      printf("Business Class");
      break;

    case 3:
      printf("Economy Class");
      break;
  }
}

1 个答案:

答案 0 :(得分:1)

可能还有其他错误,但这是最明显的错误:

  for (i=0; i<sizeof(plane); i++)
  {
    plane[i]->sNum = i+1;
    plane[i]->taken = 0;
  }

sizeof(plane)plane数组中的字节数。这是10 * 6 * sizeof(struct seat),因此您正在数组之外进行编写。如果您想知道数组中元素的数量,则必须将数组的大小除以数组元素的大小:

  for (i=0; i<sizeof(plane)/sizeof(*plane); i++)
  {
    plane[i]->sNum = i+1;
    plane[i]->taken = 0;
  }

但是您的代码仅初始化数组每一行中的第一个元素。 由于它是二维数组,因此需要嵌套循环。并且您应该对.使用普通成员访问,而不是指针间接访问。

  for (i=0; i<sizeof(plane)/sizeof(plane[0]); i++)
  {
    for (int j = 0; j < sizeof(plane[i])/sizeof(plane[i][0]); j++) 
    {
      plane[i][j].sNum = i+1;
      plane[i][j].taken = 0;
    }
  }

您可以通过定义宏来简化所有sizeof的内容:

#define ROWS 6
#define COLS 10

,然后在数组声明和for循环限制中使用这些宏。

需要对在plane数组上循环的其他代码进行类似的更改。