使用函数无法增加C中的计数

时间:2019-05-19 05:57:35

标签: c

我正在使用businessFunction增加countBusinessSeats的值,但值始终返回1。

这是我在C中的第一个任务,我很难解决如何解决问题。我的老师希望我们使用函数,因此在增加countBusinessSeats的值时,我必须使用函数。问题是,当我运行程序时,我总是得到值1而不是2,3,4等。如何解决?

#include <stdio.h>
#include <stdlib.h>

void businessFunction (int *countBusinessSeats);

int main ()
{

  unsigned int choice;

  int countBusinessSeats= 1;


  int maxEconomySeats = 10;

  printf ("Welcome to TRU airlines! \n");
  printf("If you would like to purchase a ticket in  a business class, please enter 1\n");
  printf ("If you would like a ticket in an economy class, please enter 2\n");
  printf ("If you do not want to purchase a ticket today, please press 0\n");
  scanf ("%u", &choice);

  while (1 == choice || 2 == choice)
    {

      if (1 == choice && countBusinessSeats < (maxBusinessSeats + 1))
    {

      businessFunction (&countBusinessSeats);
    }

    ...


    }
  return 0;
}

void businessFunction (int *countBusinessSeats)
{

          printf ("Thank you for choosing ABC airlines! \n");
      printf ("You purchased a ticket in a business class\n");
      printf ("Your seat number is: %d\n",*countBusinessSeats);
     *countBusinessSeats++;

}

这是我的程序产生的结果:由于countBusinessSeats不会增加,因此它总是预订1号座位。

感谢您选择ABC航空公司!
您购买了商务舱机票
您的座位号是:1
另一张票?
1
感谢您选择ABC航空公司!
您购买了商务舱机票
您的座位号是:1

0 个答案:

没有答案